From 8a278360f41d98b7357b1a64cedc37c8a9dc316f Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 8 Nov 2016 19:36:32 -0500 Subject: [PATCH] - Adds methods to format mailer send/connection succes & error results - Updates all mailing methods to return true on success and error message on failure --- lib/Mailer/Mailer.php | 22 ++++++++++++++++++++++ lib/Mailer/Methods/AmazonSES.php | 17 +++++++++++++---- lib/Mailer/Methods/MailPoet.php | 18 +++++++++++++----- lib/Mailer/Methods/PHPMail.php | 10 ++++++++-- lib/Mailer/Methods/SMTP.php | 10 ++++++++-- lib/Mailer/Methods/SendGrid.php | 20 +++++++++++++------- 6 files changed, 77 insertions(+), 20 deletions(-) diff --git a/lib/Mailer/Mailer.php b/lib/Mailer/Mailer.php index c898215a72..b132564abe 100644 --- a/lib/Mailer/Mailer.php +++ b/lib/Mailer/Mailer.php @@ -154,4 +154,26 @@ class Mailer { // bse64_encode non-ASCII string as per RFC 2047 (https://www.ietf.org/rfc/rfc2047.txt) return sprintf('=?utf-8?B?%s?=', base64_encode($name)); } + + static function formatMailerConnectionErrorResult($error_message) { + return array( + 'response' => false, + 'action'=> 'connect', + 'error' => $error_message + ); + } + + static function formatMailerSendErrorResult($error_message) { + return array( + 'response' => false, + 'action'=> 'send', + 'error' => $error_message + ); + } + + static function formatMailerSendSuccessResult() { + return array( + 'response' => true + ); + } } \ No newline at end of file diff --git a/lib/Mailer/Methods/AmazonSES.php b/lib/Mailer/Methods/AmazonSES.php index 72c38dd927..dfbc4e6a5d 100644 --- a/lib/Mailer/Methods/AmazonSES.php +++ b/lib/Mailer/Methods/AmazonSES.php @@ -1,6 +1,8 @@ url, $this->request($newsletter, $subscriber) ); - return ( - !is_wp_error($result) === true && - wp_remote_retrieve_response_code($result) === 200 - ); + if(is_wp_error($result)) { + return Mailer::formatMailerConnectionErrorResult($result->get_error_message()); + } + if(wp_remote_retrieve_response_code($result) !== 200) { + $response = simplexml_load_string(wp_remote_retrieve_body($result)); + $response = ($response) ? + $response->Error->Message->__toString() : + sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_AMAZONSES); + return Mailer::formatMailerSendErrorResult($response); + } + return Mailer::formatMailerSendSuccessResult(); } function getBody($newsletter, $subscriber) { diff --git a/lib/Mailer/Methods/MailPoet.php b/lib/Mailer/Methods/MailPoet.php index 43a4a2b345..228c82dcfe 100644 --- a/lib/Mailer/Methods/MailPoet.php +++ b/lib/Mailer/Methods/MailPoet.php @@ -1,6 +1,8 @@ url, $this->request($message_body) ); - return ( - !is_wp_error($result) === true && - wp_remote_retrieve_response_code($result) === 201 - ); + if(is_wp_error($result)) { + return Mailer::formatMailerConnectionErrorResult($result->get_error_message()); + } + if(wp_remote_retrieve_response_code($result) !== 201) { + $response = (wp_remote_retrieve_body($result)) ? + wp_remote_retrieve_body($result) : + wp_remote_retrieve_response_message($result); + return Mailer::formatMailerSendErrorResult($response); + } + return Mailer::formatMailerSendSuccessResult(); } function processSubscriber($subscriber) { @@ -41,7 +49,7 @@ class MailPoet { } function getBody($newsletter, $subscriber) { - $composeBody = function ($newsletter, $subscriber) { + $composeBody = function($newsletter, $subscriber) { $body = array( 'to' => (array( 'address' => $subscriber['email'], diff --git a/lib/Mailer/Methods/PHPMail.php b/lib/Mailer/Methods/PHPMail.php index 19df340a52..87be807ee6 100644 --- a/lib/Mailer/Methods/PHPMail.php +++ b/lib/Mailer/Methods/PHPMail.php @@ -1,6 +1,8 @@ createMessage($newsletter, $subscriber); $result = $this->mailer->send($message); } catch(\Exception $e) { - $result = false; + return Mailer::formatMailerSendErrorResult($e->getMessage()); } - return ($result === 1); + return ($result === 1) ? + Mailer::formatMailerSendSuccessResult() : + Mailer::formatMailerSendErrorResult( + sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_PHPMAIL) + ); } function buildMailer() { diff --git a/lib/Mailer/Methods/SMTP.php b/lib/Mailer/Methods/SMTP.php index 7ab318599d..8992f47175 100644 --- a/lib/Mailer/Methods/SMTP.php +++ b/lib/Mailer/Methods/SMTP.php @@ -1,6 +1,8 @@ createMessage($newsletter, $subscriber); $result = $this->mailer->send($message); } catch(\Exception $e) { - $result = false; + return Mailer::formatMailerSendErrorResult($e->getMessage()); } - return ($result === 1); + return ($result === 1) ? + Mailer::formatMailerSendSuccessResult() : + Mailer::formatMailerSendErrorResult( + sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_SMTP) + ); } function buildMailer() { diff --git a/lib/Mailer/Methods/SendGrid.php b/lib/Mailer/Methods/SendGrid.php index 9c990b2085..53eb6ccf30 100644 --- a/lib/Mailer/Methods/SendGrid.php +++ b/lib/Mailer/Methods/SendGrid.php @@ -1,6 +1,8 @@ url, $this->request($newsletter, $subscriber) ); - $result_body = json_decode($result['body'], true); - return ( - !is_wp_error($result) === true && - !preg_match('!invalid!', $result['body']) === true && - !isset($result_body['errors']) === true && - wp_remote_retrieve_response_code($result) === 200 - ); + if(is_wp_error($result)) { + return Mailer::formatMailerConnectionErrorResult($result->get_error_message()); + } + if(wp_remote_retrieve_response_code($result) !== 200) { + $response = json_decode($result['body'], true); + $response = (!empty($response['errors'])) ? + $response['errors'] : + sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_SENDGRID); + return Mailer::formatMailerSendErrorResult($response); + } + return Mailer::formatMailerSendSuccessResult(); } function getBody($newsletter, $subscriber) {