- Adds methods to format mailer send/connection succes & error results

- Updates all mailing methods to return true on success and error message
  on failure
This commit is contained in:
Vlad
2016-11-08 19:36:32 -05:00
parent 5d12de8ec0
commit 8a278360f4
6 changed files with 77 additions and 20 deletions

View File

@ -154,4 +154,26 @@ class Mailer {
// bse64_encode non-ASCII string as per RFC 2047 (https://www.ietf.org/rfc/rfc2047.txt) // 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)); 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
);
}
} }

View File

@ -1,6 +1,8 @@
<?php <?php
namespace MailPoet\Mailer\Methods; namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class AmazonSES { class AmazonSES {
@ -48,10 +50,17 @@ class AmazonSES {
$this->url, $this->url,
$this->request($newsletter, $subscriber) $this->request($newsletter, $subscriber)
); );
return ( if(is_wp_error($result)) {
!is_wp_error($result) === true && return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
wp_remote_retrieve_response_code($result) === 200 }
); 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) { function getBody($newsletter, $subscriber) {

View File

@ -1,6 +1,8 @@
<?php <?php
namespace MailPoet\Mailer\Methods; namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class MailPoet { class MailPoet {
@ -21,10 +23,16 @@ class MailPoet {
$this->url, $this->url,
$this->request($message_body) $this->request($message_body)
); );
return ( if(is_wp_error($result)) {
!is_wp_error($result) === true && return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
wp_remote_retrieve_response_code($result) === 201 }
); 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) { function processSubscriber($subscriber) {

View File

@ -1,6 +1,8 @@
<?php <?php
namespace MailPoet\Mailer\Methods; namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class PHPMail { class PHPMail {
@ -19,9 +21,13 @@ class PHPMail {
$message = $this->createMessage($newsletter, $subscriber); $message = $this->createMessage($newsletter, $subscriber);
$result = $this->mailer->send($message); $result = $this->mailer->send($message);
} catch(\Exception $e) { } 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() { function buildMailer() {

View File

@ -1,6 +1,8 @@
<?php <?php
namespace MailPoet\Mailer\Methods; namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class SMTP { class SMTP {
@ -33,9 +35,13 @@ class SMTP {
$message = $this->createMessage($newsletter, $subscriber); $message = $this->createMessage($newsletter, $subscriber);
$result = $this->mailer->send($message); $result = $this->mailer->send($message);
} catch(\Exception $e) { } 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() { function buildMailer() {

View File

@ -1,6 +1,8 @@
<?php <?php
namespace MailPoet\Mailer\Methods; namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class SendGrid { class SendGrid {
@ -20,13 +22,17 @@ class SendGrid {
$this->url, $this->url,
$this->request($newsletter, $subscriber) $this->request($newsletter, $subscriber)
); );
$result_body = json_decode($result['body'], true); if(is_wp_error($result)) {
return ( return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
!is_wp_error($result) === true && }
!preg_match('!invalid!', $result['body']) === true && if(wp_remote_retrieve_response_code($result) !== 200) {
!isset($result_body['errors']) === true && $response = json_decode($result['body'], true);
wp_remote_retrieve_response_code($result) === 200 $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) { function getBody($newsletter, $subscriber) {