- Refactors mailer classes and tests

- Updates Newsletter router to send to multiple recipients
This commit is contained in:
MrCasual
2015-10-08 09:33:38 -04:00
parent 864c352384
commit e370e4b4c8
13 changed files with 212 additions and 277 deletions

View File

@ -20,11 +20,9 @@ class AmazonSES {
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
$result = wp_remote_post(
$this->url,
$this->request()
$this->request($newsletter, $subscriber)
);
return (
!is_wp_error($result) === true &&
@ -32,37 +30,38 @@ class AmazonSES {
);
}
function getBody() {
function getBody($newsletter, $subscriber) {
return array(
'Action' => 'SendEmail',
'Version' => '2010-12-01',
'Source' => $this->from,
'Destination.ToAddresses.member.1' => $this->subscriber,
'Message.Subject.Data' => $this->newsletter['subject'],
'Message.Body.Html.Data' => $this->newsletter['body']['html'],
'Message.Body.Text.Data' => $this->newsletter['body']['text'],
'Destination.ToAddresses.member.1' => $subscriber,
'Message.Subject.Data' => $newsletter['subject'],
'Message.Body.Html.Data' => $newsletter['body']['html'],
'Message.Body.Text.Data' => $newsletter['body']['text'],
'ReturnPath' => $this->from
);
}
function request() {
function request($newsletter, $subscriber) {
$body = $this->getBody($newsletter, $subscriber);
return array(
'timeout' => 10,
'httpversion' => '1.1',
'method' => 'POST',
'headers' => array(
'Host' => $this->awsEndpoint,
'Authorization' => $this->signRequest($this->getBody()),
'Authorization' => $this->signRequest($body),
'X-Amz-Date' => $this->date
),
'body' => urldecode(http_build_query($this->getBody()))
'body' => urldecode(http_build_query($body))
);
}
function signRequest() {
function signRequest($body) {
$stringToSign = $this->createStringToSign(
$this->getCredentialScope(),
$this->getCanonicalRequest()
$this->getCanonicalRequest($body)
);
$signature = hash_hmac($this->hashAlgorithm, $stringToSign, $this->getSigningKey());
@ -78,7 +77,7 @@ class AmazonSES {
return sprintf('%s/%s/%s/%s', $this->dateWithoutTime, $this->awsRegion, $this->awsService, $this->awsTerminationString);
}
function getCanonicalRequest() {
function getCanonicalRequest($body) {
return implode("\n", array(
'POST',
'/',
@ -87,7 +86,7 @@ class AmazonSES {
'x-amz-date:' . $this->date,
'',
'host;x-amz-date',
hash($this->hashAlgorithm, urldecode(http_build_query($this->getBody())))
hash($this->hashAlgorithm, urldecode(http_build_query($body)))
));
}

View File

@ -12,35 +12,34 @@ class ElasticEmail {
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
$result = wp_remote_post(
$this->url,
$this->request());
$this->request($newsletter, $subscriber));
return (
!is_wp_error($result) === true &&
!preg_match('/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/', $result['body']) === false
);
}
function getBody() {
function getBody($newsletter, $subscriber) {
return array(
'api_key' => $this->apiKey,
'from' => $this->fromEmail,
'from_name' => $this->fromName,
'to' => $this->subscriber,
'subject' => $this->newsletter['subject'],
'body_html' => $this->newsletter['body']['html'],
'body_text' => $this->newsletter['body']['text']
'to' => $subscriber,
'subject' => $newsletter['subject'],
'body_html' => $newsletter['body']['html'],
'body_text' => $newsletter['body']['text']
);
}
function request() {
function request($newsletter, $subscriber) {
$body = $this->getBody($newsletter, $subscriber);
return array(
'timeout' => 10,
'httpversion' => '1.0',
'method' => 'POST',
'body' => urldecode(http_build_query($this->getBody()))
'body' => urldecode(http_build_query($body))
);
}
}

View File

@ -11,11 +11,9 @@ class MailGun {
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
$result = wp_remote_post(
$this->url,
$this->request()
$this->request($newsletter, $subscriber)
);
return (
!is_wp_error($result) === true &&
@ -23,13 +21,13 @@ class MailGun {
);
}
function getBody() {
function getBody($newsletter, $subscriber) {
return array(
'from' => $this->from,
'to' => $this->subscriber,
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']['html'],
'text' => $this->newsletter['body']['text']
'to' => $subscriber,
'subject' => $newsletter['subject'],
'html' => $newsletter['body']['html'],
'text' => $newsletter['body']['text']
);
}
@ -37,7 +35,8 @@ class MailGun {
return 'Basic ' . base64_encode('api:' . $this->apiKey);
}
function request() {
function request($newsletter, $subscriber) {
$body = $this->getBody($newsletter, $subscriber);
return array(
'timeout' => 10,
'httpversion' => '1.0',
@ -46,7 +45,7 @@ class MailGun {
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => $this->auth()
),
'body' => urldecode(http_build_query($this->getBody()))
'body' => urldecode(http_build_query($body))
);
}
}

View File

@ -12,11 +12,9 @@ class MailPoet {
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $this->processSubscriber($subscriber);
$result = wp_remote_post(
$this->url,
$this->request()
$this->request($newsletter, $this->processSubscriber($subscriber))
);
return (
!is_wp_error($result) === true &&
@ -37,19 +35,19 @@ class MailPoet {
);
}
function getBody() {
function getBody($newsletter, $subscriber) {
return array(
'to' => (array(
'address' => $this->subscriber['email'],
'name' => $this->subscriber['name']
'address' => $subscriber['email'],
'name' => $subscriber['name']
)),
'from' => (array(
'address' => $this->fromEmail,
'name' => $this->fromName
)),
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']['html'],
'text' => $this->newsletter['body']['text']
'subject' => $newsletter['subject'],
'html' => $newsletter['body']['html'],
'text' => $newsletter['body']['text']
);
}
@ -57,8 +55,9 @@ class MailPoet {
return 'Basic ' . base64_encode('api:' . $this->apiKey);
}
function request() {
$request = array(
function request($newsletter, $subscriber) {
$body = array($this->getBody($newsletter, $subscriber));
return array(
'timeout' => 10,
'httpversion' => '1.0',
'method' => 'POST',
@ -66,8 +65,7 @@ class MailPoet {
'Content-Type' => 'application/json',
'Authorization' => $this->auth()
),
'body' => json_encode(array($this->getBody()))
'body' => json_encode($body)
);
return $request;
}
}

View File

@ -10,13 +10,11 @@ class Mandrill {
$this->fromName = $fromName;
$this->fromEmail = $fromEmail;
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $this->processSubscriber($subscriber);
$result = wp_remote_post(
$this->url,
$this->request()
$this->request($newsletter, $this->processSubscriber($subscriber))
);
return (
!is_wp_error($result) === true &&
@ -24,7 +22,7 @@ class Mandrill {
wp_remote_retrieve_response_code($result) === 200
);
}
function processSubscriber($subscriber) {
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriberData);
if(!isset($subscriberData['email'])) {
@ -33,27 +31,28 @@ class Mandrill {
);
}
return array(
'email' => $subscriberData['email'],
'name' => (isset($subscriberData['name'])) ? $subscriberData['name'] : ''
'email' => $subscriberData['email'],
'name' => (isset($subscriberData['name'])) ? $subscriberData['name'] : ''
);
}
function getBody() {
function getBody($newsletter, $subscriber) {
return array(
'key' => $this->apiKey,
'message' => array(
'from_email' => $this->fromEmail,
'from_name' => $this->fromName,
'to' => array($this->subscriber),
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']['html'],
'text' => $this->newsletter['body']['text']
'to' => array($subscriber),
'subject' => $newsletter['subject'],
'html' => $newsletter['body']['html'],
'text' => $newsletter['body']['text']
),
'async' => false,
);
}
function request() {
function request($newsletter, $subscriber) {
$body = $this->getBody($newsletter, $subscriber);
return array(
'timeout' => 10,
'httpversion' => '1.0',
@ -61,7 +60,7 @@ class Mandrill {
'headers' => array(
'Content-Type' => 'application/json'
),
'body' => json_encode($this->getBody())
'body' => json_encode($body)
);
}
}

View File

@ -12,11 +12,9 @@ class SendGrid {
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
$result = wp_remote_post(
$this->url,
$this->request()
$this->request($newsletter, $subscriber)
);
return (
!is_wp_error($result) === true &&
@ -26,14 +24,14 @@ class SendGrid {
);
}
function getBody() {
function getBody($newsletter, $subscriber) {
return array(
'to' => $this->subscriber,
'to' => $subscriber,
'from' => $this->fromEmail,
'fromname' => $this->fromName,
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']['html'],
'text' => $this->newsletter['body']['text']
'subject' => $newsletter['subject'],
'html' => $newsletter['body']['html'],
'text' => $newsletter['body']['text']
);
}
@ -41,7 +39,8 @@ class SendGrid {
return 'Bearer ' . $this->apiKey;
}
function request() {
function request($newsletter, $subscriber) {
$body = $this->getBody($newsletter, $subscriber);
return array(
'timeout' => 10,
'httpversion' => '1.1',
@ -49,7 +48,7 @@ class SendGrid {
'headers' => array(
'Authorization' => $this->auth()
),
'body' => urldecode(http_build_query($this->getBody()))
'body' => urldecode(http_build_query($body))
);
}
}