From 0906fcda93aff192ac9522c145f3ec428cf72838 Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 4 Apr 2014 14:30:23 +0200 Subject: [PATCH 1/9] Make explicit you define variables for all recipients --- docs/Mailgun.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/Mailgun.md b/docs/Mailgun.md index 0dd6e95..a73f4fa 100644 --- a/docs/Mailgun.md +++ b/docs/Mailgun.md @@ -92,9 +92,10 @@ $message->addTo('demo2@mailgun.com'); $message->setSubject("Hi %recipient.name%"); $message->setBody("Hi, activate your account by clicking on http://mailgun.com/activate/%recipient.key%"); +// Set all variables for demo1@mailgun.com $message->setRecipientVariables('demo1@mailgun.com', array('name' => 'Demo', 'key' => 'key1')); -// Or add one: +// Or add one by one for demo2@mailgun.com: $message->addRecipientVariable('demo2@mailgun.com', 'name', 'Demo'); $message->addRecipientVariable('demo2@mailgun.com', 'key', 'key2'); ``` From 3338de86ac71e6af77236340d4be4eb052205821 Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 4 Apr 2014 14:31:45 +0200 Subject: [PATCH 2/9] Revert indentation changes from #68 --- src/SlmMail/Mail/Message/Mailgun.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/SlmMail/Mail/Message/Mailgun.php b/src/SlmMail/Mail/Message/Mailgun.php index 0e24493..f151282 100644 --- a/src/SlmMail/Mail/Message/Mailgun.php +++ b/src/SlmMail/Mail/Message/Mailgun.php @@ -60,12 +60,12 @@ class Mailgun extends Message * @var array */ protected $validOptions = array( - 'dkim' => 'o:dkim', - 'delivery_time' => 'o:deliverytime', - 'test_mode' => 'o:testmode', - 'tracking' => 'o:tracking', - 'tracking_clicks' => 'o:tracking-clicks', - 'tracking_opens' => 'o:tracking-opens', + 'dkim' => 'o:dkim', + 'delivery_time' => 'o:deliverytime', + 'test_mode' => 'o:testmode', + 'tracking' => 'o:tracking', + 'tracking_clicks' => 'o:tracking-clicks', + 'tracking_opens' => 'o:tracking-opens', ); /** From e94821bffa85275ce4eb89692b1e86b2dc07282f Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 4 Apr 2014 14:33:37 +0200 Subject: [PATCH 3/9] Cast recipient variables to a string --- src/SlmMail/Mail/Message/Mailgun.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/SlmMail/Mail/Message/Mailgun.php b/src/SlmMail/Mail/Message/Mailgun.php index f151282..4932022 100644 --- a/src/SlmMail/Mail/Message/Mailgun.php +++ b/src/SlmMail/Mail/Message/Mailgun.php @@ -188,7 +188,7 @@ public function getValidOptions() */ public function setRecipientVariables($recipient, array $variables) { - $this->recipientVariables[$recipient] = $variables; + $this->recipientVariables[(string) $recipient] = $variables; } /** @@ -196,8 +196,9 @@ public function setRecipientVariables($recipient, array $variables) * @param string $key * @param string $value */ - public function addRecipientVariable($recipient, $key, $value) { - $this->recipientVariables[$recipient][$key] = $value; + public function addRecipientVariable($recipient, $key, $value) + { + $this->recipientVariables[(string) $recipient][(string) $key] = (string) $value; } /** From aa898f1e88f14bc70218c9df4e7210cc581a39be Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 4 Apr 2014 14:34:19 +0200 Subject: [PATCH 4/9] Relocate MailgunMessage instance check --- src/SlmMail/Service/MailgunService.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/SlmMail/Service/MailgunService.php b/src/SlmMail/Service/MailgunService.php index f392fac..074b6d6 100644 --- a/src/SlmMail/Service/MailgunService.php +++ b/src/SlmMail/Service/MailgunService.php @@ -113,18 +113,6 @@ public function send(Message $message) $parameters['to'] = implode(',', $to); - if ($message instanceof MailgunMessage && count($message->getRecipientVariables())) { - foreach ($message->getRecipientVariables() as $recipientEmail => $variables) { - if (!$message->getTo()->has($recipientEmail)) { - throw new \Exception(sprintf( - 'The email "%s" must be added as a receiver before you can add recipient variables', $recipientEmail - )); - } - } - - $parameters['recipient-variables'] = json_encode($message->getRecipientVariables()); - } - $cc = array(); foreach ($message->getCc() as $address) { $cc[] = $address->toString(); @@ -154,6 +142,17 @@ public function send(Message $message) $parameters['o:tag'] = $message->getTags(); } } + if ($message instanceof MailgunMessage && count($message->getRecipientVariables())) { + foreach ($message->getRecipientVariables() as $recipientEmail => $variables) { + if (!$message->getTo()->has($recipientEmail)) { + throw new \Exception(sprintf( + 'The email "%s" must be added as a receiver before you can add recipient variables', $recipientEmail + )); + } + } + + $parameters['recipient-variables'] = json_encode($message->getRecipientVariables()); + } $client = $this->prepareHttpClient('/messages', $parameters); From 8d290890a54175cbb50c29fc8a69bc61d4482129 Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 4 Apr 2014 14:35:12 +0200 Subject: [PATCH 5/9] Simplify check on instanceof MailgunMessage --- src/SlmMail/Service/MailgunService.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/SlmMail/Service/MailgunService.php b/src/SlmMail/Service/MailgunService.php index 074b6d6..609cb8e 100644 --- a/src/SlmMail/Service/MailgunService.php +++ b/src/SlmMail/Service/MailgunService.php @@ -141,17 +141,18 @@ public function send(Message $message) if (count($message->getTags()) > 0) { $parameters['o:tag'] = $message->getTags(); } - } - if ($message instanceof MailgunMessage && count($message->getRecipientVariables())) { - foreach ($message->getRecipientVariables() as $recipientEmail => $variables) { - if (!$message->getTo()->has($recipientEmail)) { - throw new \Exception(sprintf( - 'The email "%s" must be added as a receiver before you can add recipient variables', $recipientEmail - )); + + if (count($message->getRecipientVariables())) { + foreach ($message->getRecipientVariables() as $recipientEmail => $variables) { + if (!$message->getTo()->has($recipientEmail)) { + throw new \Exception(sprintf( + 'The email "%s" must be added as a receiver before you can add recipient variables', $recipientEmail + )); + } } + + $parameters['recipient-variables'] = json_encode($message->getRecipientVariables()); } - - $parameters['recipient-variables'] = json_encode($message->getRecipientVariables()); } $client = $this->prepareHttpClient('/messages', $parameters); From 9433e1959000e2e6fa92301bcb1a39affa8aa639 Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 4 Apr 2014 14:37:06 +0200 Subject: [PATCH 6/9] Extract getRecipientVariables() into variable --- src/SlmMail/Service/MailgunService.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/SlmMail/Service/MailgunService.php b/src/SlmMail/Service/MailgunService.php index 609cb8e..26a9d8d 100644 --- a/src/SlmMail/Service/MailgunService.php +++ b/src/SlmMail/Service/MailgunService.php @@ -143,7 +143,8 @@ public function send(Message $message) } if (count($message->getRecipientVariables())) { - foreach ($message->getRecipientVariables() as $recipientEmail => $variables) { + $variables = $message->getRecipientVariables(); + foreach ($variables as $recipientEmail => $variable) { if (!$message->getTo()->has($recipientEmail)) { throw new \Exception(sprintf( 'The email "%s" must be added as a receiver before you can add recipient variables', $recipientEmail @@ -151,7 +152,7 @@ public function send(Message $message) } } - $parameters['recipient-variables'] = json_encode($message->getRecipientVariables()); + $parameters['recipient-variables'] = json_encode($variables); } } From b997e9a4579d9b88054173a44c9ecc4c9e63fa58 Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 4 Apr 2014 14:37:56 +0200 Subject: [PATCH 7/9] s/$recipientEmail/$recipient --- src/SlmMail/Service/MailgunService.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/SlmMail/Service/MailgunService.php b/src/SlmMail/Service/MailgunService.php index 26a9d8d..10a1e67 100644 --- a/src/SlmMail/Service/MailgunService.php +++ b/src/SlmMail/Service/MailgunService.php @@ -144,10 +144,11 @@ public function send(Message $message) if (count($message->getRecipientVariables())) { $variables = $message->getRecipientVariables(); - foreach ($variables as $recipientEmail => $variable) { - if (!$message->getTo()->has($recipientEmail)) { + foreach ($variables as $recipient => $variable) { + if (!$message->getTo()->has($recipient)) { throw new \Exception(sprintf( - 'The email "%s" must be added as a receiver before you can add recipient variables', $recipientEmail + 'The email "%s" must be added as a receiver before you can add recipient variables', + $recipient )); } } From f8ead0a339be220a17f37a9859ac91c8b556e33a Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 4 Apr 2014 14:38:26 +0200 Subject: [PATCH 8/9] Throw an SlmMail Runtime exception --- src/SlmMail/Service/MailgunService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SlmMail/Service/MailgunService.php b/src/SlmMail/Service/MailgunService.php index 10a1e67..54f7723 100644 --- a/src/SlmMail/Service/MailgunService.php +++ b/src/SlmMail/Service/MailgunService.php @@ -146,7 +146,7 @@ public function send(Message $message) $variables = $message->getRecipientVariables(); foreach ($variables as $recipient => $variable) { if (!$message->getTo()->has($recipient)) { - throw new \Exception(sprintf( + throw new Exception\RuntimeException(sprintf( 'The email "%s" must be added as a receiver before you can add recipient variables', $recipient )); From 09192d9b9862ff39714314a309941b062b9f8f5d Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 4 Apr 2014 14:40:05 +0200 Subject: [PATCH 9/9] Extract variables outside if() statements --- src/SlmMail/Service/MailgunService.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/SlmMail/Service/MailgunService.php b/src/SlmMail/Service/MailgunService.php index 54f7723..7303cd0 100644 --- a/src/SlmMail/Service/MailgunService.php +++ b/src/SlmMail/Service/MailgunService.php @@ -138,12 +138,14 @@ public function send(Message $message) $parameters[$options[$key]] = $value; } - if (count($message->getTags()) > 0) { - $parameters['o:tag'] = $message->getTags(); + $tags = $message->getTags(); + if (count($tags) > 0) { + $parameters['o:tag'] = $tags; } - if (count($message->getRecipientVariables())) { - $variables = $message->getRecipientVariables(); + $variables = $message->getRecipientVariables(); + if (count($variables)) { + // It is only possible to add variables for recipients that exist in the To: field foreach ($variables as $recipient => $variable) { if (!$message->getTo()->has($recipient)) { throw new Exception\RuntimeException(sprintf(