Skip to content

Commit 6983ae7

Browse files
committed
Fix #47983: mixed LF and CRLF line endings in mail()
Email headers are supposed to be separated with CRLF. Period.
1 parent 737f7dd commit 6983ae7

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ PHP NEWS
125125
is the last char). (Islam Israfilov)
126126
. Fixed bug #75902 (str_replace should warn when misused with nested arrays).
127127
(Nikita)
128+
. Fixed bug #47983 (mixed LF and CRLF line endings in mail()). (cmb)
128129
. Made quoting of cmd execution functions consistent. (cmb)
129130

130131
- tidy:

ext/standard/mail.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
486486
f = php_basename(tmp, strlen(tmp), NULL, 0);
487487

488488
if (headers != NULL && *headers) {
489-
spprintf(&hdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\n%s", php_getuid(), ZSTR_VAL(f), headers);
489+
spprintf(&hdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\r\n%s", php_getuid(), ZSTR_VAL(f), headers);
490490
} else {
491491
spprintf(&hdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s", php_getuid(), ZSTR_VAL(f));
492492
}
@@ -559,12 +559,12 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
559559
MAIL_RET(0);
560560
}
561561
#endif
562-
fprintf(sendmail, "To: %s\n", to);
563-
fprintf(sendmail, "Subject: %s\n", subject);
562+
fprintf(sendmail, "To: %s\r\n", to);
563+
fprintf(sendmail, "Subject: %s\r\n", subject);
564564
if (hdr != NULL) {
565-
fprintf(sendmail, "%s\n", hdr);
565+
fprintf(sendmail, "%s\r\n", hdr);
566566
}
567-
fprintf(sendmail, "\n%s\n", message);
567+
fprintf(sendmail, "\r\n%s\r\n", message);
568568
ret = pclose(sendmail);
569569

570570
#if PHP_SIGCHILD

ext/standard/tests/mail/bug47983.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #47983 (mixed LF and CRLF line endings in mail())
3+
--INI--
4+
sendmail_path={MAIL:bug47983.out}
5+
--FILE--
6+
<?php
7+
var_dump(mail('[email protected]', 'Test Subject', 'A Message', 'KHeaders'));
8+
$mail = file_get_contents('bug47983.out');
9+
var_dump(preg_match_all('/(?<!\r)\n/', $mail));
10+
?>
11+
--CLEAN--
12+
<?php
13+
unlink('bug47983.out');
14+
?>
15+
--EXPECT--
16+
bool(true)
17+
int(0)

0 commit comments

Comments
 (0)