- Adds MailGun mailer class + tests

- Updates tests for mailer classes
This commit is contained in:
MrCasual
2015-10-04 22:17:13 -04:00
parent 901544b875
commit ce1674da63
6 changed files with 313 additions and 109 deletions

View File

@@ -4,21 +4,21 @@ namespace MailPoet\Mailer;
if(!defined('ABSPATH')) exit;
class SendGrid {
function __construct($api_key, $from_email, $from_name, $newsletter, $subscribers) {
function __construct($api_key, $from_email, $from_name, $newsletter,
$subscribers) {
$this->url = 'https://api.sendgrid.com/api/mail.send.json';
$this->api_key = $api_key;
$this->newsletter = $newsletter;
$this->subscribers = $subscribers;
$this->from_name = $from_name;
$this->from_email = $from_email;
$this->from = sprintf('%s <%s>', $from_name, $from_email);
}
function send() {
if (!count($this->getSubscribers())) return false;
$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']) === false);
}
@@ -26,9 +26,13 @@ class SendGrid {
function getSubscribers() {
$subscribers = array_map(function ($subscriber) {
if(!isset($subscriber['email'])) return;
$first_name = (isset($subscriber['first_name'])) ? $subscriber['first_name'] : '';
$last_name = (isset($subscriber['last_name'])) ? $subscriber['last_name'] : '';
$subscriber = sprintf('%s %s <%s>', $first_name, $last_name, $subscriber['email']);
$first_name = (isset($subscriber['first_name']))
? $subscriber['first_name'] : '';
$last_name = (isset($subscriber['last_name']))
? $subscriber['last_name'] : '';
$subscriber = sprintf(
'%s %s <%s>', $first_name, $last_name, $subscriber['email']
);
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
return $subscriber;
}, $this->subscribers);
@@ -37,16 +41,17 @@ class SendGrid {
function getBody() {
$parameters = array(
'from' => sprintf('%s <%s>', $this->from_name, $this->from_email),
'to' => $this->from,
'from' => $this->from,
'x-smtpapi' => json_encode(array('to' => $this->getSubscribers())),
'to' => $this->from_email,
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']
);
$body = array_map(function ($parameter, $value) {
return $parameter . '=' . urlencode($value);
}, array_keys($parameters), $parameters);
return implode('&', array_filter($body));
return urldecode(http_build_query($parameters));
}
function auth() {
return 'Bearer ' . $this->api_key;
}
function request() {
@@ -55,8 +60,7 @@ class SendGrid {
'httpversion' => '1.1',
'method' => 'POST',
'headers' => array(
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/x-www-form-urlencoded'
'Authorization' => $this->auth()
),
'body' => $this->getBody()
);