Replaces SwiftMailer's mail() method with WP's PHPMailer
This commit is contained in:
@ -1,10 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace MailPoet\Mailer\Methods;
|
namespace MailPoet\Mailer\Methods;
|
||||||
|
|
||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
require_once ABSPATH . WPINC . '/class-phpmailer.php';
|
||||||
|
|
||||||
class PHPMail {
|
class PHPMail {
|
||||||
public $sender;
|
public $sender;
|
||||||
public $reply_to;
|
public $reply_to;
|
||||||
@ -22,12 +25,12 @@ class PHPMail {
|
|||||||
|
|
||||||
function send($newsletter, $subscriber, $extra_params = array()) {
|
function send($newsletter, $subscriber, $extra_params = array()) {
|
||||||
try {
|
try {
|
||||||
$message = $this->createMessage($newsletter, $subscriber, $extra_params);
|
$mailer = $this->configureMailerWithMessage($newsletter, $subscriber, $extra_params);
|
||||||
$result = $this->mailer->send($message);
|
$result = $mailer->send();
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
return Mailer::formatMailerSendErrorResult($e->getMessage());
|
return Mailer::formatMailerSendErrorResult($e->getMessage());
|
||||||
}
|
}
|
||||||
if($result === 1) {
|
if($result === true) {
|
||||||
return Mailer::formatMailerSendSuccessResult();
|
return Mailer::formatMailerSendSuccessResult();
|
||||||
} else {
|
} else {
|
||||||
$result = sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_PHPMAIL);
|
$result = sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_PHPMAIL);
|
||||||
@ -39,33 +42,30 @@ class PHPMail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function buildMailer() {
|
function buildMailer() {
|
||||||
$transport = \Swift_MailTransport::newInstance();
|
$mailer = new \PHPMailer(true);
|
||||||
return \Swift_Mailer::newInstance($transport);
|
// send using PHP's mail() function
|
||||||
|
$mailer->isMail();
|
||||||
|
return $mailer;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createMessage($newsletter, $subscriber, $extra_params = array()) {
|
function configureMailerWithMessage($newsletter, $subscriber, $extra_params = array()) {
|
||||||
$message = \Swift_Message::newInstance()
|
$mailer = $this->mailer;
|
||||||
->setTo($this->processSubscriber($subscriber))
|
$mailer->clearAddresses();
|
||||||
->setFrom(array(
|
$mailer->clearCustomHeaders();
|
||||||
$this->sender['from_email'] => $this->sender['from_name']
|
$mailer->isHTML();
|
||||||
))
|
$mailer->CharSet = 'UTF-8';
|
||||||
->setSender($this->sender['from_email'])
|
$mailer->setFrom($this->sender['from_email'], $this->sender['from_name'], false);
|
||||||
->setReplyTo(array(
|
$mailer->addReplyTo($this->reply_to['reply_to_email'], $this->reply_to['reply_to_name']);
|
||||||
$this->reply_to['reply_to_email'] => $this->reply_to['reply_to_name']
|
$subscriber = $this->processSubscriber($subscriber);
|
||||||
))
|
$mailer->addAddress($subscriber['email'], $subscriber['name']);
|
||||||
->setReturnPath($this->return_path)
|
$mailer->Subject = (!empty($newsletter['subject'])) ? $newsletter['subject'] : '';
|
||||||
->setSubject($newsletter['subject']);
|
$mailer->Body = (!empty($newsletter['body']['html'])) ? $newsletter['body']['html'] : '';
|
||||||
|
$mailer->AltBody = (!empty($newsletter['body']['text'])) ? $newsletter['body']['text'] : '';
|
||||||
|
$mailer->Sender = $this->return_path;
|
||||||
if(!empty($extra_params['unsubscribe_url'])) {
|
if(!empty($extra_params['unsubscribe_url'])) {
|
||||||
$headers = $message->getHeaders();
|
$this->mailer->addCustomHeader('List-Unsubscribe', $extra_params['unsubscribe_url']);
|
||||||
$headers->addTextHeader('List-Unsubscribe', '<' . $extra_params['unsubscribe_url'] . '>');
|
|
||||||
}
|
}
|
||||||
if(!empty($newsletter['body']['html'])) {
|
return $mailer;
|
||||||
$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) {
|
function processSubscriber($subscriber) {
|
||||||
@ -76,8 +76,8 @@ class PHPMail {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
return array(
|
return array(
|
||||||
$subscriber_data['email'] =>
|
'email' => $subscriber_data['email'],
|
||||||
(isset($subscriber_data['name'])) ? $subscriber_data['name'] : ''
|
'name' => (isset($subscriber_data['name'])) ? $subscriber_data['name'] : ''
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -35,7 +35,8 @@ class PHPMailTest extends MailPoetTest {
|
|||||||
|
|
||||||
function testItCanBuildMailer() {
|
function testItCanBuildMailer() {
|
||||||
$mailer = $this->mailer->buildMailer();
|
$mailer = $this->mailer->buildMailer();
|
||||||
expect($mailer->getTransport() instanceof \Swift_MailTransport)->true();
|
expect($mailer)->isInstanceOf('PHPMailer');
|
||||||
|
expect($mailer->Mailer)->equals('mail'); // uses PHP's mail() function
|
||||||
}
|
}
|
||||||
|
|
||||||
function testWhenReturnPathIsNullItIsSetToSenderEmail() {
|
function testWhenReturnPathIsNullItIsSetToSenderEmail() {
|
||||||
@ -47,34 +48,63 @@ class PHPMailTest extends MailPoetTest {
|
|||||||
expect($mailer->return_path)->equals($this->sender['from_email']);
|
expect($mailer->return_path)->equals($this->sender['from_email']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanCreateMessage() {
|
function testItCanConfigureMailerWithMessage() {
|
||||||
$message = $this->mailer
|
$mailer = $this->mailer
|
||||||
->createMessage($this->newsletter, $this->subscriber, $this->extra_params);
|
->configureMailerWithMessage($this->newsletter, $this->subscriber, $this->extra_params);
|
||||||
expect($message->getTo())
|
expect($mailer->CharSet)->equals('UTF-8');
|
||||||
->equals(array('mailpoet-phoenix-test@mailinator.com' => 'Recipient'));
|
expect($mailer->getToAddresses())->equals(
|
||||||
expect($message->getFrom())
|
array(
|
||||||
->equals(array($this->sender['from_email'] => $this->sender['from_name']));
|
array(
|
||||||
expect($message->getSender())
|
'mailpoet-phoenix-test@mailinator.com',
|
||||||
->equals(array($this->sender['from_email'] => null));
|
'Recipient'
|
||||||
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($mailer->getAllRecipientAddresses())
|
||||||
expect($message->getBody())
|
->equals(array('mailpoet-phoenix-test@mailinator.com' => true));
|
||||||
|
expect($mailer->From)->equals($this->sender['from_email']);
|
||||||
|
expect($mailer->FromName)->equals($this->sender['from_name']);
|
||||||
|
expect($mailer->getReplyToAddresses())->equals(
|
||||||
|
array(
|
||||||
|
'reply-to@mailpoet.com' => array(
|
||||||
|
'reply-to@mailpoet.com',
|
||||||
|
'Reply To'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
expect($mailer->Sender)->equals($this->return_path);
|
||||||
|
expect($mailer->ContentType)->equals('text/html');
|
||||||
|
expect($mailer->Subject)->equals($this->newsletter['subject']);
|
||||||
|
expect($mailer->Body)
|
||||||
->equals($this->newsletter['body']['html']);
|
->equals($this->newsletter['body']['html']);
|
||||||
expect($message->getChildren()[0]->getContentType())
|
expect($mailer->AltBody)
|
||||||
->equals('text/plain');
|
->equals($this->newsletter['body']['text']);
|
||||||
expect($message->getHeaders()->get('List-Unsubscribe')->getValue())
|
expect($mailer->getCustomHeaders())->equals(
|
||||||
->equals('<' . $this->extra_params['unsubscribe_url'] . '>');
|
array(
|
||||||
|
array(
|
||||||
|
'List-Unsubscribe',
|
||||||
|
'http://www.mailpoet.com'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanProcessSubscriber() {
|
function testItCanProcessSubscriber() {
|
||||||
expect($this->mailer->processSubscriber('test@test.com'))
|
expect($this->mailer->processSubscriber('test@test.com'))->equals(
|
||||||
->equals(array('test@test.com' => ''));
|
array(
|
||||||
expect($this->mailer->processSubscriber('First <test@test.com>'))
|
'email' => 'test@test.com',
|
||||||
->equals(array('test@test.com' => 'First'));
|
'name' => ''
|
||||||
expect($this->mailer->processSubscriber('First Last <test@test.com>'))
|
));
|
||||||
->equals(array('test@test.com' => 'First Last'));
|
expect($this->mailer->processSubscriber('First <test@test.com>'))->equals(
|
||||||
|
array(
|
||||||
|
'email' => 'test@test.com',
|
||||||
|
'name' => 'First'
|
||||||
|
));
|
||||||
|
expect($this->mailer->processSubscriber('First Last <test@test.com>'))->equals(
|
||||||
|
array(
|
||||||
|
'email' => 'test@test.com',
|
||||||
|
'name' => 'First Last'
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanSend() {
|
function testItCanSend() {
|
||||||
|
Reference in New Issue
Block a user