- Adds wp_mail() mailer + tests

- Updates Mailer router + tests
- Updates mailer classes + tests
Closes #174
This commit is contained in:
MrCasual
2015-10-13 15:31:51 -04:00
parent 1d0e2755c1
commit a6c145476e
9 changed files with 190 additions and 22 deletions

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\Mailer\API;
namespace MailPoet\Mailer;
if(!defined('ABSPATH')) exit;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\Mailer\SMTP;
namespace MailPoet\Mailer;
if(!defined('ABSPATH')) exit;

58
lib/Mailer/WPMail.php Normal file
View File

@ -0,0 +1,58 @@
<?php
namespace MailPoet\Mailer;
require_once(ABSPATH . 'wp-includes/pluggable.php');
if(!defined('ABSPATH')) exit;
class WPMail {
function __construct($fromEmail, $fromName) {
$this->fromEmail = $fromEmail;
$this->fromName = $fromName;
add_filter('wp_mail_from', array(
$this,
'setFromEmail'
));
$this->filters = array(
'wp_mail_from' => 'setFromEmail',
'wp_mail_from_name' => 'setFromName',
'wp_mail_content_type' => 'setContentType'
);
}
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->fromEmail;
}
function setFromName() {
return $this->fromName;
}
function setContentType() {
return 'text/html';
}
function send($newsletter, $subscriber) {
$this->addFilters();
$result = wp_mail($subscriber, $newsletter['subject'], $newsletter['body']['html']);
$this->removeFilters();
return ($result === true);
}
}

View File

@ -20,25 +20,54 @@ class Mailer {
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);
$mailer = new $this->mailer['class'](
$this->mailer['region'],
$this->mailer['access_key'],
$this->mailer['secret_key'],
$this->from
);
break;
case 'ElasticEmail':
$mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName);
$mailer = new $this->mailer['class'](
$this->mailer['api_key'],
$this->fromEmail, $this->fromName
);
break;
case 'MailGun':
$mailer = new $this->mailer['class']($this->mailer['domain'], $this->mailer['api_key'], $this->from);
$mailer = new $this->mailer['class'](
$this->mailer['domain'],
$this->mailer['api_key'],
$this->from
);
break;
case 'MailPoet':
$mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName);
$mailer = new $this->mailer['class'](
$this->mailer['api_key'],
$this->fromEmail,
$this->fromName
);
break;
case 'Mandrill':
$mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName);
$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->fromEmail, $this->fromName);
$mailer = new $this->mailer['class'](
$this->mailer['api_key'],
$this->fromEmail,
$this->fromName
);
break;
case 'SMTP':
$mailer = new $this->mailer['class']($this->mailer['host'], $this->mailer['port'], $this->mailer['authentication'], $this->mailer['encryption'], $this->fromEmail, $this->fromName);
$mailer = new $this->mailer['class'](
$this->mailer['host'],
$this->mailer['port'],
$this->mailer['authentication'],
$this->mailer['encryption'],
$this->fromEmail,
$this->fromName);
break;
}
return $mailer;
@ -62,7 +91,7 @@ class Mailer {
'type' => 'API',
'access_key' => 'AKIAJM6Y5HMGXBLDNSRA',
'secret_key' => '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',
'region' => 'us-east-1',
'region' => 'us-east-1'
),
array(
'name' => 'ElasticEmail',
@ -77,7 +106,6 @@ class Mailer {
),
array(
'name' => 'MailPoet',
'type' => 'API',
'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU'
),
array(
@ -92,7 +120,6 @@ class Mailer {
),
array(
'name' => 'SMTP',
'type' => 'SMTP',
'host' => 'email-smtp.us-west-2.amazonaws.com',
'port' => 587,
'authentication' => array(
@ -100,10 +127,18 @@ class Mailer {
'password' => 'AudVHXHaYkvr54veCzqiqOxDiMMyfQW3/V6F1tYzGXY3'
),
'encryption' => 'tls'
),
array(
'name' => 'WPMail'
)
);
$mailer = $mailers[array_rand($mailers)];
return array_merge($mailer, array('class' => sprintf('MailPoet\\Mailer\\%s\\%s', $mailer['type'], $mailer['name'])));
$mailer['class'] = 'MailPoet\\Mailer\\' .
((isset($mailer['type'])) ?
$mailer['type'] . '\\' . $mailer['name'] :
$mailer['name']
);
return $mailer;
}
if($setting === 'from_name') return 'Sender';
if($setting === 'from_address') return 'staff@mailpoet.com';

View File

@ -28,8 +28,13 @@ class AmazonSESCest {
}
function itsConstructorWorks() {
expect($this->mailer->awsEndpoint)->equals('email.us-east-1.amazonaws.com');
expect($this->mailer->url)->equals('https://email.us-east-1.amazonaws.com');
expect($this->mailer->awsEndpoint)
->equals(
sprintf('email.%s.amazonaws.com', $this->settings['region'])
);
expect($this->mailer->url) ->equals(
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}$!', $this->mailer->dateWithoutTime))->equals(1);
}

View File

@ -1,12 +1,11 @@
<?php
use MailPoet\Mailer\API\MailPoet;
use MailPoet\Mailer\MailPoet;
class MailPoetCest {
function _before() {
$this->settings = array(
'name' => 'MailPoet',
'type' => 'API',
'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU'
);
$this->fromEmail = 'staff@mailpoet.com';

View File

@ -1,12 +1,11 @@
<?php
use MailPoet\Mailer\SMTP\SMTP;
use MailPoet\Mailer\SMTP;
class SMTPCest {
function _before() {
$this->settings = array(
'name' => 'SMTP',
'type' => 'SMTP',
'host' => 'email-smtp.us-west-2.amazonaws.com',
'port' => 587,
'authentication' => array(

View File

@ -0,0 +1,70 @@
<?php
use MailPoet\Mailer\WPMail;
class WPMailCest {
function _before() {
$this->settings = array(
'name' => 'WPMail'
);
$this->fromEmail = 'staff@mailpoet.com';
$this->fromName = 'Sender';
$this->mailer = new WPMail(
$this->fromEmail,
$this->fromName
);
$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->fromName);
}
function itCanSetFromEmail() {
expect($this->mailer->setFromName())->equals($this->fromName);
}
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

@ -43,15 +43,17 @@ class MailerCest {
function itCanConfigureMailer() {
$mailer = $this->router->buildMailer();
$class = 'Mailpoet\\Mailer\\' .
$this->router->mailer['type'] . '\\' .
$this->router->mailer['name'];
((isset($this->router->mailer['type'])) ?
$this->router->mailer['type'] . '\\' . $this->router->mailer['name'] :
$this->router->mailer['name']
);
expect($mailer instanceof $class)->true();
expect(method_exists($mailer, 'send'))->true();
}
function itCanSend() {
$newsletter = array(
'subject' => 'testing Mailer router with '.$this->router->mailer['name'],
'subject' => 'testing Mailer router with ' . $this->router->mailer['name'],
'body' => array(
'html' => 'HTML body',
'text' => 'TEXT body'