From 1a40f6b7aeb19ae9b55d286ef416d8b23f9dec93 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Fri, 30 Dec 2016 06:44:40 +0000 Subject: [PATCH] Mail: Ensure that any `phpmailerException` exceptions generated by `setFrom()` are caught to avoid PHP Fatal errors. This change avoids a PHP fatal error that can be encountered when the specified (or generated) source email is an invalid address, such as `wordpress@_`, it makes no effort to set a valid source, only avoid the fatal error. See #25239 for correcting the email address. Fixes #39360. Built from https://develop.svn.wordpress.org/trunk@39655 git-svn-id: http://core.svn.wordpress.org/trunk@39595 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/pluggable.php | 20 +++++++++++++++----- wp-includes/version.php | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php index 2d76e73e6c..aa9b1adf47 100644 --- a/wp-includes/pluggable.php +++ b/wp-includes/pluggable.php @@ -187,6 +187,10 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() $to = $atts['to']; } + if ( !is_array( $to ) ) { + $to = explode( ',', $to ); + } + if ( isset( $atts['subject'] ) ) { $subject = $atts['subject']; } @@ -349,17 +353,23 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() */ $from_name = apply_filters( 'wp_mail_from_name', $from_name ); - $phpmailer->setFrom( $from_email, $from_name, false ); + try { + $phpmailer->setFrom( $from_email, $from_name, false ); + } catch ( phpmailerException $e ) { + $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); + $mail_error_data['phpmailer_exception_code'] = $e->getCode(); - // Set destination addresses - if ( !is_array( $to ) ) - $to = explode( ',', $to ); + /** This filter is documented in wp-includes/pluggable.php */ + do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) ); + + return false; + } // Set mail's subject and body $phpmailer->Subject = $subject; $phpmailer->Body = $message; - // Use appropriate methods for handling addresses, rather than treating them as generic headers + // Set destination addresses, using appropriate methods for handling addresses $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' ); foreach ( $address_headers as $address_header => $addresses ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index e3314f2a7a..52e7b01086 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.8-alpha-39649'; +$wp_version = '4.8-alpha-39655'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.