mail.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Rasmus Lerdorf <rasmus@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include <stdlib.h>
  19. #include <ctype.h>
  20. #include <stdio.h>
  21. #include <time.h>
  22. #include "php.h"
  23. #include "ext/standard/info.h"
  24. #include "ext/standard/php_string.h"
  25. #include "ext/standard/basic_functions.h"
  26. #include "ext/date/php_date.h"
  27. #include "zend_smart_str.h"
  28. #if HAVE_SYSEXITS_H
  29. #include <sysexits.h>
  30. #endif
  31. #if HAVE_SYS_SYSEXITS_H
  32. #include <sys/sysexits.h>
  33. #endif
  34. #if PHP_SIGCHILD
  35. #if HAVE_SIGNAL_H
  36. #include <signal.h>
  37. #endif
  38. #endif
  39. #include "php_syslog.h"
  40. #include "php_mail.h"
  41. #include "php_ini.h"
  42. #include "php_string.h"
  43. #include "exec.h"
  44. #ifdef PHP_WIN32
  45. #include "win32/sendmail.h"
  46. #endif
  47. #define SKIP_LONG_HEADER_SEP(str, pos) \
  48. if (str[pos] == '\r' && str[pos + 1] == '\n' && (str[pos + 2] == ' ' || str[pos + 2] == '\t')) { \
  49. pos += 2; \
  50. while (str[pos + 1] == ' ' || str[pos + 1] == '\t') { \
  51. pos++; \
  52. } \
  53. continue; \
  54. } \
  55. #define MAIL_ASCIIZ_CHECK(str, len) \
  56. p = str; \
  57. e = p + len; \
  58. while ((p = memchr(p, '\0', (e - p)))) { \
  59. *p = ' '; \
  60. } \
  61. extern zend_long php_getuid(void);
  62. /* {{{ proto int ezmlm_hash(string addr)
  63. Calculate EZMLM list hash value. */
  64. PHP_FUNCTION(ezmlm_hash)
  65. {
  66. char *str = NULL;
  67. unsigned int h = 5381;
  68. size_t j, str_len;
  69. ZEND_PARSE_PARAMETERS_START(1, 1)
  70. Z_PARAM_STRING(str, str_len)
  71. ZEND_PARSE_PARAMETERS_END();
  72. for (j = 0; j < str_len; j++) {
  73. h = (h + (h << 5)) ^ (zend_ulong) (unsigned char) tolower(str[j]);
  74. }
  75. h = (h % 53);
  76. RETURN_LONG((zend_long) h);
  77. }
  78. /* }}} */
  79. static zend_bool php_mail_build_headers_check_field_value(zval *val)
  80. {
  81. size_t len = 0;
  82. zend_string *value = Z_STR_P(val);
  83. /* https://tools.ietf.org/html/rfc2822#section-2.2.1 */
  84. /* https://tools.ietf.org/html/rfc2822#section-2.2.3 */
  85. while (len < value->len) {
  86. if (*(value->val+len) == '\r') {
  87. if (value->len - len >= 3
  88. && *(value->val+len+1) == '\n'
  89. && (*(value->val+len+2) == ' ' || *(value->val+len+2) == '\t')) {
  90. len += 3;
  91. continue;
  92. }
  93. return FAILURE;
  94. }
  95. if (*(value->val+len) == '\0') {
  96. return FAILURE;
  97. }
  98. len++;
  99. }
  100. return SUCCESS;
  101. }
  102. static zend_bool php_mail_build_headers_check_field_name(zend_string *key)
  103. {
  104. size_t len = 0;
  105. /* https://tools.ietf.org/html/rfc2822#section-2.2 */
  106. while (len < key->len) {
  107. if (*(key->val+len) < 33 || *(key->val+len) > 126 || *(key->val+len) == ':') {
  108. return FAILURE;
  109. }
  110. len++;
  111. }
  112. return SUCCESS;
  113. }
  114. static void php_mail_build_headers_elems(smart_str *s, zend_string *key, zval *val);
  115. static void php_mail_build_headers_elem(smart_str *s, zend_string *key, zval *val)
  116. {
  117. switch(Z_TYPE_P(val)) {
  118. case IS_STRING:
  119. if (php_mail_build_headers_check_field_name(key) != SUCCESS) {
  120. php_error_docref(NULL, E_WARNING, "Header field name (%s) contains invalid chars", ZSTR_VAL(key));
  121. return;
  122. }
  123. if (php_mail_build_headers_check_field_value(val) != SUCCESS) {
  124. php_error_docref(NULL, E_WARNING, "Header field value (%s => %s) contains invalid chars or format", ZSTR_VAL(key), Z_STRVAL_P(val));
  125. return;
  126. }
  127. smart_str_append(s, key);
  128. smart_str_appendl(s, ": ", 2);
  129. smart_str_appends(s, Z_STRVAL_P(val));
  130. smart_str_appendl(s, "\r\n", 2);
  131. break;
  132. case IS_ARRAY:
  133. php_mail_build_headers_elems(s, key, val);
  134. break;
  135. default:
  136. php_error_docref(NULL, E_WARNING, "headers array elements must be string or array (%s)", ZSTR_VAL(key));
  137. }
  138. }
  139. static void php_mail_build_headers_elems(smart_str *s, zend_string *key, zval *val)
  140. {
  141. zend_ulong idx;
  142. zend_string *tmp_key;
  143. zval *tmp_val;
  144. (void)(idx);
  145. ZEND_HASH_FOREACH_KEY_VAL(HASH_OF(val), idx, tmp_key, tmp_val) {
  146. if (tmp_key) {
  147. php_error_docref(NULL, E_WARNING, "Multiple header key must be numeric index (%s)", ZSTR_VAL(tmp_key));
  148. continue;
  149. }
  150. if (Z_TYPE_P(tmp_val) != IS_STRING) {
  151. php_error_docref(NULL, E_WARNING, "Multiple header values must be string (%s)", ZSTR_VAL(key));
  152. continue;
  153. }
  154. php_mail_build_headers_elem(s, key, tmp_val);
  155. } ZEND_HASH_FOREACH_END();
  156. }
  157. PHPAPI zend_string *php_mail_build_headers(zval *headers)
  158. {
  159. zend_ulong idx;
  160. zend_string *key;
  161. zval *val;
  162. smart_str s = {0};
  163. ZEND_ASSERT(Z_TYPE_P(headers) == IS_ARRAY);
  164. ZEND_HASH_FOREACH_KEY_VAL(HASH_OF(headers), idx, key, val) {
  165. if (!key) {
  166. php_error_docref(NULL, E_WARNING, "Found numeric header (" ZEND_LONG_FMT ")", idx);
  167. continue;
  168. }
  169. /* https://tools.ietf.org/html/rfc2822#section-3.6 */
  170. switch(ZSTR_LEN(key)) {
  171. case sizeof("orig-date")-1:
  172. if (!strncasecmp("orig-date", ZSTR_VAL(key), ZSTR_LEN(key))) {
  173. PHP_MAIL_BUILD_HEADER_CHECK("orig-date", s, key, val);
  174. } else {
  175. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  176. }
  177. break;
  178. case sizeof("from")-1:
  179. if (!strncasecmp("from", ZSTR_VAL(key), ZSTR_LEN(key))) {
  180. PHP_MAIL_BUILD_HEADER_CHECK("from", s, key, val);
  181. } else {
  182. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  183. }
  184. break;
  185. case sizeof("sender")-1:
  186. if (!strncasecmp("sender", ZSTR_VAL(key), ZSTR_LEN(key))) {
  187. PHP_MAIL_BUILD_HEADER_CHECK("sender", s, key, val);
  188. } else {
  189. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  190. }
  191. break;
  192. case sizeof("reply-to")-1:
  193. if (!strncasecmp("reply-to", ZSTR_VAL(key), ZSTR_LEN(key))) {
  194. PHP_MAIL_BUILD_HEADER_CHECK("reply-to", s, key, val);
  195. } else {
  196. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  197. }
  198. break;
  199. case sizeof("to")-1: /* "to", "cc" */
  200. if (!strncasecmp("to", ZSTR_VAL(key), ZSTR_LEN(key))) {
  201. php_error_docref(NULL, E_WARNING, "Extra header cannot contain 'To' header");
  202. continue;
  203. }
  204. if (!strncasecmp("cc", ZSTR_VAL(key), ZSTR_LEN(key))) {
  205. PHP_MAIL_BUILD_HEADER_CHECK("cc", s, key, val);
  206. } else {
  207. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  208. }
  209. break;
  210. case sizeof("bcc")-1:
  211. if (!strncasecmp("bcc", ZSTR_VAL(key), ZSTR_LEN(key))) {
  212. PHP_MAIL_BUILD_HEADER_CHECK("bcc", s, key, val);
  213. } else {
  214. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  215. }
  216. break;
  217. case sizeof("message-id")-1: /* "references" */
  218. if (!strncasecmp("message-id", ZSTR_VAL(key), ZSTR_LEN(key))) {
  219. PHP_MAIL_BUILD_HEADER_CHECK("message-id", s, key, val);
  220. } else if (!strncasecmp("references", ZSTR_VAL(key), ZSTR_LEN(key))) {
  221. PHP_MAIL_BUILD_HEADER_CHECK("references", s, key, val);
  222. } else {
  223. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  224. }
  225. break;
  226. case sizeof("in-reply-to")-1:
  227. if (!strncasecmp("in-reply-to", ZSTR_VAL(key), ZSTR_LEN(key))) {
  228. PHP_MAIL_BUILD_HEADER_CHECK("in-reply-to", s, key, val);
  229. } else {
  230. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  231. }
  232. break;
  233. case sizeof("subject")-1:
  234. if (!strncasecmp("subject", ZSTR_VAL(key), ZSTR_LEN(key))) {
  235. php_error_docref(NULL, E_WARNING, "Extra header cannot contain 'Subject' header");
  236. continue;
  237. }
  238. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  239. break;
  240. default:
  241. PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
  242. }
  243. } ZEND_HASH_FOREACH_END();
  244. /* Remove the last \r\n */
  245. if (s.s) s.s->len -= 2;
  246. smart_str_0(&s);
  247. return s.s;
  248. }
  249. /* {{{ proto int mail(string to, string subject, string message [, string additional_headers [, string additional_parameters]])
  250. Send an email message */
  251. PHP_FUNCTION(mail)
  252. {
  253. char *to=NULL, *message=NULL;
  254. char *subject=NULL;
  255. zend_string *extra_cmd=NULL, *str_headers=NULL, *tmp_headers;
  256. zval *headers = NULL;
  257. size_t to_len, message_len;
  258. size_t subject_len, i;
  259. char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
  260. char *to_r, *subject_r;
  261. char *p, *e;
  262. ZEND_PARSE_PARAMETERS_START(3, 5)
  263. Z_PARAM_STRING(to, to_len)
  264. Z_PARAM_STRING(subject, subject_len)
  265. Z_PARAM_STRING(message, message_len)
  266. Z_PARAM_OPTIONAL
  267. Z_PARAM_ZVAL(headers)
  268. Z_PARAM_STR(extra_cmd)
  269. ZEND_PARSE_PARAMETERS_END();
  270. /* ASCIIZ check */
  271. MAIL_ASCIIZ_CHECK(to, to_len);
  272. MAIL_ASCIIZ_CHECK(subject, subject_len);
  273. MAIL_ASCIIZ_CHECK(message, message_len);
  274. if (headers) {
  275. switch(Z_TYPE_P(headers)) {
  276. case IS_STRING:
  277. tmp_headers = zend_string_init(Z_STRVAL_P(headers), Z_STRLEN_P(headers), 0);
  278. MAIL_ASCIIZ_CHECK(ZSTR_VAL(tmp_headers), ZSTR_LEN(tmp_headers));
  279. str_headers = php_trim(tmp_headers, NULL, 0, 2);
  280. zend_string_release_ex(tmp_headers, 0);
  281. break;
  282. case IS_ARRAY:
  283. str_headers = php_mail_build_headers(headers);
  284. break;
  285. default:
  286. php_error_docref(NULL, E_WARNING, "headers parameter must be string or array");
  287. RETURN_FALSE;
  288. }
  289. }
  290. if (extra_cmd) {
  291. MAIL_ASCIIZ_CHECK(ZSTR_VAL(extra_cmd), ZSTR_LEN(extra_cmd));
  292. }
  293. if (to_len > 0) {
  294. to_r = estrndup(to, to_len);
  295. for (; to_len; to_len--) {
  296. if (!isspace((unsigned char) to_r[to_len - 1])) {
  297. break;
  298. }
  299. to_r[to_len - 1] = '\0';
  300. }
  301. for (i = 0; to_r[i]; i++) {
  302. if (iscntrl((unsigned char) to_r[i])) {
  303. /* According to RFC 822, section 3.1.1 long headers may be separated into
  304. * parts using CRLF followed at least one linear-white-space character ('\t' or ' ').
  305. * To prevent these separators from being replaced with a space, we use the
  306. * SKIP_LONG_HEADER_SEP to skip over them. */
  307. SKIP_LONG_HEADER_SEP(to_r, i);
  308. to_r[i] = ' ';
  309. }
  310. }
  311. } else {
  312. to_r = to;
  313. }
  314. if (subject_len > 0) {
  315. subject_r = estrndup(subject, subject_len);
  316. for (; subject_len; subject_len--) {
  317. if (!isspace((unsigned char) subject_r[subject_len - 1])) {
  318. break;
  319. }
  320. subject_r[subject_len - 1] = '\0';
  321. }
  322. for (i = 0; subject_r[i]; i++) {
  323. if (iscntrl((unsigned char) subject_r[i])) {
  324. SKIP_LONG_HEADER_SEP(subject_r, i);
  325. subject_r[i] = ' ';
  326. }
  327. }
  328. } else {
  329. subject_r = subject;
  330. }
  331. if (force_extra_parameters) {
  332. extra_cmd = php_escape_shell_cmd(force_extra_parameters);
  333. } else if (extra_cmd) {
  334. extra_cmd = php_escape_shell_cmd(ZSTR_VAL(extra_cmd));
  335. }
  336. if (php_mail(to_r, subject_r, message, str_headers && ZSTR_LEN(str_headers) ? ZSTR_VAL(str_headers) : NULL, extra_cmd ? ZSTR_VAL(extra_cmd) : NULL)) {
  337. RETVAL_TRUE;
  338. } else {
  339. RETVAL_FALSE;
  340. }
  341. if (str_headers) {
  342. zend_string_release_ex(str_headers, 0);
  343. }
  344. if (extra_cmd) {
  345. zend_string_release_ex(extra_cmd, 0);
  346. }
  347. if (to_r != to) {
  348. efree(to_r);
  349. }
  350. if (subject_r != subject) {
  351. efree(subject_r);
  352. }
  353. }
  354. /* }}} */
  355. void php_mail_log_crlf_to_spaces(char *message) {
  356. /* Find all instances of carriage returns or line feeds and
  357. * replace them with spaces. Thus, a log line is always one line
  358. * long
  359. */
  360. char *p = message;
  361. while ((p = strpbrk(p, "\r\n"))) {
  362. *p = ' ';
  363. }
  364. }
  365. void php_mail_log_to_syslog(char *message) {
  366. /* Write 'message' to syslog. */
  367. #ifdef HAVE_SYSLOG_H
  368. php_syslog(LOG_NOTICE, "%s", message);
  369. #endif
  370. }
  371. void php_mail_log_to_file(char *filename, char *message, size_t message_size) {
  372. /* Write 'message' to the given file. */
  373. uint32_t flags = IGNORE_URL_WIN | REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR;
  374. php_stream *stream = php_stream_open_wrapper(filename, "a", flags, NULL);
  375. if (stream) {
  376. php_stream_write(stream, message, message_size);
  377. php_stream_close(stream);
  378. }
  379. }
  380. static int php_mail_detect_multiple_crlf(char *hdr) {
  381. /* This function detects multiple/malformed multiple newlines. */
  382. if (!hdr || !strlen(hdr)) {
  383. return 0;
  384. }
  385. /* Should not have any newlines at the beginning. */
  386. /* RFC 2822 2.2. Header Fields */
  387. if (*hdr < 33 || *hdr > 126 || *hdr == ':') {
  388. return 1;
  389. }
  390. while(*hdr) {
  391. if (*hdr == '\r') {
  392. if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || (*(hdr+1) == '\n' && (*(hdr+2) == '\0' || *(hdr+2) == '\n' || *(hdr+2) == '\r'))) {
  393. /* Malformed or multiple newlines. */
  394. return 1;
  395. } else {
  396. hdr += 2;
  397. }
  398. } else if (*hdr == '\n') {
  399. if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || *(hdr+1) == '\n') {
  400. /* Malformed or multiple newlines. */
  401. return 1;
  402. } else {
  403. hdr += 2;
  404. }
  405. } else {
  406. hdr++;
  407. }
  408. }
  409. return 0;
  410. }
  411. /* {{{ php_mail
  412. */
  413. PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd)
  414. {
  415. #ifdef PHP_WIN32
  416. int tsm_err;
  417. char *tsm_errmsg = NULL;
  418. #endif
  419. FILE *sendmail;
  420. int ret;
  421. char *sendmail_path = INI_STR("sendmail_path");
  422. char *sendmail_cmd = NULL;
  423. char *mail_log = INI_STR("mail.log");
  424. char *hdr = headers;
  425. #if PHP_SIGCHILD
  426. void (*sig_handler)() = NULL;
  427. #endif
  428. #define MAIL_RET(val) \
  429. if (hdr != headers) { \
  430. efree(hdr); \
  431. } \
  432. return val; \
  433. if (mail_log && *mail_log) {
  434. char *logline;
  435. spprintf(&logline, 0, "mail() on [%s:%d]: To: %s -- Headers: %s -- Subject: %s", zend_get_executed_filename(), zend_get_executed_lineno(), to, hdr ? hdr : "", subject);
  436. if (hdr) {
  437. php_mail_log_crlf_to_spaces(logline);
  438. }
  439. if (!strcmp(mail_log, "syslog")) {
  440. php_mail_log_to_syslog(logline);
  441. } else {
  442. /* Add date when logging to file */
  443. char *tmp;
  444. time_t curtime;
  445. zend_string *date_str;
  446. size_t len;
  447. time(&curtime);
  448. date_str = php_format_date("d-M-Y H:i:s e", 13, curtime, 1);
  449. len = spprintf(&tmp, 0, "[%s] %s%s", date_str->val, logline, PHP_EOL);
  450. php_mail_log_to_file(mail_log, tmp, len);
  451. zend_string_free(date_str);
  452. efree(tmp);
  453. }
  454. efree(logline);
  455. }
  456. if (PG(mail_x_header)) {
  457. const char *tmp = zend_get_executed_filename();
  458. zend_string *f;
  459. f = php_basename(tmp, strlen(tmp), NULL, 0);
  460. if (headers != NULL && *headers) {
  461. spprintf(&hdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\n%s", php_getuid(), ZSTR_VAL(f), headers);
  462. } else {
  463. spprintf(&hdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s", php_getuid(), ZSTR_VAL(f));
  464. }
  465. zend_string_release_ex(f, 0);
  466. }
  467. if (hdr && php_mail_detect_multiple_crlf(hdr)) {
  468. php_error_docref(NULL, E_WARNING, "Multiple or malformed newlines found in additional_header");
  469. MAIL_RET(0);
  470. }
  471. if (!sendmail_path) {
  472. #ifdef PHP_WIN32
  473. /* handle old style win smtp sending */
  474. if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL) == FAILURE) {
  475. if (tsm_errmsg) {
  476. php_error_docref(NULL, E_WARNING, "%s", tsm_errmsg);
  477. efree(tsm_errmsg);
  478. } else {
  479. php_error_docref(NULL, E_WARNING, "%s", GetSMErrorText(tsm_err));
  480. }
  481. MAIL_RET(0);
  482. }
  483. MAIL_RET(1);
  484. #else
  485. MAIL_RET(0);
  486. #endif
  487. }
  488. if (extra_cmd != NULL) {
  489. spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd);
  490. } else {
  491. sendmail_cmd = sendmail_path;
  492. }
  493. #if PHP_SIGCHILD
  494. /* Set signal handler of SIGCHLD to default to prevent other signal handlers
  495. * from being called and reaping the return code when our child exits.
  496. * The original handler needs to be restored after pclose() */
  497. sig_handler = (void *)signal(SIGCHLD, SIG_DFL);
  498. if (sig_handler == SIG_ERR) {
  499. sig_handler = NULL;
  500. }
  501. #endif
  502. #ifdef PHP_WIN32
  503. sendmail = popen_ex(sendmail_cmd, "wb", NULL, NULL);
  504. #else
  505. /* Since popen() doesn't indicate if the internal fork() doesn't work
  506. * (e.g. the shell can't be executed) we explicitly set it to 0 to be
  507. * sure we don't catch any older errno value. */
  508. errno = 0;
  509. sendmail = popen(sendmail_cmd, "w");
  510. #endif
  511. if (extra_cmd != NULL) {
  512. efree (sendmail_cmd);
  513. }
  514. if (sendmail) {
  515. #ifndef PHP_WIN32
  516. if (EACCES == errno) {
  517. php_error_docref(NULL, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
  518. pclose(sendmail);
  519. #if PHP_SIGCHILD
  520. /* Restore handler in case of error on Windows
  521. Not sure if this applicable on Win but just in case. */
  522. if (sig_handler) {
  523. signal(SIGCHLD, sig_handler);
  524. }
  525. #endif
  526. MAIL_RET(0);
  527. }
  528. #endif
  529. fprintf(sendmail, "To: %s\n", to);
  530. fprintf(sendmail, "Subject: %s\n", subject);
  531. if (hdr != NULL) {
  532. fprintf(sendmail, "%s\n", hdr);
  533. }
  534. fprintf(sendmail, "\n%s\n", message);
  535. ret = pclose(sendmail);
  536. #if PHP_SIGCHILD
  537. if (sig_handler) {
  538. signal(SIGCHLD, sig_handler);
  539. }
  540. #endif
  541. #ifdef PHP_WIN32
  542. if (ret == -1)
  543. #else
  544. #if defined(EX_TEMPFAIL)
  545. if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
  546. #elif defined(EX_OK)
  547. if (ret != EX_OK)
  548. #else
  549. if (ret != 0)
  550. #endif
  551. #endif
  552. {
  553. MAIL_RET(0);
  554. } else {
  555. MAIL_RET(1);
  556. }
  557. } else {
  558. php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
  559. #if PHP_SIGCHILD
  560. if (sig_handler) {
  561. signal(SIGCHLD, sig_handler);
  562. }
  563. #endif
  564. MAIL_RET(0);
  565. }
  566. MAIL_RET(1); /* never reached */
  567. }
  568. /* }}} */
  569. /* {{{ PHP_MINFO_FUNCTION
  570. */
  571. PHP_MINFO_FUNCTION(mail)
  572. {
  573. char *sendmail_path = INI_STR("sendmail_path");
  574. #ifdef PHP_WIN32
  575. if (!sendmail_path) {
  576. php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled");
  577. } else {
  578. php_info_print_table_row(2, "Path to sendmail", sendmail_path);
  579. }
  580. #else
  581. php_info_print_table_row(2, "Path to sendmail", sendmail_path);
  582. #endif
  583. }
  584. /* }}} */
  585. /*
  586. * Local variables:
  587. * tab-width: 4
  588. * c-basic-offset: 4
  589. * End:
  590. * vim600: sw=4 ts=4 fdm=marker
  591. * vim<600: sw=4 ts=4
  592. */