@@ -269,17 +269,19 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
269
269
} else {
270
270
if ( ! is_array ( $ headers ) ) {
271
271
/*
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 .
274
274
*/
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 ;
283
285
}
284
286
}
285
287
} else {
0 commit comments