Skip to content

Commit 0666157

Browse files
Mail: Fix multiline From header parsing in wp_mail()
Properly handle RFC 5322 folded headers and add MIME decoding support.
1 parent ab634ef commit 0666157

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/wp-includes/pluggable.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,19 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
269269
} else {
270270
if ( ! is_array( $headers ) ) {
271271
/*
272-
* Explode the headers out, so this function can take
273-
* both string headers and an array of headers.
272+
* Process headers and handle folding in a single pass.
273+
* Lines starting with whitespace (space or tab) are continuations of the previous line.
274274
*/
275-
$tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
276-
277-
// Line which starts with whitespace (space or tab) is a continuation of previous line, need to keep them as one.
278-
for ( $index = 0; $index < count( $tempheaders ); $index++ ) {
279-
if ( $index > 0 && isset( $tempheaders[ $index ] ) && ( ' ' === $tempheaders[ $index ][0] || "\t" === $tempheaders[ $index ][0] ) ) {
280-
$tempheaders[ $index - 1 ] .= "\n" . $tempheaders[ $index ];
281-
array_splice( $tempheaders, $index, 1 );
282-
--$index;
275+
$tempheaders = array();
276+
$normalized_headers = str_replace( "\r\n", "\n", $headers );
277+
278+
foreach ( explode( "\n", $normalized_headers ) as $header_line ) {
279+
if ( ! empty( $tempheaders ) && isset( $header_line[0] ) && ( ' ' === $header_line[0] || "\t" === $header_line[0] ) ) {
280+
// Continuation line - append to previous header.
281+
$last_index = count( $tempheaders ) - 1;
282+
$tempheaders[ $last_index ] .= "\n" . $header_line;
283+
} else {
284+
$tempheaders[] = $header_line;
283285
}
284286
}
285287
} else {

0 commit comments

Comments
 (0)