@@ -294,17 +294,19 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
294294 } else {
295295 if ( ! is_array ( $ headers ) ) {
296296 /*
297- * Explode the headers out, so this function can take
298- * both string headers and an array of headers .
297+ * Process headers and handle folding in a single pass.
298+ * Lines starting with whitespace (space or tab) are continuations of the previous line .
299299 */
300- $ tempheaders = explode ( "\n" , str_replace ( "\r\n" , "\n" , $ headers ) );
301-
302- // Line which starts with whitespace (space or tab) is a continuation of previous line, need to keep them as one.
303- for ( $ index = 0 ; $ index < count ( $ tempheaders ); $ index ++ ) {
304- if ( $ index > 0 && isset ( $ tempheaders [ $ index ] ) && ( ' ' === $ tempheaders [ $ index ][0 ] || "\t" === $ tempheaders [ $ index ][0 ] ) ) {
305- $ tempheaders [ $ index - 1 ] .= "\n" . $ tempheaders [ $ index ];
306- array_splice ( $ tempheaders , $ index , 1 );
307- --$ index ;
300+ $ tempheaders = array ();
301+ $ normalized_headers = str_replace ( "\r\n" , "\n" , $ headers );
302+
303+ foreach ( explode ( "\n" , $ normalized_headers ) as $ header_line ) {
304+ if ( ! empty ( $ tempheaders ) && isset ( $ header_line [0 ] ) && ( ' ' === $ header_line [0 ] || "\t" === $ header_line [0 ] ) ) {
305+ // Continuation line - append to previous header.
306+ $ last_index = count ( $ tempheaders ) - 1 ;
307+ $ tempheaders [ $ last_index ] .= "\n" . $ header_line ;
308+ } else {
309+ $ tempheaders [] = $ header_line ;
308310 }
309311 }
310312 } else {
0 commit comments