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