Skip to content

Commit

Permalink
Merge pull request #68 from Nikolajpetersen/master
Browse files Browse the repository at this point in the history
MailGun - Send Bulk
  • Loading branch information
bakura10 committed Apr 4, 2014
2 parents 1e85972 + c06b6d1 commit 33a7e37
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
20 changes: 20 additions & 0 deletions docs/Mailgun.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ $message->setTags(array('registration-mail', 'my-designed-mail'));
$message->addTag('registration-mail');
```

### Batch sending

Batch sending allows you to send an e-mail to a group from one single API call. By using recipient variables,
you can define custom parameters for each recipient.

```php
$message = new \SlmMail\Mail\Message\Mailgun();
$message->addTo('[email protected]');
$message->addTo('[email protected]');

$message->setSubject("Hi %recipient.name%");
$message->setBody("Hi, activate your account by clicking on http://mailgun.com/activate/%recipient.key%");

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

// Or add one:
$message->addRecipientVariable('[email protected]', 'name', 'Demo');
$message->addRecipientVariable('[email protected]', 'key', 'key2');
```

### Use service locator

If you have access to the service locator, you can retrieve the Mailgun transport:
Expand Down
43 changes: 37 additions & 6 deletions src/SlmMail/Mail/Message/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,19 @@ 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',
);

/**
* @var array
*/
protected $recipientVariables = array();

/**
* Get all tags for this message
*
Expand Down Expand Up @@ -176,4 +181,30 @@ public function getValidOptions()
{
return $this->validOptions;
}

/**
* @param string $recipient
* @param array $variables
*/
public function setRecipientVariables($recipient, array $variables)
{
$this->recipientVariables[$recipient] = $variables;
}

/**
* @param string $recipient
* @param string $key
* @param string $value
*/
public function addRecipientVariable($recipient, $key, $value) {
$this->recipientVariables[$recipient][$key] = $value;
}

/**
* @return array
*/
public function getRecipientVariables()
{
return $this->recipientVariables;
}
}
12 changes: 12 additions & 0 deletions src/SlmMail/Service/MailgunService.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ 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 Down

0 comments on commit 33a7e37

Please sign in to comment.