diff --git a/lib/Mailer/API/AmazonSES.php b/lib/Mailer/API/AmazonSES.php index f459a34ca0..370e2fac93 100644 --- a/lib/Mailer/API/AmazonSES.php +++ b/lib/Mailer/API/AmazonSES.php @@ -38,7 +38,6 @@ class AmazonSES { 'Message.Subject.Data' => $this->newsletter['subject'], 'Message.Body.Html.Data' => $this->newsletter['body']['html'], 'Message.Body.Text.Data' => $this->newsletter['body']['text'], - 'ReplyToAddresses.member.1' => $this->from, 'ReturnPath' => $this->from ); return urldecode(http_build_query($parameters)); diff --git a/lib/Mailer/API/Mandrill.php b/lib/Mailer/API/Mandrill.php index 09f40279db..2f7b9cf988 100644 --- a/lib/Mailer/API/Mandrill.php +++ b/lib/Mailer/API/Mandrill.php @@ -46,10 +46,7 @@ class Mandrill { 'to' => $this->subscriber, 'subject' => $this->newsletter['subject'], 'html' => $this->newsletter['body']['html'], - 'text' => $this->newsletter['body']['text'], - 'headers' => array( - 'Reply-To' => $this->from_email - ) + 'text' => $this->newsletter['body']['text'] ), 'async' => false, ); diff --git a/lib/Mailer/API/SendGrid.php b/lib/Mailer/API/SendGrid.php index 6dc1fb68ad..7add14e242 100644 --- a/lib/Mailer/API/SendGrid.php +++ b/lib/Mailer/API/SendGrid.php @@ -4,10 +4,11 @@ namespace MailPoet\Mailer\API; if(!defined('ABSPATH')) exit; class SendGrid { - function __construct($api_key, $from) { + function __construct($api_key, $from_email, $from_name) { $this->url = 'https://api.sendgrid.com/api/mail.send.json'; $this->api_key = $api_key; - $this->from = $from; + $this->fromEmail = $from_email; + $this->fromName = $from_name; } function send($newsletter, $subscriber) { @@ -25,7 +26,8 @@ class SendGrid { function getBody() { $parameters = array( 'to' => $this->subscriber, - 'from' => $this->from, + 'from' => $this->fromEmail, + 'fromname' => $this->fromName, 'subject' => $this->newsletter['subject'], 'html' => $this->newsletter['body']['html'], 'text' => $this->newsletter['body']['text'] diff --git a/lib/Router/Mailer.php b/lib/Router/Mailer.php index f711914035..fe6cc2d1ac 100644 --- a/lib/Router/Mailer.php +++ b/lib/Router/Mailer.php @@ -13,11 +13,11 @@ class Mailer { function send($newsletter, $subscriber) { $subscriber = $this->transformSubscriber($subscriber); - $mailer = $this->configureMailer(); + $mailer = $this->buildMailer(); return wp_send_json($mailer->send($newsletter, $subscriber)); } - function configureMailer() { + function buildMailer() { switch ($this->mailer['name']) { case 'AmazonSES': $mailer = new $this->mailer['class']($this->mailer['region'], $this->mailer['access_key'], $this->mailer['secret_key'], $this->from); @@ -32,7 +32,7 @@ class Mailer { $mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName); break; case 'SendGrid': - $mailer = new $this->mailer['class']($this->mailer['api_key'], $this->from); + $mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName); break; } return $mailer; @@ -42,6 +42,7 @@ class Mailer { if(!is_array($subscriber)) return $subscriber; $first_name = (isset($subscriber['first_name'])) ? $subscriber['first_name'] : ''; $last_name = (isset($subscriber['last_name'])) ? $subscriber['last_name'] : ''; + if (!$first_name && !$last_name) return $subscriber['email']; $subscriber = sprintf('%s %s <%s>', $first_name, $last_name, $subscriber['email']); $subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber)); return $subscriber; @@ -83,7 +84,7 @@ class Mailer { return array_merge($mailer, array('class' => sprintf('MailPoet\\Mailer\\%s\\%s', $mailer['type'], $mailer['name']))); } if($setting === 'from_name') return 'Sender'; - if($setting === 'from_address') return 'mailpoet-test1@mailinator.com'; + if($setting === 'from_address') return 'mailpoet-phoenix-test@mailinator.com'; return Setting::where('name', $setting) ->findOne()->value; } diff --git a/tests/unit/Mailer/API/AmazonSESCest.php b/tests/unit/Mailer/API/AmazonSESCest.php index 9f42538408..a4c435e2f1 100644 --- a/tests/unit/Mailer/API/AmazonSESCest.php +++ b/tests/unit/Mailer/API/AmazonSESCest.php @@ -13,7 +13,7 @@ class AmazonSESCest { ); $this->from = 'Sender '; $this->mailer = new AmazonSES($this->settings['region'], $this->settings['access_key'], $this->settings['secret_key'], $this->from); - $this->mailer->subscriber = 'Recipient '; + $this->mailer->subscriber = 'Recipient '; $this->mailer->newsletter = array( 'subject' => 'testing AmazonSES', 'body' => array( @@ -47,8 +47,6 @@ class AmazonSESCest { expect($body[6]) ->equals('Message.Body.Text.Data=' . $this->mailer->newsletter['body']['text']); expect($body[7]) - ->equals('ReplyToAddresses.member.1=' . $this->from); - expect($body[8]) ->equals('ReturnPath=' . $this->from); } diff --git a/tests/unit/Mailer/API/ElasticEmailCest.php b/tests/unit/Mailer/API/ElasticEmailCest.php index fa9077aa59..094a4957ed 100644 --- a/tests/unit/Mailer/API/ElasticEmailCest.php +++ b/tests/unit/Mailer/API/ElasticEmailCest.php @@ -12,7 +12,7 @@ class ElasticEmailCest { $this->fromEmail = 'do-not-reply@mailpoet.com'; $this->fromName = 'Sender'; $this->mailer = new ElasticEmail($this->settings['api_key'], $this->fromEmail, $this->fromName); - $this->mailer->subscriber = 'Recipient '; + $this->mailer->subscriber = 'Recipient '; $this->mailer->newsletter = array( 'subject' => 'testing ElasticEmail', 'body' => array( diff --git a/tests/unit/Mailer/API/MailGunCest.php b/tests/unit/Mailer/API/MailGunCest.php index 51426b7cd4..9e5b0e7229 100644 --- a/tests/unit/Mailer/API/MailGunCest.php +++ b/tests/unit/Mailer/API/MailGunCest.php @@ -12,7 +12,7 @@ class MailGunCest { ); $this->from = 'Sender '; $this->mailer = new MailGun($this->settings['domain'], $this->settings['api_key'], $this->from); - $this->mailer->subscriber = 'Recipient '; + $this->mailer->subscriber = 'Recipient '; $this->mailer->newsletter = array( 'subject' => 'testing MailGun', 'body' => array( diff --git a/tests/unit/Mailer/API/MandrillCest.php b/tests/unit/Mailer/API/MandrillCest.php index 60ebdb5dc2..51b5db0ea2 100644 --- a/tests/unit/Mailer/API/MandrillCest.php +++ b/tests/unit/Mailer/API/MandrillCest.php @@ -12,7 +12,7 @@ class MandrillCest { $this->fromEmail = 'do-not-reply@mailpoet.com'; $this->fromName = 'Sender'; $this->mailer = new Mandrill($this->settings['api_key'], $this->fromEmail, $this->fromName); - $this->mailer->subscriber = 'Recipient '; + $this->mailer->subscriber = 'Recipient '; $this->mailer->newsletter = array( 'subject' => 'testing Mandrill', 'body' => array( @@ -43,9 +43,6 @@ class MandrillCest { expect($body['message']['text'])->equals( $this->mailer->newsletter['body']['text'] ); - expect($body['message']['headers']['Reply-To'])->equals( - $this->fromEmail - ); expect($body['async'])->false(); } diff --git a/tests/unit/Mailer/API/SendGridCest.php b/tests/unit/Mailer/API/SendGridCest.php index 0c081c67e6..f3752db18b 100644 --- a/tests/unit/Mailer/API/SendGridCest.php +++ b/tests/unit/Mailer/API/SendGridCest.php @@ -9,9 +9,10 @@ class SendGridCest { 'type' => 'API', 'api_key' => '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' ); - $this->from = 'Sender '; - $this->mailer = new SendGrid($this->settings['api_key'], $this->from); - $this->mailer->subscriber = 'Recipient '; + $this->fromEmail = 'do-not-reply@mailpoet.com'; + $this->fromName = 'Sender'; + $this->mailer = new SendGrid($this->settings['api_key'], $this->fromEmail, $this->fromName); + $this->mailer->subscriber = 'Recipient '; $this->mailer->newsletter = array( 'subject' => 'testing SendGrid', 'body' => array( @@ -26,12 +27,14 @@ class SendGridCest { expect($body[0]) ->contains('to=' . $this->mailer->subscriber); expect($body[1]) - ->equals('from=' . $this->from); + ->equals('from=' . $this->fromEmail); expect($body[2]) - ->equals('subject=' . $this->mailer->newsletter['subject']); + ->equals('fromname=' . $this->fromName); expect($body[3]) - ->equals('html=' . $this->mailer->newsletter['body']['html']); + ->equals('subject=' . $this->mailer->newsletter['subject']); expect($body[4]) + ->equals('html=' . $this->mailer->newsletter['body']['html']); + expect($body[5]) ->equals('text=' . $this->mailer->newsletter['body']['text']); } diff --git a/tests/unit/Router/MailerCest.php b/tests/unit/Router/MailerCest.php index 13cec7e699..0f76693c09 100644 --- a/tests/unit/Router/MailerCest.php +++ b/tests/unit/Router/MailerCest.php @@ -8,12 +8,17 @@ class MailerCest { } function itCanConstruct() { - expect($this->router->from)->equals('Sender '); + expect($this->router->from)->equals('Sender '); } function itCanTransformSubscriber() { expect($this->router->transformSubscriber('test@email.com')) ->equals('test@email.com'); + expect($this->router->transformSubscriber( + array( + 'email' => 'test@email.com' + )) + )->equals('test@email.com'); expect($this->router->transformSubscriber( array( 'first_name' => 'First', @@ -36,7 +41,7 @@ class MailerCest { } function itCanConfigureMailer() { - $mailer = $this->router->configureMailer(); + $mailer = $this->router->buildMailer(); $class = 'Mailpoet\\Mailer\\' . $this->router->mailer['type'] . '\\' . $this->router->mailer['name']; @@ -55,7 +60,7 @@ class MailerCest { $subscriber = array( 'first_name' => 'First', 'last_name' => 'Last', - 'email' => 'mailpoet-test1@mailinator.com' + 'email' => 'mailpoet-phoenix-test@mailinator.com' ); expect($this->router->send($newsletter, $subscriber))->true(); }