@@ -270,11 +270,10 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
270270 } else {
271271 $ tempheaders = $ headers ;
272272 }
273- $ headers = array ();
273+ $ headers = array ();
274+ $ parsed_headers = array ();
274275
275- // If it's actually got contents.
276276 if ( ! empty ( $ tempheaders ) ) {
277- // Iterate through the raw headers.
278277 foreach ( (array ) $ tempheaders as $ header ) {
279278 if ( ! str_contains ( $ header , ': ' ) ) {
280279 if ( false !== stripos ( $ header , 'boundary= ' ) ) {
@@ -287,26 +286,34 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
287286 list ( $ name , $ content ) = explode ( ': ' , trim ( $ header ), 2 );
288287
289288 // Cleanup crew.
290- $ name = trim ( $ name );
289+ $ name = strtolower ( trim ( $ name ) );
291290 $ content = trim ( $ content );
292291
293- switch ( strtolower ( $ name ) ) {
294- // Mainly for legacy -- process a "From:" header if it's there.
295- case 'content-type ' :
296- if ( str_contains ( $ content , '; ' ) ) {
297- list ( $ type , $ charset_content ) = explode ( '; ' , $ content );
298- $ content_type = trim ( $ type );
299- if ( false !== stripos ( $ charset_content , 'charset= ' ) ) {
300- $ charset = trim ( str_replace ( array ( 'charset= ' , '" ' ), '' , $ charset_content ) );
301- } elseif ( false !== stripos ( $ charset_content , 'boundary= ' ) ) {
302- $ boundary = trim ( str_replace ( array ( 'BOUNDARY= ' , 'boundary= ' , '" ' ), '' , $ charset_content ) );
303- }
292+ $ parsed_headers [ $ name ] = $ content ;
293+ }
304294
305- // Avoid setting an empty $content_type.
306- } elseif ( '' !== trim ( $ content ) ) {
307- $ content_type = trim ( $ content );
308- }
309- break ;
295+ /**
296+ * Set the charset from Content-Type, if present.
297+ */
298+ if ( isset ( $ parsed_headers ['content-type ' ] ) ) {
299+ $ content = $ parsed_headers ['content-type ' ];
300+ if ( str_contains ( $ content , '; ' ) ) {
301+ list ( $ type , $ charset_content ) = explode ( '; ' , $ content );
302+ $ content_type = trim ( $ type );
303+ if ( false !== stripos ( $ charset_content , 'charset= ' ) ) {
304+ $ charset = trim ( str_replace ( array ( 'charset= ' , '" ' ), '' , $ charset_content ) );
305+ } elseif ( false !== stripos ( $ charset_content , 'boundary= ' ) ) {
306+ $ boundary = trim ( str_replace ( array ( 'BOUNDARY= ' , 'boundary= ' , '" ' ), '' , $ charset_content ) );
307+ }
308+
309+ // Avoid setting an empty $content_type.
310+ } elseif ( '' !== trim ( $ content ) ) {
311+ $ content_type = trim ( $ content );
312+ }
313+ }
314+
315+ foreach ( $ parsed_headers as $ name => $ content ) {
316+ switch ( $ name ) {
310317 case 'from ' :
311318 if ( ! empty ( $ content ) ) {
312319 $ addresses = $ phpmailer ->parseAddresses ( $ content , null , $ charset );
@@ -333,6 +340,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
333340 break ;
334341 }
335342 }
343+ $ parsed_headers = array ();
336344 }
337345 }
338346
0 commit comments