Skip to content

Commit

Permalink
Merge pull request #69 from juriansluiman/hotfix/mailgun-bulk-adjustm…
Browse files Browse the repository at this point in the history
…ents

Hotfix/mailgun bulk adjustments
  • Loading branch information
bakura10 committed Apr 4, 2014
2 parents 33a7e37 + 09192d9 commit a4c83f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
3 changes: 2 additions & 1 deletion docs/Mailgun.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ $message->addTo('[email protected]');
$message->setSubject("Hi %recipient.name%");
$message->setBody("Hi, activate your account by clicking on http://mailgun.com/activate/%recipient.key%");

// Set all variables for [email protected]
$message->setRecipientVariables('[email protected]', array('name' => 'Demo', 'key' => 'key1'));

// Or add one:
// Or add one by one for [email protected]:
$message->addRecipientVariable('[email protected]', 'name', 'Demo');
$message->addRecipientVariable('[email protected]', 'key', 'key2');
```
Expand Down
19 changes: 10 additions & 9 deletions src/SlmMail/Mail/Message/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);

/**
Expand Down Expand Up @@ -188,16 +188,17 @@ public function getValidOptions()
*/
public function setRecipientVariables($recipient, array $variables)
{
$this->recipientVariables[$recipient] = $variables;
$this->recipientVariables[(string) $recipient] = $variables;
}

/**
* @param string $recipient
* @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;
}

/**
Expand Down
32 changes: 18 additions & 14 deletions src/SlmMail/Service/MailgunService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -150,8 +138,24 @@ 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;
}

$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(
'The email "%s" must be added as a receiver before you can add recipient variables',
$recipient
));
}
}

$parameters['recipient-variables'] = json_encode($variables);
}
}

Expand Down

0 comments on commit a4c83f5

Please sign in to comment.