Coverage Report

Created: 2025-07-23 06:33

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