- 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)
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
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit;
class AmazonSES {
@ -48,10 +50,17 @@ class AmazonSES {
$this->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) {

View File

@ -1,6 +1,8 @@
<?php
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit;
class MailPoet {
@ -21,10 +23,16 @@ class MailPoet {
$this->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) {

View File

@ -1,6 +1,8 @@
<?php
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit;
class PHPMail {
@ -19,9 +21,13 @@ class PHPMail {
$message = $this->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() {

View File

@ -1,6 +1,8 @@
<?php
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit;
class SMTP {
@ -33,9 +35,13 @@ class SMTP {
$message = $this->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() {

View File

@ -1,6 +1,8 @@
<?php
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
if(!defined('ABSPATH')) exit;
class SendGrid {
@ -20,13 +22,17 @@ class SendGrid {
$this->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) {