Use exceptions with phpmailer to avoid headers already sent errors. fixes #17228

git-svn-id: http://svn.automattic.com/wordpress/trunk@17753 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2011-04-28 18:16:01 +00:00
parent e8f22e3175
commit 7f9f067e60
1 changed files with 27 additions and 7 deletions

View File

@ -281,7 +281,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) { if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
require_once ABSPATH . WPINC . '/class-phpmailer.php'; require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php'; require_once ABSPATH . WPINC . '/class-smtp.php';
$phpmailer = new PHPMailer(); $phpmailer = new PHPMailer( true );
} }
// Headers // Headers
@ -400,7 +400,11 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
$to = explode( ',', $to ); $to = explode( ',', $to );
foreach ( (array) $to as $recipient ) { foreach ( (array) $to as $recipient ) {
$phpmailer->AddAddress( trim( $recipient ) ); try {
$phpmailer->AddAddress( trim( $recipient ) );
} catch ( phpmailerException $e ) {
continue;
}
} }
// Set mail's subject and body // Set mail's subject and body
@ -410,13 +414,21 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
// Add any CC and BCC recipients // Add any CC and BCC recipients
if ( !empty( $cc ) ) { if ( !empty( $cc ) ) {
foreach ( (array) $cc as $recipient ) { foreach ( (array) $cc as $recipient ) {
$phpmailer->AddCc( trim($recipient) ); try {
$phpmailer->AddCc( trim($recipient) );
} catch ( phpmailerException $e ) {
continue;
}
} }
} }
if ( !empty( $bcc ) ) { if ( !empty( $bcc ) ) {
foreach ( (array) $bcc as $recipient) { foreach ( (array) $bcc as $recipient) {
$phpmailer->AddBcc( trim($recipient) ); try {
$phpmailer->AddBcc( trim($recipient) );
} catch ( phpmailerException $e ) {
continue;
}
} }
} }
@ -455,16 +467,24 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
if ( !empty( $attachments ) ) { if ( !empty( $attachments ) ) {
foreach ( $attachments as $attachment ) { foreach ( $attachments as $attachment ) {
$phpmailer->AddAttachment($attachment); try {
$phpmailer->AddAttachment($attachment);
} catch ( phpmailerException $e ) {
continue;
}
} }
} }
do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) ); do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
// Send! // Send!
$result = @$phpmailer->Send(); try {
$phpmailer->Send();
} catch ( phpmailerException $e ) {
return false;
}
return $result; return true;
} }
endif; endif;