Back to index

php5  5.3.10
mail.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Rasmus Lerdorf <rasmus@php.net>                              |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: mail.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include <stdlib.h>
00022 #include <ctype.h>
00023 #include <stdio.h>
00024 #include "php.h"
00025 #include "ext/standard/info.h"
00026 #include "ext/standard/php_string.h"
00027 #include "ext/standard/basic_functions.h"
00028 
00029 #if HAVE_SYSEXITS_H
00030 #include <sysexits.h>
00031 #endif
00032 #if HAVE_SYS_SYSEXITS_H
00033 #include <sys/sysexits.h>
00034 #endif
00035 
00036 #if PHP_SIGCHILD
00037 #if HAVE_SIGNAL_H
00038 #include <signal.h>
00039 #endif
00040 #endif
00041 
00042 #include "php_mail.h"
00043 #include "php_ini.h"
00044 #include "php_string.h"
00045 #include "safe_mode.h"
00046 #include "exec.h"
00047 
00048 #ifdef PHP_WIN32
00049 #include "win32/sendmail.h"
00050 #endif
00051 
00052 #ifdef NETWARE
00053 #define EX_OK           0       /* successful termination */
00054 #define EX_TEMPFAIL     75      /* temp failure; user is invited to retry */
00055 #endif
00056 
00057 #define SKIP_LONG_HEADER_SEP(str, pos)                                                                                                                    \
00058        if (str[pos] == '\r' && str[pos + 1] == '\n' && (str[pos + 2] == ' ' || str[pos + 2] == '\t')) {  \
00059               pos += 2;                                                                                                                                                        \
00060               while (str[pos + 1] == ' ' || str[pos + 1] == '\t') {                                                                         \
00061                      pos++;                                                                                                                                                    \
00062               }                                                                                                                                                                       \
00063               continue;                                                                                                                                                        \
00064        }                                                                                                                                                                              \
00065 
00066 #define MAIL_ASCIIZ_CHECK(str, len)                            \
00067        p = str;                                                              \
00068        e = p + len;                                                   \
00069        while ((p = memchr(p, '\0', (e - p)))) {  \
00070               *p = ' ';                                                      \
00071        }                                                                            \
00072 
00073 extern long php_getuid(void);
00074 
00075 /* {{{ proto int ezmlm_hash(string addr)
00076    Calculate EZMLM list hash value. */
00077 PHP_FUNCTION(ezmlm_hash)
00078 {
00079        char *str = NULL;
00080        unsigned int h = 5381;
00081        int j, str_len;
00082 
00083        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
00084               return;
00085        }
00086 
00087        for (j = 0; j < str_len; j++) {
00088               h = (h + (h << 5)) ^ (unsigned long) (unsigned char) tolower(str[j]);
00089        }
00090 
00091        h = (h % 53);
00092 
00093        RETURN_LONG((int) h);
00094 }
00095 /* }}} */
00096 
00097 /* {{{ proto int mail(string to, string subject, string message [, string additional_headers [, string additional_parameters]])
00098    Send an email message */
00099 PHP_FUNCTION(mail)
00100 {
00101        char *to=NULL, *message=NULL, *headers=NULL, *headers_trimmed=NULL;
00102        char *subject=NULL, *extra_cmd=NULL;
00103        int to_len, message_len, headers_len = 0;
00104        int subject_len, extra_cmd_len = 0, i;
00105        char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
00106        char *to_r, *subject_r;
00107        char *p, *e;
00108 
00109        if (PG(safe_mode) && (ZEND_NUM_ARGS() == 5)) {
00110               php_error_docref(NULL TSRMLS_CC, E_WARNING, "SAFE MODE Restriction in effect.  The fifth parameter is disabled in SAFE MODE");
00111               RETURN_FALSE;
00112        }
00113 
00114        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|ss", &to, &to_len, &subject, &subject_len, &message, &message_len,
00115                                                                                                                        &headers, &headers_len, &extra_cmd, &extra_cmd_len) == FAILURE
00116        ) {
00117               return;
00118        }
00119 
00120        /* ASCIIZ check */
00121        MAIL_ASCIIZ_CHECK(to, to_len);
00122        MAIL_ASCIIZ_CHECK(subject, subject_len);
00123        MAIL_ASCIIZ_CHECK(message, message_len);
00124        if (headers) {
00125               MAIL_ASCIIZ_CHECK(headers, headers_len);
00126               headers_trimmed = php_trim(headers, headers_len, NULL, 0, NULL, 2 TSRMLS_CC);
00127        }
00128        if (extra_cmd) {
00129               MAIL_ASCIIZ_CHECK(extra_cmd, extra_cmd_len);
00130        }
00131 
00132        if (to_len > 0) {
00133               to_r = estrndup(to, to_len);
00134               for (; to_len; to_len--) {
00135                      if (!isspace((unsigned char) to_r[to_len - 1])) {
00136                             break;
00137                      }
00138                      to_r[to_len - 1] = '\0';
00139               }
00140               for (i = 0; to_r[i]; i++) {
00141                      if (iscntrl((unsigned char) to_r[i])) {
00142                             /* According to RFC 822, section 3.1.1 long headers may be separated into
00143                              * parts using CRLF followed at least one linear-white-space character ('\t' or ' ').
00144                              * To prevent these separators from being replaced with a space, we use the
00145                              * SKIP_LONG_HEADER_SEP to skip over them. */
00146                             SKIP_LONG_HEADER_SEP(to_r, i);
00147                             to_r[i] = ' ';
00148                      }
00149               }
00150        } else {
00151               to_r = to;
00152        }
00153 
00154        if (subject_len > 0) {
00155               subject_r = estrndup(subject, subject_len);
00156               for (; subject_len; subject_len--) {
00157                      if (!isspace((unsigned char) subject_r[subject_len - 1])) {
00158                             break;
00159                      }
00160                      subject_r[subject_len - 1] = '\0';
00161               }
00162               for (i = 0; subject_r[i]; i++) {
00163                      if (iscntrl((unsigned char) subject_r[i])) {
00164                             SKIP_LONG_HEADER_SEP(subject_r, i);
00165                             subject_r[i] = ' ';
00166                      }
00167               }
00168        } else {
00169               subject_r = subject;
00170        }
00171 
00172        if (force_extra_parameters) {
00173               extra_cmd = php_escape_shell_cmd(force_extra_parameters);
00174        } else if (extra_cmd) {
00175               extra_cmd = php_escape_shell_cmd(extra_cmd);
00176        }
00177 
00178        if (php_mail(to_r, subject_r, message, headers_trimmed, extra_cmd TSRMLS_CC)) {
00179               RETVAL_TRUE;
00180        } else {
00181               RETVAL_FALSE;
00182        }
00183 
00184        if (headers_trimmed) {
00185               efree(headers_trimmed);
00186        }
00187 
00188        if (extra_cmd) {
00189               efree (extra_cmd);
00190        }
00191        if (to_r != to) {
00192               efree(to_r);
00193        }
00194        if (subject_r != subject) {
00195               efree(subject_r);
00196        }
00197 }
00198 /* }}} */
00199 
00200 /* {{{ php_mail
00201  */
00202 PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd TSRMLS_DC)
00203 {
00204 #if (defined PHP_WIN32 || defined NETWARE)
00205        int tsm_err;
00206        char *tsm_errmsg = NULL;
00207 #endif
00208        FILE *sendmail;
00209        int ret;
00210        char *sendmail_path = INI_STR("sendmail_path");
00211        char *sendmail_cmd = NULL;
00212        char *mail_log = INI_STR("mail.log");
00213        char *hdr = headers;
00214 #if PHP_SIGCHILD
00215        void (*sig_handler)() = NULL;
00216 #endif
00217 
00218 #define MAIL_RET(val) \
00219        if (hdr != headers) {       \
00220               efree(hdr);   \
00221        }      \
00222        return val;   \
00223 
00224        if (mail_log && *mail_log) {
00225               char *tmp;
00226               int l = spprintf(&tmp, 0, "mail() on [%s:%d]: To: %s -- Headers: %s\n", zend_get_executed_filename(TSRMLS_C), zend_get_executed_lineno(TSRMLS_C), to, hdr ? hdr : "");
00227               php_stream *stream = php_stream_open_wrapper(mail_log, "a", IGNORE_URL_WIN | REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR, NULL);
00228 
00229               if (hdr) { /* find all \r\n instances and replace them with spaces, so a log line is always one line long */ 
00230                      char *p = tmp;
00231                      while ((p = strpbrk(p, "\r\n"))) {
00232                             *p = ' ';
00233                      }
00234                      tmp[l - 1] = '\n';
00235               }
00236               if (stream) {
00237                      php_stream_write(stream, tmp, l);
00238                      php_stream_close(stream);
00239               }
00240               efree(tmp);
00241        }
00242        if (PG(mail_x_header)) {
00243               char *tmp = zend_get_executed_filename(TSRMLS_C);
00244               char *f;
00245               size_t f_len;
00246 
00247               php_basename(tmp, strlen(tmp), NULL, 0,&f, &f_len TSRMLS_CC);
00248 
00249               if (headers != NULL) {
00250                      spprintf(&hdr, 0, "X-PHP-Originating-Script: %ld:%s\n%s", php_getuid(), f, headers);
00251               } else {
00252                      spprintf(&hdr, 0, "X-PHP-Originating-Script: %ld:%s\n", php_getuid(), f);
00253               }
00254               efree(f);
00255        }
00256 
00257        if (!sendmail_path) {
00258 #if (defined PHP_WIN32 || defined NETWARE)
00259               /* handle old style win smtp sending */
00260               if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL TSRMLS_CC) == FAILURE) {
00261                      if (tsm_errmsg) {
00262                             php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tsm_errmsg);
00263                             efree(tsm_errmsg);
00264                      } else {
00265                             php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", GetSMErrorText(tsm_err));
00266                      }
00267                      MAIL_RET(0);
00268               }
00269               MAIL_RET(1);
00270 #else
00271               MAIL_RET(0);
00272 #endif
00273        }
00274        if (extra_cmd != NULL) {
00275               spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd);
00276        } else {
00277               sendmail_cmd = sendmail_path;
00278        }
00279 
00280 #if PHP_SIGCHILD
00281        /* Set signal handler of SIGCHLD to default to prevent other signal handlers
00282         * from being called and reaping the return code when our child exits.
00283         * The original handler needs to be restored after pclose() */
00284        sig_handler = (void *)signal(SIGCHLD, SIG_DFL);
00285        if (sig_handler == SIG_ERR) {
00286               sig_handler = NULL;
00287        }
00288 #endif
00289 
00290 #ifdef PHP_WIN32
00291        sendmail = popen(sendmail_cmd, "wb");
00292 #else
00293        /* Since popen() doesn't indicate if the internal fork() doesn't work
00294         * (e.g. the shell can't be executed) we explicitely set it to 0 to be
00295         * sure we don't catch any older errno value. */
00296        errno = 0;
00297        sendmail = popen(sendmail_cmd, "w");
00298 #endif
00299        if (extra_cmd != NULL) {
00300               efree (sendmail_cmd);
00301        }
00302 
00303        if (sendmail) {
00304 #ifndef PHP_WIN32
00305               if (EACCES == errno) {
00306                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
00307                      pclose(sendmail);
00308 #if PHP_SIGCHILD
00309                      /* Restore handler in case of error on Windows
00310                         Not sure if this applicable on Win but just in case. */
00311                      if (sig_handler) {
00312                             signal(SIGCHLD, sig_handler);
00313                      }
00314 #endif
00315                      MAIL_RET(0);
00316               }
00317 #endif
00318               fprintf(sendmail, "To: %s\n", to);
00319               fprintf(sendmail, "Subject: %s\n", subject);
00320               if (hdr != NULL) {
00321                      fprintf(sendmail, "%s\n", hdr);
00322               }
00323               fprintf(sendmail, "\n%s\n", message);
00324               ret = pclose(sendmail);
00325 
00326 #if PHP_SIGCHILD
00327               if (sig_handler) {
00328                      signal(SIGCHLD, sig_handler);
00329               }
00330 #endif
00331 
00332 #ifdef PHP_WIN32
00333               if (ret == -1)
00334 #else
00335 #if defined(EX_TEMPFAIL)
00336               if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
00337 #elif defined(EX_OK)
00338               if (ret != EX_OK)
00339 #else
00340               if (ret != 0)
00341 #endif
00342 #endif
00343               {
00344                      MAIL_RET(0);
00345               } else {
00346                      MAIL_RET(1);
00347               }
00348        } else {
00349               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
00350 #if PHP_SIGCHILD
00351               if (sig_handler) {
00352                      signal(SIGCHLD, sig_handler);                                         
00353               }
00354 #endif
00355               MAIL_RET(0);
00356        }
00357 
00358        MAIL_RET(1); /* never reached */
00359 }
00360 /* }}} */
00361 
00362 /* {{{ PHP_MINFO_FUNCTION
00363  */
00364 PHP_MINFO_FUNCTION(mail)
00365 {
00366        char *sendmail_path = INI_STR("sendmail_path");
00367 
00368 #ifdef PHP_WIN32
00369        if (!sendmail_path) {
00370               php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled");
00371        } else {
00372               php_info_print_table_row(2, "Path to sendmail", sendmail_path);
00373        }
00374 #else
00375        php_info_print_table_row(2, "Path to sendmail", sendmail_path);
00376 #endif
00377 }
00378 /* }}} */
00379 
00380 /*
00381  * Local variables:
00382  * tab-width: 4
00383  * c-basic-offset: 4
00384  * End:
00385  * vim600: sw=4 ts=4 fdm=marker
00386  * vim<600: sw=4 ts=4
00387  */