- Adds "reply to" option to all mailers

- Replaces WPMail with Swift using local transport (PHP mail)
- Fixes AmazonSES region naming convention
- Updates tests
This commit is contained in:
Vlad
2016-01-26 18:43:23 -05:00
parent 91981cc324
commit 0776e9ad73
19 changed files with 378 additions and 292 deletions

View File

@@ -27,63 +27,65 @@ class Mailer {
function buildMailer() { function buildMailer() {
switch($this->mailer['method']) { switch($this->mailer['method']) {
case 'AmazonSES': case 'AmazonSES':
$mailer_instance = new $this->mailer['class']( $mailer_instance = new $this->mailer['class'](
$this->mailer['region'], $this->mailer['region'],
$this->mailer['access_key'], $this->mailer['access_key'],
$this->mailer['secret_key'], $this->mailer['secret_key'],
$this->sender['from_name_email'] $this->sender,
); $this->reply_to
break; );
case 'ElasticEmail': break;
$mailer_instance = new $this->mailer['class']( case 'ElasticEmail':
$this->mailer['api_key'], $mailer_instance = new $this->mailer['class'](
$this->sender['from_email'], $this->mailer['api_key'],
$this->sender['from_name'] $this->sender,
); $this->reply_to
break; );
case 'MailGun': break;
$mailer_instance = new $this->mailer['class']( case 'MailGun':
$this->mailer['domain'], $mailer_instance = new $this->mailer['class'](
$this->mailer['api_key'], $this->mailer['domain'],
$this->sender['from_name_email'] $this->mailer['api_key'],
); $this->sender,
break; $this->reply_to
case 'MailPoet': );
$mailer_instance = new $this->mailer['class']( break;
$this->mailer['mailpoet_api_key'], case 'MailPoet':
$this->sender['from_email'], $mailer_instance = new $this->mailer['class'](
$this->sender['from_name'] $this->mailer['mailpoet_api_key'],
); $this->sender,
break; $this->reply_to
case 'SendGrid': );
$mailer_instance = new $this->mailer['class']( break;
$this->mailer['api_key'], case 'SendGrid':
$this->sender['from_email'], $mailer_instance = new $this->mailer['class'](
$this->sender['from_name'] $this->mailer['api_key'],
); $this->sender,
break; $this->reply_to
case 'WPMail': );
$mailer_instance = new $this->mailer['class']( break;
$this->sender['from_email'], case 'PHPMail':
$this->sender['from_name'] $mailer_instance = new $this->mailer['class'](
); $this->sender,
break; $this->reply_to
case 'SMTP': );
$mailer_instance = new $this->mailer['class']( break;
$this->mailer['host'], case 'SMTP':
$this->mailer['port'], $mailer_instance = new $this->mailer['class'](
$this->mailer['authentication'], $this->mailer['host'],
$this->mailer['login'], $this->mailer['port'],
$this->mailer['password'], $this->mailer['authentication'],
$this->mailer['encryption'], $this->mailer['login'],
$this->sender['from_email'], $this->mailer['password'],
$this->sender['from_name'] $this->mailer['encryption'],
); $this->sender,
break; $this->reply_to
default: );
throw new \Exception(__('Mailing method does not exist.')); break;
break; default:
throw new \Exception(__('Mailing method does not exist.'));
break;
} }
return $mailer_instance; return $mailer_instance;
} }
@@ -119,6 +121,9 @@ class Mailer {
); );
} }
} }
if(!$reply_to['address']) {
$reply_to['reply_to_email'] = $this->sender['from_email'];
}
return array( return array(
'reply_to_name' => $reply_to['name'], 'reply_to_name' => $reply_to['name'],
'reply_to_email' => $reply_to['address'], 'reply_to_email' => $reply_to['address'],

View File

@@ -13,21 +13,23 @@ class AmazonSES {
public $aws_termination_string; public $aws_termination_string;
public $hash_algorithm; public $hash_algorithm;
public $url; public $url;
public $from; public $sender;
public $reply_to;
public $date; public $date;
public $date_without_time; public $date_without_time;
function __construct($region, $access_key, $secret_key, $from) { function __construct($region, $access_key, $secret_key, $sender, $reply_to) {
$this->aws_access_key = $access_key; $this->aws_access_key = $access_key;
$this->aws_secret_key = $secret_key; $this->aws_secret_key = $secret_key;
$this->aws_region = $region; $this->aws_region = $region;
$this->aws_endpoint = sprintf('email.%s.amazonaws.com', $region); $this->aws_endpoint = sprintf('email.%s.amazonaws.com', $this->aws_region);
$this->aws_signing_algorithm = 'AWS4-HMAC-SHA256'; $this->aws_signing_algorithm = 'AWS4-HMAC-SHA256';
$this->aws_service = 'ses'; $this->aws_service = 'ses';
$this->aws_termination_string = 'aws4_request'; $this->aws_termination_string = 'aws4_request';
$this->hash_algorithm = 'sha256'; $this->hash_algorithm = 'sha256';
$this->url = 'https://' . $this->aws_endpoint; $this->url = 'https://' . $this->aws_endpoint;
$this->from = $from; $this->sender = $sender;
$this->reply_to = $reply_to;
$this->date = gmdate('Ymd\THis\Z'); $this->date = gmdate('Ymd\THis\Z');
$this->date_without_time = gmdate('Ymd'); $this->date_without_time = gmdate('Ymd');
} }
@@ -47,10 +49,11 @@ class AmazonSES {
$body = array( $body = array(
'Action' => 'SendEmail', 'Action' => 'SendEmail',
'Version' => '2010-12-01', 'Version' => '2010-12-01',
'Source' => $this->from,
'Destination.ToAddresses.member.1' => $subscriber, 'Destination.ToAddresses.member.1' => $subscriber,
'Source' => $this->sender['from_name_email'],
'ReplyToAddresses.member.1' => $this->reply_to['reply_to_name_email'],
'Message.Subject.Data' => $newsletter['subject'], 'Message.Subject.Data' => $newsletter['subject'],
'ReturnPath' => $this->from 'ReturnPath' => $this->sender['from_name_email'],
); );
if(!empty($newsletter['body']['html'])) { if(!empty($newsletter['body']['html'])) {
$body['Message.Body.Html.Data'] = $newsletter['body']['html']; $body['Message.Body.Html.Data'] = $newsletter['body']['html'];

View File

@@ -6,13 +6,13 @@ if(!defined('ABSPATH')) exit;
class ElasticEmail { class ElasticEmail {
public $url = 'https://api.elasticemail.com/mailer/send'; public $url = 'https://api.elasticemail.com/mailer/send';
public $api_key; public $api_key;
public $from_email; public $sender;
public $from_name; public $reply_to;
function __construct($api_key, $from_email, $from_name) { function __construct($api_key, $sender, $reply_to) {
$this->api_key = $api_key; $this->api_key = $api_key;
$this->from_email = $from_email; $this->sender = $sender;
$this->from_name = $from_name; $this->reply_to = $reply_to;
} }
function send($newsletter, $subscriber) { function send($newsletter, $subscriber) {
@@ -28,9 +28,11 @@ class ElasticEmail {
function getBody($newsletter, $subscriber) { function getBody($newsletter, $subscriber) {
$body = array( $body = array(
'api_key' => $this->api_key, 'api_key' => $this->api_key,
'from' => $this->from_email,
'from_name' => $this->from_name,
'to' => $subscriber, 'to' => $subscriber,
'from' => $this->sender['from_email'],
'from_name' => $this->sender['from_name'],
'reply_to' => $this->reply_to['reply_to_email'],
'reply_to_name' => $this->reply_to['reply_to_name'],
'subject' => $newsletter['subject'] 'subject' => $newsletter['subject']
); );
if(!empty($newsletter['body']['html'])) { if(!empty($newsletter['body']['html'])) {

View File

@@ -6,12 +6,14 @@ if(!defined('ABSPATH')) exit;
class MailGun { class MailGun {
public $url; public $url;
public $api_key; public $api_key;
public $from; public $sender;
public $reply_to;
function __construct($domain, $api_key, $from) { function __construct($domain, $api_key, $sender, $reply_to) {
$this->url = sprintf('https://api.mailgun.net/v3/%s/messages', $domain); $this->url = sprintf('https://api.mailgun.net/v3/%s/messages', $domain);
$this->api_key = $api_key; $this->api_key = $api_key;
$this->from = $from; $this->sender = $sender;
$this->reply_to = $reply_to;
} }
function send($newsletter, $subscriber) { function send($newsletter, $subscriber) {
@@ -27,8 +29,9 @@ class MailGun {
function getBody($newsletter, $subscriber) { function getBody($newsletter, $subscriber) {
$body = array( $body = array(
'from' => $this->from,
'to' => $subscriber, 'to' => $subscriber,
'from' => $this->sender['from_name_email'],
'h:Reply-To' => $this->reply_to['reply_to_name_email'],
'subject' => $newsletter['subject'] 'subject' => $newsletter['subject']
); );
if(!empty($newsletter['body']['html'])) { if(!empty($newsletter['body']['html'])) {

View File

@@ -6,13 +6,13 @@ if(!defined('ABSPATH')) exit;
class MailPoet { class MailPoet {
public $url = 'https://bridge.mailpoet.com/api/messages'; public $url = 'https://bridge.mailpoet.com/api/messages';
public $api_key; public $api_key;
public $from_email; public $sender;
public $from_name; public $reply_to;
function __construct($api_key, $from_email, $from_name) { function __construct($api_key, $sender, $reply_to) {
$this->api_key = $api_key; $this->api_key = $api_key;
$this->from_email = $from_email; $this->sender = $sender;
$this->from_name = $from_name; $this->reply_to = $reply_to;
} }
function send($newsletter, $subscriber) { function send($newsletter, $subscriber) {
@@ -46,8 +46,12 @@ class MailPoet {
'name' => $subscriber['name'] 'name' => $subscriber['name']
)), )),
'from' => (array( 'from' => (array(
'address' => $this->from_email, 'address' => $this->sender['from_email'],
'name' => $this->from_name 'name' => $this->sender['from_name']
)),
'reply_to' => (array(
'address' => $this->reply_to['reply_to_email'],
'name' => $this->reply_to['reply_to_name']
)), )),
'subject' => $newsletter['subject'] 'subject' => $newsletter['subject']
); );

View File

@@ -0,0 +1,64 @@
<?php
namespace MailPoet\Mailer\Methods;
if(!defined('ABSPATH')) exit;
class PHPMail {
public $sender;
public $reply_to;
public $mailer;
function __construct($sender, $reply_to) {
$this->sender = $sender;
$this->reply_to = $reply_to;
$this->mailer = $this->buildMailer();
}
function send($newsletter, $subscriber) {
try {
$message = $this->createMessage($newsletter, $subscriber);
$result = $this->mailer->send($message);
} catch(\Exception $e) {
$result = false;
}
return ($result === 1);
}
function buildMailer() {
$transport = \Swift_SmtpTransport::newInstance();
$transport->setTimeout(10);
return \Swift_Mailer::newInstance($transport);
}
function createMessage($newsletter, $subscriber) {
$message = \Swift_Message::newInstance()
->setTo($this->processSubscriber($subscriber))
->setFrom(array(
$this->sender['from_email'] => $this->sender['from_name']
))
->setReplyTo(array(
$this->reply_to['reply_to_email'] => $this->reply_to['reply_to_name']
))
->setSubject($newsletter['subject']);
if(!empty($newsletter['body']['html'])) {
$message = $message->setBody($newsletter['body']['html'], 'text/html');
}
if(!empty($newsletter['body']['text'])) {
$message = $message->addPart($newsletter['body']['text'], 'text/plain');
}
return $message;
}
function processSubscriber($subscriber) {
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriber_data);
if(!isset($subscriber_data['email'])) {
$subscriber_data = array(
'email' => $subscriber,
);
}
return array(
$subscriber_data['email'] =>
(isset($subscriber_data['name'])) ? $subscriber_data['name'] : ''
);
}
}

View File

@@ -10,21 +10,21 @@ class SMTP {
public $login; public $login;
public $password; public $password;
public $encryption; public $encryption;
public $from_name; public $sender;
public $from_email; public $reply_to;
public $mailer; public $mailer;
function __construct( function __construct(
$host, $port, $authentication, $login = null, $password = null, $encryption, $host, $port, $authentication, $login = null, $password = null, $encryption,
$from_email, $from_name) { $sender, $reply_to) {
$this->host = $host; $this->host = $host;
$this->port = $port; $this->port = $port;
$this->authentication = $authentication; $this->authentication = $authentication;
$this->login = $login; $this->login = $login;
$this->password = $password; $this->password = $password;
$this->encryption = $encryption; $this->encryption = $encryption;
$this->from_name = $from_name; $this->sender = $sender;
$this->from_email = $from_email; $this->reply_to = $reply_to;
$this->mailer = $this->buildMailer(); $this->mailer = $this->buildMailer();
} }
@@ -50,11 +50,15 @@ class SMTP {
return \Swift_Mailer::newInstance($transport); return \Swift_Mailer::newInstance($transport);
} }
function createMessage($newsletter, $subscriber) { function createMessage($newsletter, $subscriber) {
$message = \Swift_Message::newInstance() $message = \Swift_Message::newInstance()
->setFrom(array($this->from_email => $this->from_name))
->setTo($this->processSubscriber($subscriber)) ->setTo($this->processSubscriber($subscriber))
->setFrom(array(
$this->sender['from_email'] => $this->sender['from_name']
))
->setReplyTo(array(
$this->reply_to['reply_to_email'] => $this->reply_to['reply_to_name']
))
->setSubject($newsletter['subject']); ->setSubject($newsletter['subject']);
if(!empty($newsletter['body']['html'])) { if(!empty($newsletter['body']['html'])) {
$message = $message->setBody($newsletter['body']['html'], 'text/html'); $message = $message->setBody($newsletter['body']['html'], 'text/html');

View File

@@ -6,13 +6,13 @@ if(!defined('ABSPATH')) exit;
class SendGrid { class SendGrid {
public $url = 'https://api.sendgrid.com/api/mail.send.json'; public $url = 'https://api.sendgrid.com/api/mail.send.json';
public $api_key; public $api_key;
public $from_email; public $sender;
public $from_name; public $reply_to;
function __construct($api_key, $from_email, $from_name) { function __construct($api_key, $sender, $reply_to) {
$this->api_key = $api_key; $this->api_key = $api_key;
$this->from_email = $from_email; $this->sender = $sender;
$this->from_name = $from_name; $this->reply_to = $reply_to;
} }
function send($newsletter, $subscriber) { function send($newsletter, $subscriber) {
@@ -31,8 +31,9 @@ class SendGrid {
function getBody($newsletter, $subscriber) { function getBody($newsletter, $subscriber) {
$body = array( $body = array(
'to' => $subscriber, 'to' => $subscriber,
'from' => $this->from_email, 'from' => $this->sender['from_email'],
'from_name' => $this->from_name, 'fromname' => $this->sender['from_name'],
'replyto' => $this->reply_to['reply_to_email'],
'subject' => $newsletter['subject'] 'subject' => $newsletter['subject']
); );
if(!empty($newsletter['body']['html'])) { if(!empty($newsletter['body']['html'])) {

View File

@@ -1,63 +0,0 @@
<?php
namespace MailPoet\Mailer\Methods;
require_once(ABSPATH . 'wp-includes/pluggable.php');
if(!defined('ABSPATH')) exit;
class WPMail {
public $from_email;
public $from_name;
public $filters = array(
'wp_mail_from' => 'setFromEmail',
'wp_mail_from_name' => 'setFromName',
'wp_mail_content_type' => 'setContentType'
);
function __construct($from_email, $from_name) {
$this->from_email = $from_email;
$this->from_name = $from_name;
}
function addFilters() {
foreach($this->filters as $filter => $method) {
add_filter($filter, array(
$this,
$method
));
}
}
function removeFilters() {
foreach($this->filters as $filter => $method) {
remove_filter($filter, array(
$this,
$method
));
}
}
function setFromEmail() {
return $this->from_email;
}
function setFromName() {
return $this->from_name;
}
function setContentType() {
return 'text/html';
}
function send($newsletter, $subscriber) {
$this->addFilters();
$result = wp_mail(
$subscriber, $newsletter['subject'],
(!empty($newsletter['body']['html'])) ?
$newsletter['body']['html'] :
$newsletter['body']['text']
);
$this->removeFilters();
return ($result === true);
}
}

View File

@@ -13,9 +13,9 @@ class Hosts {
'secret_key' 'secret_key'
), ),
'regions' => array( 'regions' => array(
'US East (N. Virginia)' => 'us-east-1.amazonaws.com', 'US East (N. Virginia)' => 'us-east-1',
'US West (Oregon)' => 'us-west-2.amazonaws.com', 'US West (Oregon)' => 'us-west-2',
'EU (Ireland)' => 'eu-west-1.amazonaws.com' 'EU (Ireland)' => 'eu-west-1'
) )
), ),
'ElasticEmail' => array( 'ElasticEmail' => array(

View File

@@ -6,16 +6,27 @@ class AmazonSESCest {
function _before() { function _before() {
$this->settings = array( $this->settings = array(
'method' => 'AmazonSES', 'method' => 'AmazonSES',
'access_key' => 'AKIAJM6Y5HMGXBLDNSRA', 'access_key' => 'AKIAJDCYK7DHCRVF7LXA',
'secret_key' => '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', 'secret_key' => 'xVv9cKLf38d630YECGZMg7tb1kkN6GTG58WNBP9q',
'region' => 'us-east-1', 'region' => 'us-west-2',
);
$this->sender = array(
'from_name' => 'Sender',
'from_email' => 'staff@mailpoet.com',
'from_name_email' => 'Sender <staff@mailpoet.com>'
);
$this->reply_to = array(
'reply_to_name' => 'Reply To',
'reply_to_email' => 'reply-to@mailpoet.com',
'reply_to_name_email' => 'Reply To <reply-to@mailpoet.com>'
); );
$this->from = 'Sender <vlad@mailpoet.com>';
$this->mailer = new AmazonSES( $this->mailer = new AmazonSES(
$this->settings['region'], $this->settings['region'],
$this->settings['access_key'], $this->settings['access_key'],
$this->settings['secret_key'], $this->settings['secret_key'],
$this->from); $this->sender,
$this->reply_to
);
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>'; $this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array( $this->newsletter = array(
'subject' => 'testing AmazonSES', 'subject' => 'testing AmazonSES',
@@ -33,8 +44,8 @@ class AmazonSESCest {
); );
expect($this->mailer->url) expect($this->mailer->url)
->equals( ->equals(
sprintf('https://email.%s.amazonaws.com', $this->settings['region']) sprintf('https://email.%s.amazonaws.com', $this->settings['region'])
); );
expect(preg_match('!^\d{8}T\d{6}Z$!', $this->mailer->date))->equals(1); expect(preg_match('!^\d{8}T\d{6}Z$!', $this->mailer->date))->equals(1);
expect(preg_match('!^\d{8}$!', $this->mailer->date_without_time))->equals(1); expect(preg_match('!^\d{8}$!', $this->mailer->date_without_time))->equals(1);
} }
@@ -43,7 +54,9 @@ class AmazonSESCest {
$body = $this->mailer->getBody($this->newsletter, $this->subscriber); $body = $this->mailer->getBody($this->newsletter, $this->subscriber);
expect($body['Action'])->equals('SendEmail'); expect($body['Action'])->equals('SendEmail');
expect($body['Version'])->equals('2010-12-01'); expect($body['Version'])->equals('2010-12-01');
expect($body['Source'])->equals($this->from); expect($body['Source'])->equals($this->sender['from_name_email']);
expect($body['ReplyToAddresses.member.1'])
->equals($this->reply_to['reply_to_name_email']);
expect($body['Destination.ToAddresses.member.1']) expect($body['Destination.ToAddresses.member.1'])
->contains($this->subscriber); ->contains($this->subscriber);
expect($body['Message.Subject.Data']) expect($body['Message.Subject.Data'])
@@ -52,7 +65,7 @@ class AmazonSESCest {
->equals($this->newsletter['body']['html']); ->equals($this->newsletter['body']['html']);
expect($body['Message.Body.Text.Data']) expect($body['Message.Body.Text.Data'])
->equals($this->newsletter['body']['text']); ->equals($this->newsletter['body']['text']);
expect($body['ReturnPath'])->equals($this->from); expect($body['ReturnPath'])->equals($this->sender['from_name_email']);
} }
function itCanCreateRequest() { function itCanCreateRequest() {

View File

@@ -8,12 +8,20 @@ class ElasticEmailCest {
'method' => 'ElasticEmail', 'method' => 'ElasticEmail',
'api_key' => '997f1f7f-41de-4d7f-a8cb-86c8481370fa' 'api_key' => '997f1f7f-41de-4d7f-a8cb-86c8481370fa'
); );
$this->from_email = 'staff@mailpoet.com'; $this->sender = array(
$this->from_name = 'Sender'; 'from_name' => 'Sender',
'from_email' => 'staff@mailpoet.com',
'from_name_email' => 'Sender <staff@mailpoet.com>'
);
$this->reply_to = array(
'reply_to_name' => 'Reply To',
'reply_to_email' => 'reply-to@mailpoet.com',
'reply_to_name_email' => 'Reply To <reply-to@mailpoet.com>'
);
$this->mailer = new ElasticEmail( $this->mailer = new ElasticEmail(
$this->settings['api_key'], $this->settings['api_key'],
$this->from_email, $this->sender,
$this->from_name $this->reply_to
); );
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>'; $this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array( $this->newsletter = array(
@@ -28,8 +36,10 @@ class ElasticEmailCest {
function itCanGenerateBody() { function itCanGenerateBody() {
$body = $this->mailer->getBody($this->newsletter, $this->subscriber); $body = $this->mailer->getBody($this->newsletter, $this->subscriber);
expect($body['api_key'])->equals($this->settings['api_key']); expect($body['api_key'])->equals($this->settings['api_key']);
expect($body['from'])->equals($this->from_email); expect($body['from'])->equals($this->sender['from_email']);
expect($body['from_name'])->equals($this->from_name); expect($body['from_name'])->equals($this->sender['from_name']);
expect($body['reply_to'])->equals($this->reply_to['reply_to_email']);
expect($body['reply_to_name'])->equals($this->reply_to['reply_to_name']);
expect($body['to'])->contains($this->subscriber); expect($body['to'])->contains($this->subscriber);
expect($body['subject'])->equals($this->newsletter['subject']); expect($body['subject'])->equals($this->newsletter['subject']);
expect($body['body_html'])->equals($this->newsletter['body']['html']); expect($body['body_html'])->equals($this->newsletter['body']['html']);

View File

@@ -9,11 +9,21 @@ class MailGunCest {
'api_key' => 'key-6cf5g5qjzenk-7nodj44gdt8phe6vam2', 'api_key' => 'key-6cf5g5qjzenk-7nodj44gdt8phe6vam2',
'domain' => 'mrcasual.com' 'domain' => 'mrcasual.com'
); );
$this->from = 'Sender <staff@mailpoet.com>'; $this->sender = array(
'from_name' => 'Sender',
'from_email' => 'staff@mailpoet.com',
'from_name_email' => 'Sender <staff@mailpoet.com>'
);
$this->reply_to = array(
'reply_to_name' => 'Reply To',
'reply_to_email' => 'reply-to@mailpoet.com',
'reply_to_name_email' => 'Reply To <reply-to@mailpoet.com>'
);
$this->mailer = new MailGun( $this->mailer = new MailGun(
$this->settings['domain'], $this->settings['domain'],
$this->settings['api_key'], $this->settings['api_key'],
$this->from $this->sender,
$this->reply_to
); );
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>'; $this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array( $this->newsletter = array(
@@ -27,7 +37,8 @@ class MailGunCest {
function itCanGenerateBody() { function itCanGenerateBody() {
$body = $this->mailer->getBody($this->newsletter, $this->subscriber); $body = $this->mailer->getBody($this->newsletter, $this->subscriber);
expect($body['from'])->equals($this->from); expect($body['from'])->equals($this->sender['from_name_email']);
expect($body['h:Reply-To'])->equals($this->reply_to['reply_to_name_email']);
expect($body['to'])->equals($this->subscriber); expect($body['to'])->equals($this->subscriber);
expect($body['subject'])->equals($this->newsletter['subject']); expect($body['subject'])->equals($this->newsletter['subject']);
expect($body['html'])->equals($this->newsletter['body']['html']); expect($body['html'])->equals($this->newsletter['body']['html']);

View File

@@ -8,12 +8,20 @@ class MailPoetCest {
'method' => 'MailPoet', 'method' => 'MailPoet',
'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU' 'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU'
); );
$this->from_email = 'staff@mailpoet.com'; $this->sender = array(
$this->from_name = 'Sender'; 'from_name' => 'Sender',
'from_email' => 'staff@mailpoet.com',
'from_name_email' => 'Sender <staff@mailpoet.com>'
);
$this->reply_to = array(
'reply_to_name' => 'Reply To',
'reply_to_email' => 'reply-to@mailpoet.com',
'reply_to_name_email' => 'Reply To <reply-to@mailpoet.com>'
);
$this->mailer = new MailPoet( $this->mailer = new MailPoet(
$this->settings['api_key'], $this->settings['api_key'],
$this->from_email, $this->sender,
$this->from_name $this->reply_to
); );
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>'; $this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array( $this->newsletter = array(
@@ -30,7 +38,10 @@ class MailPoetCest {
$body = $this->mailer->getBody($this->newsletter, $subscriber); $body = $this->mailer->getBody($this->newsletter, $subscriber);
expect($body['to']['address'])->equals($subscriber['email']); expect($body['to']['address'])->equals($subscriber['email']);
expect($body['to']['name'])->equals($subscriber['name']); expect($body['to']['name'])->equals($subscriber['name']);
expect($body['from']['address'])->equals($this->from_email); expect($body['from']['address'])->equals($this->sender['from_email']);
expect($body['from']['name'])->equals($this->sender['from_name']);
expect($body['reply_to']['address'])->equals($this->reply_to['reply_to_email']);
expect($body['reply_to']['name'])->equals($this->reply_to['reply_to_name']);
expect($body['subject'])->equals($this->newsletter['subject']); expect($body['subject'])->equals($this->newsletter['subject']);
expect($body['html'])->equals($this->newsletter['body']['html']); expect($body['html'])->equals($this->newsletter['body']['html']);
expect($body['text'])->equals($this->newsletter['body']['text']); expect($body['text'])->equals($this->newsletter['body']['text']);

View File

@@ -0,0 +1,69 @@
<?php
use MailPoet\Mailer\Methods\PHPMail;
class WPMailCest {
function _before() {
$this->sender = array(
'from_name' => 'Sender',
'from_email' => 'staff@mailpoet.com',
'from_name_email' => 'Sender <staff@mailpoet.com>'
);
$this->reply_to = array(
'reply_to_name' => 'Reply To',
'reply_to_email' => 'reply-to@mailpoet.com',
'reply_to_name_email' => 'Reply To <reply-to@mailpoet.com>'
);
$this->mailer = new PHPMail(
$this->sender,
$this->reply_to
);
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array(
'subject' => 'testing local method (PHP mail)',
'body' => array(
'html' => 'HTML body',
'text' => 'TEXT body'
)
);
}
function itCanBuildMailer() {
$mailer = $this->mailer->buildMailer();
expect($mailer->getTransport()->getHost())
->equals('localhost');
}
function itCanCreateMessage() {
$message = $this->mailer->createMessage($this->newsletter, $this->subscriber);
expect($message->getTo())
->equals(array('mailpoet-phoenix-test@mailinator.com' => 'Recipient'));
expect($message->getFrom())
->equals(array($this->sender['from_email'] => $this->sender['from_name']));
expect($message->getReplyTo())
->equals(array($this->reply_to['reply_to_email'] => $this->reply_to['reply_to_name']));
expect($message->getSubject())
->equals($this->newsletter['subject']);
expect($message->getBody())
->equals($this->newsletter['body']['html']);
expect($message->getChildren()[0]->getContentType())
->equals('text/plain');
}
function itCanProcessSubscriber() {
expect($this->mailer->processSubscriber('test@test.com'))
->equals(array('test@test.com' => ''));
expect($this->mailer->processSubscriber('First <test@test.com>'))
->equals(array('test@test.com' => 'First'));
expect($this->mailer->processSubscriber('First Last <test@test.com>'))
->equals(array('test@test.com' => 'First Last'));
}
function itCanSend() {
$result = $this->mailer->send(
$this->newsletter,
$this->subscriber
);
expect($result)->true();
}
}

View File

@@ -13,8 +13,16 @@ class SMTPCest {
'authentication' => '1', 'authentication' => '1',
'encryption' => 'tls' 'encryption' => 'tls'
); );
$this->from_email = 'staff@mailpoet.com'; $this->sender = array(
$this->from_name = 'Sender'; 'from_name' => 'Sender',
'from_email' => 'staff@mailpoet.com',
'from_name_email' => 'Sender <staff@mailpoet.com>'
);
$this->reply_to = array(
'reply_to_name' => 'Reply To',
'reply_to_email' => 'reply-to@mailpoet.com',
'reply_to_name_email' => 'Reply To <reply-to@mailpoet.com>'
);
$this->mailer = new SMTP( $this->mailer = new SMTP(
$this->settings['host'], $this->settings['host'],
$this->settings['port'], $this->settings['port'],
@@ -22,8 +30,8 @@ class SMTPCest {
$this->settings['login'], $this->settings['login'],
$this->settings['password'], $this->settings['password'],
$this->settings['encryption'], $this->settings['encryption'],
$this->from_email, $this->sender,
$this->from_name $this->reply_to
); );
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>'; $this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array( $this->newsletter = array(
@@ -54,7 +62,9 @@ class SMTPCest {
expect($message->getTo()) expect($message->getTo())
->equals(array('mailpoet-phoenix-test@mailinator.com' => 'Recipient')); ->equals(array('mailpoet-phoenix-test@mailinator.com' => 'Recipient'));
expect($message->getFrom()) expect($message->getFrom())
->equals(array($this->from_email => $this->from_name)); ->equals(array($this->sender['from_email'] => $this->sender['from_name']));
expect($message->getReplyTo())
->equals(array($this->reply_to['reply_to_email'] => $this->reply_to['reply_to_name']));
expect($message->getSubject()) expect($message->getSubject())
->equals($this->newsletter['subject']); ->equals($this->newsletter['subject']);
expect($message->getBody()) expect($message->getBody())

View File

@@ -8,12 +8,20 @@ class SendGridCest {
'method' => 'SendGrid', 'method' => 'SendGrid',
'api_key' => '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' 'api_key' => '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'
); );
$this->from_email = 'staff@mailpoet.com'; $this->sender = array(
$this->from_name = 'Sender'; 'from_name' => 'Sender',
'from_email' => 'staff@mailpoet.com',
'from_name_email' => 'Sender <staff@mailpoet.com>'
);
$this->reply_to = array(
'reply_to_name' => 'Reply To',
'reply_to_email' => 'reply-to@mailpoet.com',
'reply_to_name_email' => 'Reply To <reply-to@mailpoet.com>'
);
$this->mailer = new SendGrid( $this->mailer = new SendGrid(
$this->settings['api_key'], $this->settings['api_key'],
$this->from_email, $this->sender,
$this->from_name $this->reply_to
); );
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>'; $this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array( $this->newsletter = array(
@@ -28,8 +36,9 @@ class SendGridCest {
function itCanGenerateBody() { function itCanGenerateBody() {
$body = $this->mailer->getBody($this->newsletter, $this->subscriber); $body = $this->mailer->getBody($this->newsletter, $this->subscriber);
expect($body['to'])->contains($this->subscriber); expect($body['to'])->contains($this->subscriber);
expect($body['from'])->equals($this->from_email); expect($body['from'])->equals($this->sender['from_email']);
expect($body['from_name'])->equals($this->from_name); expect($body['fromname'])->equals($this->sender['from_name']);
expect($body['replyto'])->equals($this->reply_to['reply_to_email']);
expect($body['subject'])->equals($this->newsletter['subject']); expect($body['subject'])->equals($this->newsletter['subject']);
expect($body['html'])->equals($this->newsletter['body']['html']); expect($body['html'])->equals($this->newsletter['body']['html']);
expect($body['text'])->equals($this->newsletter['body']['text']); expect($body['text'])->equals($this->newsletter['body']['text']);

View File

@@ -1,70 +0,0 @@
<?php
use MailPoet\Mailer\Methods\WPMail;
class WPMailCest {
function _before() {
$this->settings = array(
'method' => 'WPMail'
);
$this->from_email = 'staff@mailpoet.com';
$this->from_name = 'Sender';
$this->mailer = new WPMail(
$this->from_email,
$this->from_name
);
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array(
'subject' => 'testing SMTP',
'body' => array(
'html' => 'HTML body',
'text' => 'TEXT body'
)
);
}
function itCanAddFilters() {
$this->mailer->addFilters();
expect(has_filter('wp_mail_from_name', array(
$this->mailer,
'setFromName'
)))->notEmpty();
expect(has_filter('wp_mail_from', array(
$this->mailer,
'setFromEmail'
)))->notEmpty();
expect(has_filter('wp_mail_content_type', array(
$this->mailer,
'setContentType'
)))->notEmpty();
}
function itCanRemoveFilters() {
$this->mailer->addFilters();
$this->mailer->removeFilters();
expect(has_filter('wp_mail_from_name'))->false();
expect(has_filter('wp_mail_from'))->false();
expect(has_filter('wp_mail_content_type'))->false();
}
function itCanSetFromName() {
expect($this->mailer->setFromName())->equals($this->from_name);
}
function itCanSetFromEmail() {
expect($this->mailer->setFromEmail())->equals($this->from_email);
}
function itCanSetContentType() {
expect($this->mailer->setContentType())->equals('text/html');
}
function itCanSend() {
$_SERVER['SERVER_NAME'] = 'localhost';
$result = $this->mailer->send(
$this->newsletter,
$this->subscriber
);
//expect($result)->true();
}
}

View File

@@ -852,7 +852,7 @@
return 'MailPoet'; return 'MailPoet';
break; break;
case 'website': case 'website':
return 'WPMail'; return 'PHPMail';
break; break;
case 'smtp': case 'smtp':
var method = $('#mailpoet_smtp_provider').val(); var method = $('#mailpoet_smtp_provider').val();