- Renames Mailer router method responsible for building the mailer

- Updates tests
This commit is contained in:
MrCasual
2015-10-07 09:40:11 -04:00
parent 1d9ef9bd91
commit 14160f92f3
10 changed files with 32 additions and 30 deletions

View File

@ -38,7 +38,6 @@ class AmazonSES {
'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));

View File

@ -46,10 +46,7 @@ class Mandrill {
'to' => $this->subscriber,
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']['html'],
'text' => $this->newsletter['body']['text'],
'headers' => array(
'Reply-To' => $this->from_email
)
'text' => $this->newsletter['body']['text']
),
'async' => false,
);

View File

@ -4,10 +4,11 @@ namespace MailPoet\Mailer\API;
if(!defined('ABSPATH')) exit;
class SendGrid {
function __construct($api_key, $from) {
function __construct($api_key, $from_email, $from_name) {
$this->url = 'https://api.sendgrid.com/api/mail.send.json';
$this->api_key = $api_key;
$this->from = $from;
$this->fromEmail = $from_email;
$this->fromName = $from_name;
}
function send($newsletter, $subscriber) {
@ -25,7 +26,8 @@ class SendGrid {
function getBody() {
$parameters = array(
'to' => $this->subscriber,
'from' => $this->from,
'from' => $this->fromEmail,
'fromname' => $this->fromName,
'subject' => $this->newsletter['subject'],
'html' => $this->newsletter['body']['html'],
'text' => $this->newsletter['body']['text']

View File

@ -13,11 +13,11 @@ class Mailer {
function send($newsletter, $subscriber) {
$subscriber = $this->transformSubscriber($subscriber);
$mailer = $this->configureMailer();
$mailer = $this->buildMailer();
return wp_send_json($mailer->send($newsletter, $subscriber));
}
function configureMailer() {
function buildMailer() {
switch ($this->mailer['name']) {
case 'AmazonSES':
$mailer = new $this->mailer['class']($this->mailer['region'], $this->mailer['access_key'], $this->mailer['secret_key'], $this->from);
@ -32,7 +32,7 @@ class Mailer {
$mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName);
break;
case 'SendGrid':
$mailer = new $this->mailer['class']($this->mailer['api_key'], $this->from);
$mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName);
break;
}
return $mailer;
@ -42,6 +42,7 @@ class Mailer {
if(!is_array($subscriber)) return $subscriber;
$first_name = (isset($subscriber['first_name'])) ? $subscriber['first_name'] : '';
$last_name = (isset($subscriber['last_name'])) ? $subscriber['last_name'] : '';
if (!$first_name && !$last_name) return $subscriber['email'];
$subscriber = sprintf('%s %s <%s>', $first_name, $last_name, $subscriber['email']);
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
return $subscriber;
@ -83,7 +84,7 @@ class Mailer {
return array_merge($mailer, array('class' => sprintf('MailPoet\\Mailer\\%s\\%s', $mailer['type'], $mailer['name'])));
}
if($setting === 'from_name') return 'Sender';
if($setting === 'from_address') return 'mailpoet-test1@mailinator.com';
if($setting === 'from_address') return 'mailpoet-phoenix-test@mailinator.com';
return Setting::where('name', $setting)
->findOne()->value;
}

View File

@ -13,7 +13,7 @@ class AmazonSESCest {
);
$this->from = 'Sender <vlad@mailpoet.com>';
$this->mailer = new AmazonSES($this->settings['region'], $this->settings['access_key'], $this->settings['secret_key'], $this->from);
$this->mailer->subscriber = 'Recipient <mailpoet-test1@mailinator.com>';
$this->mailer->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->mailer->newsletter = array(
'subject' => 'testing AmazonSES',
'body' => array(
@ -47,8 +47,6 @@ class AmazonSESCest {
expect($body[6])
->equals('Message.Body.Text.Data=' . $this->mailer->newsletter['body']['text']);
expect($body[7])
->equals('ReplyToAddresses.member.1=' . $this->from);
expect($body[8])
->equals('ReturnPath=' . $this->from);
}

View File

@ -12,7 +12,7 @@ class ElasticEmailCest {
$this->fromEmail = 'do-not-reply@mailpoet.com';
$this->fromName = 'Sender';
$this->mailer = new ElasticEmail($this->settings['api_key'], $this->fromEmail, $this->fromName);
$this->mailer->subscriber = 'Recipient <mailpoet-test1@mailinator.com>';
$this->mailer->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->mailer->newsletter = array(
'subject' => 'testing ElasticEmail',
'body' => array(

View File

@ -12,7 +12,7 @@ class MailGunCest {
);
$this->from = 'Sender <do-not-reply@mailpoet.com>';
$this->mailer = new MailGun($this->settings['domain'], $this->settings['api_key'], $this->from);
$this->mailer->subscriber = 'Recipient <mailpoet-test1@mailinator.com>';
$this->mailer->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->mailer->newsletter = array(
'subject' => 'testing MailGun',
'body' => array(

View File

@ -12,7 +12,7 @@ class MandrillCest {
$this->fromEmail = 'do-not-reply@mailpoet.com';
$this->fromName = 'Sender';
$this->mailer = new Mandrill($this->settings['api_key'], $this->fromEmail, $this->fromName);
$this->mailer->subscriber = 'Recipient <mailpoet-test1@mailinator.com>';
$this->mailer->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->mailer->newsletter = array(
'subject' => 'testing Mandrill',
'body' => array(
@ -43,9 +43,6 @@ class MandrillCest {
expect($body['message']['text'])->equals(
$this->mailer->newsletter['body']['text']
);
expect($body['message']['headers']['Reply-To'])->equals(
$this->fromEmail
);
expect($body['async'])->false();
}

View File

@ -9,9 +9,10 @@ class SendGridCest {
'type' => 'API',
'api_key' => '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'
);
$this->from = 'Sender <do-not-reply@mailpoet.com>';
$this->mailer = new SendGrid($this->settings['api_key'], $this->from);
$this->mailer->subscriber = 'Recipient <mailpoet-test1@mailinator.com>';
$this->fromEmail = 'do-not-reply@mailpoet.com';
$this->fromName = 'Sender';
$this->mailer = new SendGrid($this->settings['api_key'], $this->fromEmail, $this->fromName);
$this->mailer->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->mailer->newsletter = array(
'subject' => 'testing SendGrid',
'body' => array(
@ -26,12 +27,14 @@ class SendGridCest {
expect($body[0])
->contains('to=' . $this->mailer->subscriber);
expect($body[1])
->equals('from=' . $this->from);
->equals('from=' . $this->fromEmail);
expect($body[2])
->equals('subject=' . $this->mailer->newsletter['subject']);
->equals('fromname=' . $this->fromName);
expect($body[3])
->equals('html=' . $this->mailer->newsletter['body']['html']);
->equals('subject=' . $this->mailer->newsletter['subject']);
expect($body[4])
->equals('html=' . $this->mailer->newsletter['body']['html']);
expect($body[5])
->equals('text=' . $this->mailer->newsletter['body']['text']);
}

View File

@ -8,12 +8,17 @@ class MailerCest {
}
function itCanConstruct() {
expect($this->router->from)->equals('Sender <mailpoet-test1@mailinator.com>');
expect($this->router->from)->equals('Sender <mailpoet-phoenix-test@mailinator.com>');
}
function itCanTransformSubscriber() {
expect($this->router->transformSubscriber('test@email.com'))
->equals('test@email.com');
expect($this->router->transformSubscriber(
array(
'email' => 'test@email.com'
))
)->equals('test@email.com');
expect($this->router->transformSubscriber(
array(
'first_name' => 'First',
@ -36,7 +41,7 @@ class MailerCest {
}
function itCanConfigureMailer() {
$mailer = $this->router->configureMailer();
$mailer = $this->router->buildMailer();
$class = 'Mailpoet\\Mailer\\' .
$this->router->mailer['type'] . '\\' .
$this->router->mailer['name'];
@ -55,7 +60,7 @@ class MailerCest {
$subscriber = array(
'first_name' => 'First',
'last_name' => 'Last',
'email' => 'mailpoet-test1@mailinator.com'
'email' => 'mailpoet-phoenix-test@mailinator.com'
);
expect($this->router->send($newsletter, $subscriber))->true();
}