Skip to content

Commit 443eb50

Browse files
committed
Merge branch 'PHP-8.2'
2 parents 0df92d2 + cc931af commit 443eb50

File tree

8 files changed

+78
-11
lines changed

8 files changed

+78
-11
lines changed

ext/mbstring/mbstring.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4318,7 +4318,9 @@ PHP_FUNCTION(mb_send_mail)
43184318
if (orig_str.encoding->no_encoding == mbfl_no_encoding_invalid || orig_str.encoding->no_encoding == mbfl_no_encoding_pass) {
43194319
orig_str.encoding = mb_guess_encoding((unsigned char*)subject, subject_len, MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size), MBSTRG(strict_detection));
43204320
}
4321-
pstr = mbfl_mime_header_encode(&orig_str, &conv_str, tran_cs, head_enc, CRLF, sizeof("Subject: [PHP-jp nnnnnnnn]" CRLF) - 1);
4321+
const char *line_sep = PG(mail_mixed_lf_and_crlf) ? "\n" : CRLF;
4322+
size_t line_sep_len = strlen(line_sep);
4323+
pstr = mbfl_mime_header_encode(&orig_str, &conv_str, tran_cs, head_enc, line_sep, strlen("Subject: [PHP-jp nnnnnnnn]") + line_sep_len);
43224324
if (pstr != NULL) {
43234325
subject_buf = subject = (char *)pstr->val;
43244326
}
@@ -4356,14 +4358,14 @@ PHP_FUNCTION(mb_send_mail)
43564358
n = ZSTR_LEN(str_headers);
43574359
mbfl_memory_device_strncat(&device, p, n);
43584360
if (n > 0 && p[n - 1] != '\n') {
4359-
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
4361+
mbfl_memory_device_strncat(&device, line_sep, line_sep_len);
43604362
}
43614363
zend_string_release_ex(str_headers, 0);
43624364
}
43634365

43644366
if (!zend_hash_str_exists(&ht_headers, "mime-version", sizeof("mime-version") - 1)) {
43654367
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER1, sizeof(PHP_MBSTR_MAIL_MIME_HEADER1) - 1);
4366-
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
4368+
mbfl_memory_device_strncat(&device, line_sep, line_sep_len);
43674369
}
43684370

43694371
if (!suppressed_hdrs.cnt_type) {
@@ -4374,7 +4376,7 @@ PHP_FUNCTION(mb_send_mail)
43744376
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER3, sizeof(PHP_MBSTR_MAIL_MIME_HEADER3) - 1);
43754377
mbfl_memory_device_strcat(&device, p);
43764378
}
4377-
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
4379+
mbfl_memory_device_strncat(&device, line_sep, line_sep_len);
43784380
}
43794381
if (!suppressed_hdrs.cnt_trans_enc) {
43804382
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER4, sizeof(PHP_MBSTR_MAIL_MIME_HEADER4) - 1);
@@ -4383,10 +4385,12 @@ PHP_FUNCTION(mb_send_mail)
43834385
p = "7bit";
43844386
}
43854387
mbfl_memory_device_strcat(&device, p);
4386-
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
4388+
mbfl_memory_device_strncat(&device, line_sep, line_sep_len);
43874389
}
43884390

4389-
mbfl_memory_device_unput(&device);
4391+
if (!PG(mail_mixed_lf_and_crlf)) {
4392+
mbfl_memory_device_unput(&device);
4393+
}
43904394
mbfl_memory_device_unput(&device);
43914395
mbfl_memory_device_output('\0', &device);
43924396
str_headers = zend_string_init((char *)device.buffer, strlen((char *)device.buffer), 0);

ext/mbstring/tests/gh8086.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
GH-8086 (mb_send_mail() function not working correctly in PHP 8.x)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("mbstring")) die("skip mbstring extension not available");
6+
?>
7+
--INI--
8+
sendmail_path={MAIL:{PWD}/gh8086.eml}
9+
mail.mixed_lf_and_crlf=on
10+
--FILE--
11+
<?php
12+
mb_internal_encoding("UTF-8");
13+
mb_language("uni");
14+
$to = "omittedvalidaddress@example.com";
15+
$subject = "test mail";
16+
$message = "body of testing php mail";
17+
$header["Mime-Version"] = "1.0";
18+
$header["Content-Type"] = "text/html; charset=UTF-8";
19+
$header["From"] = "omittedvalidaddress2@example.com";
20+
$header["X-Mailer"] = "PHP/" . phpversion();
21+
mb_send_mail($to, $subject, $message, $header);
22+
23+
$stream = fopen(__DIR__ . "/gh8086.eml", "rb");
24+
$eml = stream_get_contents($stream);
25+
fclose($stream);
26+
var_dump(preg_match_all('/(?<!\r)\n/', $eml));
27+
?>
28+
--CLEAN--
29+
<?php
30+
@unlink(__DIR__ . "/gh8086.eml");
31+
?>
32+
--EXPECT--
33+
int(6)

ext/standard/mail.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,14 +429,16 @@ PHPAPI int php_mail(const char *to, const char *subject, const char *message, co
429429
MAIL_RET(0);
430430
}
431431

432+
char *line_sep = PG(mail_mixed_lf_and_crlf) ? "\n" : "\r\n";
433+
432434
if (PG(mail_x_header)) {
433435
const char *tmp = zend_get_executed_filename();
434436
zend_string *f;
435437

436438
f = php_basename(tmp, strlen(tmp), NULL, 0);
437439

438440
if (headers != NULL && *headers) {
439-
spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\r\n%s", php_getuid(), ZSTR_VAL(f), headers);
441+
spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s%s%s", php_getuid(), ZSTR_VAL(f), line_sep, headers);
440442
} else {
441443
spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s", php_getuid(), ZSTR_VAL(f));
442444
}
@@ -510,12 +512,12 @@ PHPAPI int php_mail(const char *to, const char *subject, const char *message, co
510512
MAIL_RET(0);
511513
}
512514
#endif
513-
fprintf(sendmail, "To: %s\r\n", to);
514-
fprintf(sendmail, "Subject: %s\r\n", subject);
515+
fprintf(sendmail, "To: %s%s", to, line_sep);
516+
fprintf(sendmail, "Subject: %s%s", subject, line_sep);
515517
if (hdr != NULL) {
516-
fprintf(sendmail, "%s\r\n", hdr);
518+
fprintf(sendmail, "%s%s", hdr, line_sep);
517519
}
518-
fprintf(sendmail, "\r\n%s\r\n", message);
520+
fprintf(sendmail, "%s%s%s", line_sep, message, line_sep);
519521
ret = pclose(sendmail);
520522

521523
#if PHP_SIGCHILD

ext/standard/tests/mail/gh8086.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-8086 (Mail() function not working correctly in PHP 8.x)
3+
--INI--
4+
sendmail_path={MAIL:gh8086.out}
5+
mail.mixed_lf_and_crlf=on
6+
--FILE--
7+
<?php
8+
var_dump(mail('user@example.com', 'Test Subject', 'A Message', 'KHeaders'));
9+
$mail = file_get_contents('gh8086.out');
10+
var_dump(preg_match_all('/(?<!\r)\n/', $mail));
11+
?>
12+
--CLEAN--
13+
<?php
14+
unlink('gh8086.out');
15+
?>
16+
--EXPECT--
17+
bool(true)
18+
int(5)

main/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ PHP_INI_BEGIN()
738738
PHP_INI_ENTRY("SMTP", "localhost",PHP_INI_ALL, NULL)
739739
PHP_INI_ENTRY("smtp_port", "25", PHP_INI_ALL, NULL)
740740
STD_PHP_INI_BOOLEAN("mail.add_x_header", "0", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, mail_x_header, php_core_globals, core_globals)
741+
STD_PHP_INI_BOOLEAN("mail.mixed_lf_and_crlf", "0", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, mail_mixed_lf_and_crlf, php_core_globals, core_globals)
741742
STD_PHP_INI_ENTRY("mail.log", NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateMailLog, mail_log, php_core_globals, core_globals)
742743
PHP_INI_ENTRY("browscap", NULL, PHP_INI_SYSTEM, OnChangeBrowscap)
743744
PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit)

main/php_globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ struct _php_core_globals {
153153
char *request_order;
154154

155155
bool mail_x_header;
156+
bool mail_mixed_lf_and_crlf;
156157
char *mail_log;
157158

158159
bool in_error_log;

php.ini-development

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,10 @@ smtp_port = 25
11051105
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
11061106
mail.add_x_header = Off
11071107

1108+
; Use mixed LF and CRLF line separators to keep compatibility with some
1109+
; RFC 2822 non conformant MTA.
1110+
mail.mixed_lf_and_crlf = Off
1111+
11081112
; The path to a log file that will log all mail() calls. Log entries include
11091113
; the full path of the script, line number, To address and headers.
11101114
;mail.log =

php.ini-production

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,10 @@ smtp_port = 25
11071107
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
11081108
mail.add_x_header = Off
11091109

1110+
; Use mixed LF and CRLF line separators to keep compatibility with some
1111+
; RFC 2822 non conformant MTA.
1112+
mail.mixed_lf_and_crlf = Off
1113+
11101114
; The path to a log file that will log all mail() calls. Log entries include
11111115
; the full path of the script, line number, To address and headers.
11121116
;mail.log =

0 commit comments

Comments
 (0)