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 = []) {
try {
$mailer = new \MailPoet\Mailer\Mailer(
$mailer = new \MailPoet\Mailer\Mailer();
$mailer->init(
(isset($data['mailer'])) ? $data['mailer'] : false,
(isset($data['sender'])) ? $data['sender'] : false,
(isset($data['reply_to'])) ? $data['reply_to'] : false

View File

@ -442,11 +442,7 @@ class Newsletters extends APIEndpoint {
try {
$mailer = (!empty($data['mailer'])) ?
$data['mailer'] :
new \MailPoet\Mailer\Mailer(
$mailer = false,
$sender = false,
$reply_to = false
);
new \MailPoet\Mailer\Mailer();
$extra_params = ['unsubscribe_url' => WPFunctions::get()->homeUrl()];
$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
try {
new \MailPoet\Mailer\Mailer(false);
$mailer = new \MailPoet\Mailer\Mailer();
$mailer->init();
} catch (\Exception $e) {
return $this->errorResponse([
$e->getCode() => $e->getMessage(),

View File

@ -32,7 +32,8 @@ class Mailer {
if (!$reply_to['address']) {
$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;
}

View File

@ -26,8 +26,14 @@ class Mailer {
const METHOD_PHPMAIL = 'PHPMail';
const METHOD_SMTP = 'SMTP';
function __construct($mailer = false, $sender = false, $reply_to = false, $return_path = false) {
$this->settings = new SettingsController();
function __construct(SettingsController $settings = null) {
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->sender = $this->getSenderNameAndAddress($sender);
$this->reply_to = $this->getReplyToNameAndAddress($reply_to);
@ -36,6 +42,9 @@ class Mailer {
}
function send($newsletter, $subscriber, $extra_params = []) {
if (!$this->mailer_instance) {
$this->init();
}
$subscriber = $this->formatSubscriberNameAndEmailAddress($subscriber);
return $this->mailer_instance->send($newsletter, $subscriber, $extra_params);
}
@ -116,7 +125,7 @@ class Mailer {
return $mailer;
}
function getSenderNameAndAddress($sender = false) {
private function getSenderNameAndAddress($sender = false) {
if (empty($sender)) {
$sender = $this->settings->get('sender', []);
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
try {
if (!$this->mailer) {
$this->mailer = new Mailer(false, $from, $reply_to);
$this->mailer = new Mailer();
}
$this->mailer->getSenderNameAndAddress($from);
$this->mailer->getReplyToNameAndAddress($reply_to);
$this->mailer->init(false, $from, $reply_to);
$result = $this->mailer->send($email, $subscriber);
if ($result['response'] === false) {
$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);
try {
$mailer = new Mailer();
$mailer->init();
$this->fail('Mailer did not throw an exception');
} catch (\Exception $e) {
expect($e->getMessage())->equals('Mailer is not configured.');
@ -79,7 +80,8 @@ class MailerTest extends \MailPoetTest {
function testItRequiresSender() {
try {
$mailer = new Mailer($mailer = $this->mailer);
$mailer = new Mailer();
$mailer->init($mailer = $this->mailer);
$this->fail('Mailer did not throw an exception');
} catch (\Exception $e) {
expect($e->getMessage())->equals('Sender name and email are not configured.');
@ -87,7 +89,8 @@ class MailerTest extends \MailPoetTest {
}
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_email'])->equals($this->sender['address']);
expect($mailer->reply_to['reply_to_name'])->equals($this->reply_to['name']);
@ -97,7 +100,8 @@ class MailerTest extends \MailPoetTest {
function testItThrowsUnknownMailerException() {
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');
} catch (\Exception $e) {
expect($e->getMessage())->equals('Mailing method does not exist.');
@ -106,13 +110,15 @@ class MailerTest extends \MailPoetTest {
function testItSetsReplyToAddressWhenOnlyNameIsAvailable() {
$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();
expect($reply_to['reply_to_email'])->equals($this->sender['address']);
}
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');
expect($return_path)->equals('bounce@test.com');
$this->settings->set('bounce', ['address' => 'settngs_bounce@test.com']);
@ -121,7 +127,8 @@ class MailerTest extends \MailPoetTest {
}
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'))
->equals('test@email.com');
expect($mailer->formatSubscriberNameAndEmailAddress(
@ -151,7 +158,8 @@ class MailerTest extends \MailPoetTest {
}
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->reply_to['reply_to_name'])->equals($this->reply_to['name']);
$sender = [
@ -162,7 +170,8 @@ class MailerTest extends \MailPoetTest {
'name' => 'Reply-To Außergewöhnlichen тест системы',
'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'])
->equals(sprintf('=?utf-8?B?%s?=', base64_encode($sender['name'])));
expect($mailer->reply_to['reply_to_name'])
@ -172,7 +181,8 @@ class MailerTest extends \MailPoetTest {
function testItCanSend() {
if (getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
$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);
expect($result['response'])->true();
}