diff --git a/classes/SMTPMailingQueueOriginal.php b/classes/SMTPMailingQueueOriginal.php old mode 100755 new mode 100644 index f979958..9a7c8ca --- a/classes/SMTPMailingQueueOriginal.php +++ b/classes/SMTPMailingQueueOriginal.php @@ -16,10 +16,10 @@ class SMTPMailingQueueOriginal { * * The default content type is 'text/plain' which does not allow using HTML. * However, you can set the content type of the email by using the - * 'wp_mail_content_type' filter. + * {@see 'wp_mail_content_type'} filter. * * The default charset is based on the charset used on the blog. The charset can - * be set using the 'wp_mail_charset' filter. + * be set using the {@see 'wp_mail_charset'} filter. * * @since 1.2.1 * @@ -36,7 +36,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm // Compact the input, apply the filters, and extract them back out /** - * Filter the wp_mail() arguments. + * Filters the wp_mail() arguments. * * @since 2.2.0 * @@ -78,6 +78,8 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm } // Headers + $cc = $bcc = $reply_to = array(); + if ( empty( $headers ) ) { $headers = array(); } else { @@ -89,8 +91,6 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm $tempheaders = $headers; } $headers = array(); - $cc = array(); - $bcc = array(); // If it's actually got contents if ( !empty( $tempheaders ) ) { @@ -126,7 +126,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm $from_email = str_replace( '>', '', $from_email ); $from_email = trim( $from_email ); - // Avoid setting an empty $from_email. + // Avoid setting an empty $from_email. } elseif ( '' !== trim( $content ) ) { $from_email = trim( $content ); } @@ -142,7 +142,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm $charset = ''; } - // Avoid setting an empty $content_type. + // Avoid setting an empty $content_type. } elseif ( '' !== trim( $content ) ) { $content_type = trim( $content ); } @@ -153,6 +153,9 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm case 'bcc': $bcc = array_merge( (array) $bcc, explode( ',', $content ) ); break; + case 'reply-to': + $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) ); + break; default: // Add it to our grand headers array $headers[trim( $name )] = trim( $content ); @@ -191,78 +194,67 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm } /** - * Filter the email address to send from. + * Filters the email address to send from. * * @since 2.2.0 * * @param string $from_email Email address to send from. */ - $phpmailer->From = apply_filters( 'wp_mail_from', $from_email ); + $from_email = apply_filters( 'wp_mail_from', $from_email ); /** - * Filter the name to associate with the "from" email address. + * Filters the name to associate with the "from" email address. * * @since 2.3.0 * * @param string $from_name Name associated with the "from" email address. */ - $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name ); + $from_name = apply_filters( 'wp_mail_from_name', $from_name ); + + $phpmailer->setFrom( $from_email, $from_name, false ); // Set destination addresses if ( !is_array( $to ) ) $to = explode( ',', $to ); - foreach ( (array) $to as $recipient ) { - try { - // Break $recipient into name and address parts if in the format "Foo " - $recipient_name = ''; - if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) { - if ( count( $matches ) == 3 ) { - $recipient_name = $matches[1]; - $recipient = $matches[2]; - } - } - $phpmailer->AddAddress( $recipient, $recipient_name); - } catch ( phpmailerException $e ) { - continue; - } - } - // Set mail's subject and body $phpmailer->Subject = $subject; $phpmailer->Body = $message; - // Add any CC and BCC recipients - if ( !empty( $cc ) ) { - foreach ( (array) $cc as $recipient ) { - try { - // Break $recipient into name and address parts if in the format "Foo " - $recipient_name = ''; - if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) { - if ( count( $matches ) == 3 ) { - $recipient_name = $matches[1]; - $recipient = $matches[2]; - } - } - $phpmailer->AddCc( $recipient, $recipient_name ); - } catch ( phpmailerException $e ) { - continue; - } + // Use appropriate methods for handling addresses, rather than treating them as generic headers + $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' ); + + foreach ( $address_headers as $address_header => $addresses ) { + if ( empty( $addresses ) ) { + continue; } - } - if ( !empty( $bcc ) ) { - foreach ( (array) $bcc as $recipient) { + foreach ( (array) $addresses as $address ) { try { // Break $recipient into name and address parts if in the format "Foo " $recipient_name = ''; - if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) { + + if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) { if ( count( $matches ) == 3 ) { $recipient_name = $matches[1]; - $recipient = $matches[2]; + $address = $matches[2]; } } - $phpmailer->AddBcc( $recipient, $recipient_name ); + + switch ( $address_header ) { + case 'to': + $phpmailer->addAddress( $address, $recipient_name ); + break; + case 'cc': + $phpmailer->addCc( $address, $recipient_name ); + break; + case 'bcc': + $phpmailer->addBcc( $address, $recipient_name ); + break; + case 'reply_to': + $phpmailer->addReplyTo( $address, $recipient_name ); + break; + } } catch ( phpmailerException $e ) { continue; } @@ -278,7 +270,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm $content_type = 'text/plain'; /** - * Filter the wp_mail() content type. + * Filters the wp_mail() content type. * * @since 2.3.0 * @@ -299,7 +291,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm // Set the content-type and charset /** - * Filter the default wp_mail() charset. + * Filters the default wp_mail() charset. * * @since 2.3.0 * @@ -309,7 +301,7 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm // Set custom headers if ( !empty( $headers ) ) { - foreach( (array) $headers as $name => $content ) { + foreach ( (array) $headers as $name => $content ) { $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); } @@ -340,7 +332,21 @@ public static function wp_mail( $to, $subject, $message, $headers = '', $attachm try { return $phpmailer->Send(); } catch ( phpmailerException $e ) { + + $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); + $mail_error_data['phpmailer_exception_code'] = $e->getCode(); + + /** + * Fires after a phpmailerException is caught. + * + * @since 4.4.0 + * + * @param WP_Error $error A WP_Error object with the phpmailerException message, and an array + * containing the mail recipient, subject, message, headers, and attachments. + */ + do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) ); + return false; } } -} \ No newline at end of file +} diff --git a/classes/SMTPMailingQueueTools.php b/classes/SMTPMailingQueueTools.php index d178f10..d2ae6cb 100644 --- a/classes/SMTPMailingQueueTools.php +++ b/classes/SMTPMailingQueueTools.php @@ -204,7 +204,7 @@ public function sendTestMail() { $bcc = array_filter(array_map('trim', explode(',', $data['bcc']))); foreach ($bcc as $email) $data['headers'][] = 'Bcc:' . $email; - if($data['dont_wait']) + if(isset($data['dont_wait']) && $data['dont_wait']) $this->reallySendTestmail($data); else $this->writeTestmailToFile($data); @@ -346,4 +346,4 @@ protected function getTimezoneString() { // fallback to UTC return 'UTC'; } -} \ No newline at end of file +}