Allow multiple To: recipients in wp_mail(). Improve handling of \r\n in headers and multiple CC/BCC headers. fixes #10420

git-svn-id: http://svn.automattic.com/wordpress/trunk@13456 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2010-02-27 16:10:45 +00:00
parent 252e198ea8
commit 1cade6654c
1 changed files with 61 additions and 53 deletions

View File

@ -244,7 +244,7 @@ if ( !function_exists( 'wp_mail' ) ) :
* @uses PHPMailer * @uses PHPMailer
* @ * @
* *
* @param string $to Email address to send message * @param string|array $to Array or comma-separated list of email addresses to send message.
* @param string $subject Email subject * @param string $subject Email subject
* @param string $message Message contents * @param string $message Message contents
* @param string|array $headers Optional. Additional headers. * @param string|array $headers Optional. Additional headers.
@ -256,7 +256,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) ); extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
if ( !is_array($attachments) ) if ( !is_array($attachments) )
$attachments = explode( "\n", $attachments ); $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
global $phpmailer; global $phpmailer;
@ -274,7 +274,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
if ( !is_array( $headers ) ) { if ( !is_array( $headers ) ) {
// Explode the headers out, so this function can take both // Explode the headers out, so this function can take both
// string headers and an array of headers. // string headers and an array of headers.
$tempheaders = (array) explode( "\n", $headers ); $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
} else { } else {
$tempheaders = $headers; $tempheaders = $headers;
} }
@ -298,8 +298,9 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
$name = trim( $name ); $name = trim( $name );
$content = trim( $content ); $content = trim( $content );
switch ( strtolower( $name ) ) {
// Mainly for legacy -- process a From: header if it's there // Mainly for legacy -- process a From: header if it's there
if ( 'from' == strtolower($name) ) { case 'from':
if ( strpos($content, '<' ) !== false ) { if ( strpos($content, '<' ) !== false ) {
// So... making my life hard again? // So... making my life hard again?
$from_name = substr( $content, 0, strpos( $content, '<' ) - 1 ); $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
@ -312,7 +313,8 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
} else { } else {
$from_email = trim( $content ); $from_email = trim( $content );
} }
} elseif ( 'content-type' == strtolower($name) ) { break;
case 'content-type':
if ( strpos( $content, ';' ) !== false ) { if ( strpos( $content, ';' ) !== false ) {
list( $type, $charset ) = explode( ';', $content ); list( $type, $charset ) = explode( ';', $content );
$content_type = trim( $type ); $content_type = trim( $type );
@ -325,13 +327,17 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
} else { } else {
$content_type = trim( $content ); $content_type = trim( $content );
} }
} elseif ( 'cc' == strtolower($name) ) { break;
$cc = explode(",", $content); case 'cc':
} elseif ( 'bcc' == strtolower($name) ) { $cc = array_merge( (array) $cc, explode( ',', $content ) );
$bcc = explode(",", $content); break;
} else { case 'bcc':
$bcc = array_merge( (array) $bcc, explode( ',', $content ) );
break;
default:
// Add it to our grand headers array // Add it to our grand headers array
$headers[trim( $name )] = trim( $content ); $headers[trim( $name )] = trim( $content );
break;
} }
} }
} }
@ -348,9 +354,8 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
// From email and name // From email and name
// If we don't have a name from the input headers // If we don't have a name from the input headers
if ( !isset( $from_name ) ) { if ( !isset( $from_name ) )
$from_name = 'WordPress'; $from_name = 'WordPress';
}
/* If we don't have an email from the input headers default to wordpress@$sitename /* If we don't have an email from the input headers default to wordpress@$sitename
* Some hosts will block outgoing mail from this address if it doesn't exist but * Some hosts will block outgoing mail from this address if it doesn't exist but
@ -373,8 +378,13 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
$phpmailer->From = apply_filters( 'wp_mail_from' , $from_email ); $phpmailer->From = apply_filters( 'wp_mail_from' , $from_email );
$phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name ); $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
// Set destination address // Set destination addresses
$phpmailer->AddAddress( $to ); if ( !is_array( $to ) )
$to = explode( ',', $to );
foreach ( (array) $to as $recipient ) {
$phpmailer->AddAddress( trim( $recipient ) );
}
// Set mail's subject and body // Set mail's subject and body
$phpmailer->Subject = $subject; $phpmailer->Subject = $subject;
@ -386,6 +396,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
$phpmailer->AddCc( trim($recipient) ); $phpmailer->AddCc( trim($recipient) );
} }
} }
if ( !empty( $bcc ) ) { if ( !empty( $bcc ) ) {
foreach ( (array) $bcc as $recipient) { foreach ( (array) $bcc as $recipient) {
$phpmailer->AddBcc( trim($recipient) ); $phpmailer->AddBcc( trim($recipient) );
@ -397,23 +408,20 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
// Set Content-Type and charset // Set Content-Type and charset
// If we don't have a content-type from the input headers // If we don't have a content-type from the input headers
if ( !isset( $content_type ) ) { if ( !isset( $content_type ) )
$content_type = 'text/plain'; $content_type = 'text/plain';
}
$content_type = apply_filters( 'wp_mail_content_type', $content_type ); $content_type = apply_filters( 'wp_mail_content_type', $content_type );
$phpmailer->ContentType = $content_type; $phpmailer->ContentType = $content_type;
// Set whether it's plaintext, depending on $content_type // Set whether it's plaintext, depending on $content_type
if ( $content_type == 'text/html' ) { if ( 'text/html' == $content_type )
$phpmailer->IsHTML( true ); $phpmailer->IsHTML( true );
}
// If we don't have a charset from the input headers // If we don't have a charset from the input headers
if ( !isset( $charset ) ) { if ( !isset( $charset ) )
$charset = get_bloginfo( 'charset' ); $charset = get_bloginfo( 'charset' );
}
// Set the content-type and charset // Set the content-type and charset
$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset ); $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
@ -423,10 +431,10 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
foreach( (array) $headers as $name => $content ) { foreach( (array) $headers as $name => $content ) {
$phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
} }
if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) ) {
if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
$phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) ); $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
} }
}
if ( !empty( $attachments ) ) { if ( !empty( $attachments ) ) {
foreach ( $attachments as $attachment ) { foreach ( $attachments as $attachment ) {