Move mailer configuration to init method

[MAILPOET-2146]
This commit is contained in:
Pavel Dohnal
2019-07-24 10:40:00 +02:00
committed by M. Shull
parent d6f263ef3f
commit 8739646a6a
7 changed files with 40 additions and 23 deletions

View File

@ -36,7 +36,8 @@ class Mailer extends APIEndpoint {
function send($data = []) { function send($data = []) {
try { try {
$mailer = new \MailPoet\Mailer\Mailer( $mailer = new \MailPoet\Mailer\Mailer();
$mailer->init(
(isset($data['mailer'])) ? $data['mailer'] : false, (isset($data['mailer'])) ? $data['mailer'] : false,
(isset($data['sender'])) ? $data['sender'] : false, (isset($data['sender'])) ? $data['sender'] : false,
(isset($data['reply_to'])) ? $data['reply_to'] : false (isset($data['reply_to'])) ? $data['reply_to'] : false

View File

@ -442,11 +442,7 @@ class Newsletters extends APIEndpoint {
try { try {
$mailer = (!empty($data['mailer'])) ? $mailer = (!empty($data['mailer'])) ?
$data['mailer'] : $data['mailer'] :
new \MailPoet\Mailer\Mailer( new \MailPoet\Mailer\Mailer();
$mailer = false,
$sender = false,
$reply_to = false
);
$extra_params = ['unsubscribe_url' => WPFunctions::get()->homeUrl()]; $extra_params = ['unsubscribe_url' => WPFunctions::get()->homeUrl()];
$result = $mailer->send($rendered_newsletter, $data['subscriber'], $extra_params); $result = $mailer->send($rendered_newsletter, $data['subscriber'], $extra_params);

View File

@ -36,7 +36,8 @@ class SendingQueue extends APIEndpoint {
// check that the sending method has been configured properly // check that the sending method has been configured properly
try { try {
new \MailPoet\Mailer\Mailer(false); $mailer = new \MailPoet\Mailer\Mailer();
$mailer->init();
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->errorResponse([ return $this->errorResponse([
$e->getCode() => $e->getMessage(), $e->getCode() => $e->getMessage(),

View File

@ -32,7 +32,8 @@ class Mailer {
if (!$reply_to['address']) { if (!$reply_to['address']) {
$reply_to = false; $reply_to = false;
} }
$this->mailer = new MailerFactory($method = false, $sender, $reply_to); $this->mailer = new MailerFactory();
$this->mailer->init($method = false, $sender, $reply_to);
return $this->mailer; return $this->mailer;
} }

View File

@ -26,8 +26,14 @@ class Mailer {
const METHOD_PHPMAIL = 'PHPMail'; const METHOD_PHPMAIL = 'PHPMail';
const METHOD_SMTP = 'SMTP'; const METHOD_SMTP = 'SMTP';
function __construct($mailer = false, $sender = false, $reply_to = false, $return_path = false) { function __construct(SettingsController $settings = null) {
$this->settings = new SettingsController(); if (!$settings) {
$settings = new SettingsController();
}
$this->settings = $settings;
}
function init($mailer = false, $sender = false, $reply_to = false, $return_path = false) {
$this->mailer_config = self::getMailerConfig($mailer); $this->mailer_config = self::getMailerConfig($mailer);
$this->sender = $this->getSenderNameAndAddress($sender); $this->sender = $this->getSenderNameAndAddress($sender);
$this->reply_to = $this->getReplyToNameAndAddress($reply_to); $this->reply_to = $this->getReplyToNameAndAddress($reply_to);
@ -36,6 +42,9 @@ class Mailer {
} }
function send($newsletter, $subscriber, $extra_params = []) { function send($newsletter, $subscriber, $extra_params = []) {
if (!$this->mailer_instance) {
$this->init();
}
$subscriber = $this->formatSubscriberNameAndEmailAddress($subscriber); $subscriber = $this->formatSubscriberNameAndEmailAddress($subscriber);
return $this->mailer_instance->send($newsletter, $subscriber, $extra_params); return $this->mailer_instance->send($newsletter, $subscriber, $extra_params);
} }
@ -116,7 +125,7 @@ class Mailer {
return $mailer; return $mailer;
} }
function getSenderNameAndAddress($sender = false) { private function getSenderNameAndAddress($sender = false) {
if (empty($sender)) { if (empty($sender)) {
$sender = $this->settings->get('sender', []); $sender = $this->settings->get('sender', []);
if (empty($sender['address'])) throw new \Exception(__('Sender name and email are not configured.', 'mailpoet')); if (empty($sender['address'])) throw new \Exception(__('Sender name and email are not configured.', 'mailpoet'));

View File

@ -110,10 +110,9 @@ class ConfirmationEmailMailer {
// send email // send email
try { try {
if (!$this->mailer) { if (!$this->mailer) {
$this->mailer = new Mailer(false, $from, $reply_to); $this->mailer = new Mailer();
} }
$this->mailer->getSenderNameAndAddress($from); $this->mailer->init(false, $from, $reply_to);
$this->mailer->getReplyToNameAndAddress($reply_to);
$result = $this->mailer->send($email, $subscriber); $result = $this->mailer->send($email, $subscriber);
if ($result['response'] === false) { if ($result['response'] === false) {
$subscriber->setError(__('Something went wrong with your subscription. Please contact the website owner.', 'mailpoet')); $subscriber->setError(__('Something went wrong with your subscription. Please contact the website owner.', 'mailpoet'));

View File

@ -71,6 +71,7 @@ class MailerTest extends \MailPoetTest {
$this->settings->set('mta', null); $this->settings->set('mta', null);
try { try {
$mailer = new Mailer(); $mailer = new Mailer();
$mailer->init();
$this->fail('Mailer did not throw an exception'); $this->fail('Mailer did not throw an exception');
} catch (\Exception $e) { } catch (\Exception $e) {
expect($e->getMessage())->equals('Mailer is not configured.'); expect($e->getMessage())->equals('Mailer is not configured.');
@ -79,7 +80,8 @@ class MailerTest extends \MailPoetTest {
function testItRequiresSender() { function testItRequiresSender() {
try { try {
$mailer = new Mailer($mailer = $this->mailer); $mailer = new Mailer();
$mailer->init($mailer = $this->mailer);
$this->fail('Mailer did not throw an exception'); $this->fail('Mailer did not throw an exception');
} catch (\Exception $e) { } catch (\Exception $e) {
expect($e->getMessage())->equals('Sender name and email are not configured.'); expect($e->getMessage())->equals('Sender name and email are not configured.');
@ -87,7 +89,8 @@ class MailerTest extends \MailPoetTest {
} }
function testItCanConstruct() { function testItCanConstruct() {
$mailer = new Mailer($this->mailer, $this->sender, $this->reply_to, $this->return_path); $mailer = new Mailer();
$mailer->init($this->mailer, $this->sender, $this->reply_to, $this->return_path);
expect($mailer->sender['from_name'])->equals($this->sender['name']); expect($mailer->sender['from_name'])->equals($this->sender['name']);
expect($mailer->sender['from_email'])->equals($this->sender['address']); expect($mailer->sender['from_email'])->equals($this->sender['address']);
expect($mailer->reply_to['reply_to_name'])->equals($this->reply_to['name']); expect($mailer->reply_to['reply_to_name'])->equals($this->reply_to['name']);
@ -97,7 +100,8 @@ class MailerTest extends \MailPoetTest {
function testItThrowsUnknownMailerException() { function testItThrowsUnknownMailerException() {
try { try {
$mailer = new Mailer(['method' => 'Unknown'], $this->sender); $mailer = new Mailer();
$mailer->init(['method' => 'Unknown'], $this->sender);
$this->fail('Mailer did not throw an exception'); $this->fail('Mailer did not throw an exception');
} catch (\Exception $e) { } catch (\Exception $e) {
expect($e->getMessage())->equals('Mailing method does not exist.'); expect($e->getMessage())->equals('Mailing method does not exist.');
@ -106,13 +110,15 @@ class MailerTest extends \MailPoetTest {
function testItSetsReplyToAddressWhenOnlyNameIsAvailable() { function testItSetsReplyToAddressWhenOnlyNameIsAvailable() {
$reply_to = ['name' => 'test']; $reply_to = ['name' => 'test'];
$mailer = new Mailer($this->mailer, $this->sender, $reply_to); $mailer = new Mailer();
$mailer->init($this->mailer, $this->sender, $reply_to);
$reply_to = $mailer->getReplyToNameAndAddress(); $reply_to = $mailer->getReplyToNameAndAddress();
expect($reply_to['reply_to_email'])->equals($this->sender['address']); expect($reply_to['reply_to_email'])->equals($this->sender['address']);
} }
function testItGetsReturnPathAddress() { function testItGetsReturnPathAddress() {
$mailer = new Mailer($this->mailer, $this->sender, $this->reply_to); $mailer = new Mailer();
$mailer->init($this->mailer, $this->sender, $this->reply_to);
$return_path = $mailer->getReturnPathAddress('bounce@test.com'); $return_path = $mailer->getReturnPathAddress('bounce@test.com');
expect($return_path)->equals('bounce@test.com'); expect($return_path)->equals('bounce@test.com');
$this->settings->set('bounce', ['address' => 'settngs_bounce@test.com']); $this->settings->set('bounce', ['address' => 'settngs_bounce@test.com']);
@ -121,7 +127,8 @@ class MailerTest extends \MailPoetTest {
} }
function testItCanTransformSubscriber() { function testItCanTransformSubscriber() {
$mailer = new Mailer($this->mailer, $this->sender, $this->reply_to); $mailer = new Mailer();
$mailer->init($this->mailer, $this->sender, $this->reply_to);
expect($mailer->formatSubscriberNameAndEmailAddress('test@email.com')) expect($mailer->formatSubscriberNameAndEmailAddress('test@email.com'))
->equals('test@email.com'); ->equals('test@email.com');
expect($mailer->formatSubscriberNameAndEmailAddress( expect($mailer->formatSubscriberNameAndEmailAddress(
@ -151,7 +158,8 @@ class MailerTest extends \MailPoetTest {
} }
function testItCanConvertNonASCIIEmailAddressString() { function testItCanConvertNonASCIIEmailAddressString() {
$mailer = new Mailer($this->mailer, $this->sender, $this->reply_to); $mailer = new Mailer();
$mailer->init($this->mailer, $this->sender, $this->reply_to);
expect($mailer->sender['from_name'])->equals($this->sender['name']); expect($mailer->sender['from_name'])->equals($this->sender['name']);
expect($mailer->reply_to['reply_to_name'])->equals($this->reply_to['name']); expect($mailer->reply_to['reply_to_name'])->equals($this->reply_to['name']);
$sender = [ $sender = [
@ -162,7 +170,8 @@ class MailerTest extends \MailPoetTest {
'name' => 'Reply-To Außergewöhnlichen тест системы', 'name' => 'Reply-To Außergewöhnlichen тест системы',
'address' => 'staff@mailinator.com', 'address' => 'staff@mailinator.com',
]; ];
$mailer = new Mailer($this->mailer, $sender, $reply_to); $mailer = new Mailer();
$mailer->init($this->mailer, $sender, $reply_to);
expect($mailer->sender['from_name']) expect($mailer->sender['from_name'])
->equals(sprintf('=?utf-8?B?%s?=', base64_encode($sender['name']))); ->equals(sprintf('=?utf-8?B?%s?=', base64_encode($sender['name'])));
expect($mailer->reply_to['reply_to_name']) expect($mailer->reply_to['reply_to_name'])
@ -172,7 +181,8 @@ class MailerTest extends \MailPoetTest {
function testItCanSend() { function testItCanSend() {
if (getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return; if (getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
$this->sender['address'] = 'staff@mailpoet.com'; $this->sender['address'] = 'staff@mailpoet.com';
$mailer = new Mailer($this->mailer, $this->sender, $this->reply_to); $mailer = new Mailer();
$mailer->init($this->mailer, $this->sender, $this->reply_to);
$result = $mailer->send($this->newsletter, $this->subscriber); $result = $mailer->send($this->newsletter, $this->subscriber);
expect($result['response'])->true(); expect($result['response'])->true();
} }