- Adds Amazon SES tests

- Refactors all API mailers
- Updates unit tests for all API mailers
- Adds Mailer router + tests
This commit is contained in:
MrCasual
2015-10-06 20:47:34 -04:00
parent 1ab55f8991
commit 1d9ef9bd91
21 changed files with 897 additions and 804 deletions

View File

@ -0,0 +1,108 @@
<?php
namespace MailPoet\Mailer\API;
if(!defined('ABSPATH')) exit;
class AmazonSES {
function __construct($region, $access_key, $secret_key, $from) {
$this->awsAccessKey = $access_key;
$this->awsSecret_key = $secret_key;
$this->awsRegion = $region;
$this->awsEndpoint = sprintf('email.%s.amazonaws.com', $region);
$this->awsSigningAlgorithm = 'AWS4-HMAC-SHA256';
$this->awsService = 'ses';
$this->awsTerminationString = 'aws4_request';
$this->hashAlgorithm = 'sha256';
$this->url = 'https://' . $this->awsEndpoint;
$this->from = $from;
$this->date = gmdate('Ymd\THis\Z');
$this->dateWithoutTime = gmdate('Ymd');
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
$result = wp_remote_post(
$this->url,
$this->request()
);
return ($result['response']['code'] === 200);
}
function getBody() {
$parameters = 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'],
'ReplyToAddresses.member.1' => $this->from,
'ReturnPath' => $this->from
);
return urldecode(http_build_query($parameters));
}
function request() {
return array(
'timeout' => 10,
'httpversion' => '1.1',
'method' => 'POST',
'headers' => array(
'Host' => $this->awsEndpoint,
'Authorization' => $this->signRequest($this->getBody()),
'X-Amz-Date' => $this->date
),
'body' => $this->getBody()
);
}
function signRequest() {
$stringToSign = $this->createStringToSign(
$this->getCredentialScope(),
$this->getCanonicalRequest()
);
$signature = hash_hmac($this->hashAlgorithm, $stringToSign, $this->getSigningKey());
return sprintf(
'%s Credential=%s/%s, SignedHeaders=host;x-amz-date, Signature=%s',
$this->awsSigningAlgorithm,
$this->awsAccessKey,
$this->getCredentialScope(),
$signature);
}
function getCredentialScope() {
return sprintf('%s/%s/%s/%s', $this->dateWithoutTime, $this->awsRegion, $this->awsService, $this->awsTerminationString);
}
function getCanonicalRequest() {
return implode("\n", array(
'POST',
'/',
'',
'host:' . $this->awsEndpoint,
'x-amz-date:' . $this->date,
'',
'host;x-amz-date',
hash($this->hashAlgorithm, $this->getBody())
));
}
function createStringToSign($credentialScope, $canonicalRequest) {
return implode("\n", array(
$this->awsSigningAlgorithm,
$this->date,
$credentialScope,
hash($this->hashAlgorithm, $canonicalRequest)
));
}
function getSigningKey() {
$dateKey = hash_hmac($this->hashAlgorithm, $this->dateWithoutTime, 'AWS4' . $this->awsSecret_key, true);
$regionKey = hash_hmac($this->hashAlgorithm, $this->awsRegion, $dateKey, true);
$serviceKey = hash_hmac($this->hashAlgorithm, $this->awsService, $regionKey, true);
return hash_hmac($this->hashAlgorithm, $this->awsTerminationString, $serviceKey, true);
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace MailPoet\Mailer\API;
if(!defined('ABSPATH')) exit;
class ElasticEmail {
function __construct($api_key, $from_email, $from_name) {
$this->url = 'https://api.elasticemail.com/mailer/send';
$this->api_key = $api_key;
$this->from_name = $from_name;
$this->from_email = $from_email;
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
$result = wp_remote_post(
$this->url,
$this->request());
if(is_object($result) && get_class($result) === 'WP_Error') return false;
return (!preg_match('/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/', $result['body']) === false);
}
function getBody() {
$parameters = array(
'api_key' => $this->api_key,
'from' => $this->from_email,
'from_name' => $this->from_name,
'to' => $this->subscriber,
'subject' => $this->newsletter['subject'],
'body_html' => $this->newsletter['body']['html'],
'body_text' => $this->newsletter['body']['text']
);
return urldecode(http_build_query($parameters));
}
function request() {
return array(
'timeout' => 10,
'httpversion' => '1.0',
'method' => 'POST',
'body' => $this->getBody()
);
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace MailPoet\Mailer\API;
if(!defined('ABSPATH')) exit;
class MailGun {
function __construct($domain, $api_key, $from) {
$this->url = 'https://api.mailgun.net/v3';
$this->domain = $domain;
$this->api_key = $api_key;
$this->from = $from;
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
$result = wp_remote_post(
$this->url . '/' . $this->domain . '/messages',
$this->request()
);
if(is_object($result) && get_class($result) === 'WP_Error') return false;
return ($result['response']['code'] === 200);
}
function getBody() {
$parameters = array(
'from' => $this->from,
'to' => $this->subscriber,
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']['html'],
'text' => $this->newsletter['body']['text']
);
return urldecode(http_build_query($parameters));
}
function auth() {
return 'Basic ' . base64_encode('api:' . $this->api_key);
}
function request() {
return array(
'timeout' => 10,
'httpversion' => '1.0',
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => $this->auth()
),
'body' => $this->getBody()
);
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace MailPoet\Mailer\API;
if(!defined('ABSPATH')) exit;
class Mandrill {
function __construct($api_key, $from_email, $from_name) {
$this->url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$this->api_key = $api_key;
$this->from_name = $from_name;
$this->from_email = $from_email;
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $this->processSubscriber($subscriber);
$result = wp_remote_post(
$this->url,
$this->request()
);
if(is_object($result) && get_class($result) === 'WP_Error') return false;
return (!preg_match('!invalid!', $result['body']) === true && $result['response']['code'] === 200);
}
function processSubscriber($subscriber) {
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriberData);
if(!isset($subscriberData['email'])) {
$subscriberData = array(
'email' => $subscriber,
);
}
return array(
array(
'name' => (isset($subscriberData['name'])) ? $subscriberData['name'] : '',
'email' => $subscriberData['email']
)
);
}
function getBody() {
return array(
'key' => $this->api_key,
'message' => array(
'from_email' => $this->from_email,
'from_name' => $this->from_name,
'to' => $this->subscriber,
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']['html'],
'text' => $this->newsletter['body']['text'],
'headers' => array(
'Reply-To' => $this->from_email
)
),
'async' => false,
);
}
function request() {
return array(
'timeout' => 10,
'httpversion' => '1.0',
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json'
),
'body' => json_encode($this->getBody())
);
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace MailPoet\Mailer\API;
if(!defined('ABSPATH')) exit;
class SendGrid {
function __construct($api_key, $from) {
$this->url = 'https://api.sendgrid.com/api/mail.send.json';
$this->api_key = $api_key;
$this->from = $from;
}
function send($newsletter, $subscriber) {
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
$result = wp_remote_post(
$this->url,
$this->request()
);
if(is_object($result) && get_class($result) === 'WP_Error') return false;
$result = json_decode($result['body'], true);
return (!isset($result['errors']) === true);
}
function getBody() {
$parameters = array(
'to' => $this->subscriber,
'from' => $this->from,
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']['html'],
'text' => $this->newsletter['body']['text']
);
return urldecode(http_build_query($parameters));
}
function auth() {
return 'Bearer ' . $this->api_key;
}
function request() {
return array(
'timeout' => 10,
'httpversion' => '1.1',
'method' => 'POST',
'headers' => array(
'Authorization' => $this->auth()
),
'body' => $this->getBody()
);
}
}