2

I am using Mailgun to handle email in my Laravel 5.1 application. There are times when I need to email a large number of users at once, such as internal group emails and alert notices. So far, I have created an array of recipient email addresses, sent the email to a webmaster type address, and included the end recipients in BCC:

$recipients = [];

    foreach (User::emailRecipients()->get() as $user)
    {
        $recipients[] = $user->email;
    }

    $data['message'] = "Hello World";
    $data['recipients'] = $recipients;

    Mail::send('emails.group-email', $data, function($message) use ($data) {
        $message->to('support@demo.com')
            ->bcc($data['recipients'])
            ->subject('Test message');
    });

While this works, it's not ideal. For starters, I cannot reference anything unique to each recipient within the email (such as a custom unsubscribe link). I cannot use some sort of mailing list on an email provider because the recipients will never be the same, it all depends on several factors.

The next logical step is to iterate over each email address and use Mail::send on each iteration. But would this cause any performance issues or API restrictions with Mailgun? It's possible that one email could be sent to approximately 200 recipients.

Laurence
  • 58,936
  • 21
  • 171
  • 212
NightMICU
  • 9,000
  • 30
  • 89
  • 121
  • Mailgun has an API which you could use to create a mailing list on the fly. https://documentation.mailgun.com/api-mailinglists.html – Chris Jun 11 '15 at 17:24
  • @Chris see update and let me know what you think. I'd rather not use mailing lists if possible.. – NightMICU Jun 11 '15 at 17:46
  • 1
    Looks good to me. I've created a Mailgun package for Laravel which offers these Mailgun specific features in a more Laravel like syntax. https://github.com/Bogardo/Mailgun. Unfortunately I haven't had time to finish updating the package to support Laravel 5 but I think I should be able to finish it somewhere this month. – Chris Jun 11 '15 at 17:49
  • 1
    This [issue](https://github.com/Bogardo/Mailgun/issues/31#issuecomment-64841511) applies to your method, for reference. – Chris Jun 11 '15 at 17:54
  • 1
    Dude this is so great, I wish I had switched to just using Mailgun's API before. 100% better. – NightMICU Jun 11 '15 at 17:54
  • Be aware, as I mentioned before, the bogardo/mailgun package does not support Laravel 5 (yet). Though you could just use the [Mailgun API](https://github.com/mailgun/mailgun-php) library on its own. – Chris Jun 11 '15 at 17:56
  • @Chris yep, got it.. looking forward to using that in the future! – NightMICU Jun 11 '15 at 18:08

1 Answers1

3

Rather than using Laravel's built in Mail, I elected to use Mailgun's API (specifically batch sending) directly - mailgun/mailgun-php

$mailgun = new Mailgun('API-KEY');
$recipientVariables = [
        "someone@demo.com" => ['name' => 'Benny'],
        "someoneelse@demo.com" => ['name' => 'James']
    ];
$mailgun->sendMessage('demo.org', [
        "from" => 'support@demo.org',
        "to" => 'someone@demo.com, someoneelse@demo.com',
        "subject" => 'Cool Email',
        "html" => $content, // HTML from Blade template
        "text" => "Plain text message",
        "recipient-variables" => json_encode($recipientVariables)
    ]);

This also allows me to access unique recipient variables within my email template, like so:

<h1>Hello, %recipient.name%</h1>

I was already passing my Blade template through a CSS inliner, which turns it into plain HTML, so this works perfectly.

NightMICU
  • 9,000
  • 30
  • 89
  • 121