From 526f6fe677c50be78ffa4745d5fe75c8c45b9afc Mon Sep 17 00:00:00 2001 From: Peter Westwood Date: Mon, 23 May 2011 11:14:05 +0000 Subject: [PATCH] Update wp_mail to correctly call the Address adding functions on PHPMailer for To, CC, BCC in a way which preserves our support for full RFC2822 address specifications. Older versions of PHPMailer were not too careful about validating what we passed in to them as a plain email address - the new version expects we pass in the Name and Email address seperately. Fixes #17305 based on a patch from dllh. git-svn-id: https://develop.svn.wordpress.org/trunk@18006 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/pluggable.php | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php index 67a4df96c7..ebcdb6fb13 100644 --- a/wp-includes/pluggable.php +++ b/wp-includes/pluggable.php @@ -296,6 +296,8 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() $tempheaders = $headers; } $headers = array(); + $cc = array(); + $bcc = array(); // If it's actually got contents if ( !empty( $tempheaders ) ) { @@ -401,7 +403,15 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() foreach ( (array) $to as $recipient ) { try { - $phpmailer->AddAddress( trim( $recipient ) ); + // Break $recipient into name and address parts if in the format "Foo " + $recipient_name = ''; + if( preg_match( '/(.+)\s?<(.+)>/', $recipient, $matches ) ) { + if ( count( $matches ) == 3 ) { + $recipient_name = $matches[1]; + $recipient = $matches[2]; + } + } + $phpmailer->AddAddress( trim( $recipient ), $recipient_name); } catch ( phpmailerException $e ) { continue; } @@ -415,7 +425,15 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() if ( !empty( $cc ) ) { foreach ( (array) $cc as $recipient ) { try { - $phpmailer->AddCc( trim($recipient) ); + // Break $recipient into name and address parts if in the format "Foo " + $recipient_name = ''; + if( preg_match( '/(.+)\s?<(.+)>/', $recipient, $matches ) ) { + if ( count( $matches ) == 3 ) { + $recipient_name = $matches[1]; + $recipient = $matches[2]; + } + } + $phpmailer->AddCc( trim($recipient), $recipient_name ); } catch ( phpmailerException $e ) { continue; } @@ -425,7 +443,15 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() if ( !empty( $bcc ) ) { foreach ( (array) $bcc as $recipient) { try { - $phpmailer->AddBcc( trim($recipient) ); + // Break $recipient into name and address parts if in the format "Foo " + $recipient_name = ''; + if( preg_match( '/(.+)\s?<(.+)>/', $recipient, $matches ) ) { + if ( count( $matches ) == 3 ) { + $recipient_name = $matches[1]; + $recipient = $matches[2]; + } + } + $phpmailer->AddBcc( trim($recipient), $recipient_name ); } catch ( phpmailerException $e ) { continue; }