- Adds wp_mail() mailer + tests
- Updates Mailer router + tests - Updates mailer classes + tests Closes #174
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Mailer\API;
|
namespace MailPoet\Mailer;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Mailer\SMTP;
|
namespace MailPoet\Mailer;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
58
lib/Mailer/WPMail.php
Normal file
58
lib/Mailer/WPMail.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -20,25 +20,54 @@ class Mailer {
|
|||||||
function buildMailer() {
|
function buildMailer() {
|
||||||
switch ($this->mailer['name']) {
|
switch ($this->mailer['name']) {
|
||||||
case 'AmazonSES':
|
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;
|
break;
|
||||||
case 'ElasticEmail':
|
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;
|
break;
|
||||||
case 'MailGun':
|
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;
|
break;
|
||||||
case 'MailPoet':
|
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;
|
break;
|
||||||
case 'Mandrill':
|
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;
|
break;
|
||||||
case 'SendGrid':
|
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;
|
break;
|
||||||
case 'SMTP':
|
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;
|
break;
|
||||||
}
|
}
|
||||||
return $mailer;
|
return $mailer;
|
||||||
@ -62,7 +91,7 @@ class Mailer {
|
|||||||
'type' => 'API',
|
'type' => 'API',
|
||||||
'access_key' => 'AKIAJM6Y5HMGXBLDNSRA',
|
'access_key' => 'AKIAJM6Y5HMGXBLDNSRA',
|
||||||
'secret_key' => 'P3EbTbVx7U0LXKQ9nTm2eIrP+9aPiLyvaRDsFxXh',
|
'secret_key' => 'P3EbTbVx7U0LXKQ9nTm2eIrP+9aPiLyvaRDsFxXh',
|
||||||
'region' => 'us-east-1',
|
'region' => 'us-east-1'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'name' => 'ElasticEmail',
|
'name' => 'ElasticEmail',
|
||||||
@ -77,7 +106,6 @@ class Mailer {
|
|||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'name' => 'MailPoet',
|
'name' => 'MailPoet',
|
||||||
'type' => 'API',
|
|
||||||
'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU'
|
'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
@ -92,7 +120,6 @@ class Mailer {
|
|||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'name' => 'SMTP',
|
'name' => 'SMTP',
|
||||||
'type' => 'SMTP',
|
|
||||||
'host' => 'email-smtp.us-west-2.amazonaws.com',
|
'host' => 'email-smtp.us-west-2.amazonaws.com',
|
||||||
'port' => 587,
|
'port' => 587,
|
||||||
'authentication' => array(
|
'authentication' => array(
|
||||||
@ -100,10 +127,18 @@ class Mailer {
|
|||||||
'password' => 'AudVHXHaYkvr54veCzqiqOxDiMMyfQW3/V6F1tYzGXY3'
|
'password' => 'AudVHXHaYkvr54veCzqiqOxDiMMyfQW3/V6F1tYzGXY3'
|
||||||
),
|
),
|
||||||
'encryption' => 'tls'
|
'encryption' => 'tls'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'name' => 'WPMail'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$mailer = $mailers[array_rand($mailers)];
|
$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_name') return 'Sender';
|
||||||
if($setting === 'from_address') return 'staff@mailpoet.com';
|
if($setting === 'from_address') return 'staff@mailpoet.com';
|
||||||
|
@ -28,8 +28,13 @@ class AmazonSESCest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function itsConstructorWorks() {
|
function itsConstructorWorks() {
|
||||||
expect($this->mailer->awsEndpoint)->equals('email.us-east-1.amazonaws.com');
|
expect($this->mailer->awsEndpoint)
|
||||||
expect($this->mailer->url)->equals('https://email.us-east-1.amazonaws.com');
|
->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}T\d{6}Z$!', $this->mailer->date))->equals(1);
|
||||||
expect(preg_match('!^\d{8}$!', $this->mailer->dateWithoutTime))->equals(1);
|
expect(preg_match('!^\d{8}$!', $this->mailer->dateWithoutTime))->equals(1);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use MailPoet\Mailer\API\MailPoet;
|
use MailPoet\Mailer\MailPoet;
|
||||||
|
|
||||||
class MailPoetCest {
|
class MailPoetCest {
|
||||||
function _before() {
|
function _before() {
|
||||||
$this->settings = array(
|
$this->settings = array(
|
||||||
'name' => 'MailPoet',
|
'name' => 'MailPoet',
|
||||||
'type' => 'API',
|
|
||||||
'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU'
|
'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU'
|
||||||
);
|
);
|
||||||
$this->fromEmail = 'staff@mailpoet.com';
|
$this->fromEmail = 'staff@mailpoet.com';
|
@ -1,12 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use MailPoet\Mailer\SMTP\SMTP;
|
use MailPoet\Mailer\SMTP;
|
||||||
|
|
||||||
class SMTPCest {
|
class SMTPCest {
|
||||||
function _before() {
|
function _before() {
|
||||||
$this->settings = array(
|
$this->settings = array(
|
||||||
'name' => 'SMTP',
|
'name' => 'SMTP',
|
||||||
'type' => 'SMTP',
|
|
||||||
'host' => 'email-smtp.us-west-2.amazonaws.com',
|
'host' => 'email-smtp.us-west-2.amazonaws.com',
|
||||||
'port' => 587,
|
'port' => 587,
|
||||||
'authentication' => array(
|
'authentication' => array(
|
70
tests/unit/Mailer/WPMailCest.php
Normal file
70
tests/unit/Mailer/WPMailCest.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -43,15 +43,17 @@ class MailerCest {
|
|||||||
function itCanConfigureMailer() {
|
function itCanConfigureMailer() {
|
||||||
$mailer = $this->router->buildMailer();
|
$mailer = $this->router->buildMailer();
|
||||||
$class = 'Mailpoet\\Mailer\\' .
|
$class = 'Mailpoet\\Mailer\\' .
|
||||||
$this->router->mailer['type'] . '\\' .
|
((isset($this->router->mailer['type'])) ?
|
||||||
$this->router->mailer['name'];
|
$this->router->mailer['type'] . '\\' . $this->router->mailer['name'] :
|
||||||
|
$this->router->mailer['name']
|
||||||
|
);
|
||||||
expect($mailer instanceof $class)->true();
|
expect($mailer instanceof $class)->true();
|
||||||
expect(method_exists($mailer, 'send'))->true();
|
expect(method_exists($mailer, 'send'))->true();
|
||||||
}
|
}
|
||||||
|
|
||||||
function itCanSend() {
|
function itCanSend() {
|
||||||
$newsletter = array(
|
$newsletter = array(
|
||||||
'subject' => 'testing Mailer router with '.$this->router->mailer['name'],
|
'subject' => 'testing Mailer router with ' . $this->router->mailer['name'],
|
||||||
'body' => array(
|
'body' => array(
|
||||||
'html' => 'HTML body',
|
'html' => 'HTML body',
|
||||||
'text' => 'TEXT body'
|
'text' => 'TEXT body'
|
||||||
|
Reference in New Issue
Block a user