Nona Blog

How we send Mailgun template emails in Laravel

I’m currently working on a Laravel project we are using Mailgun to handle emails. This is great considering Laravel has built in functionality that works well with Mailgun and other email providers. 

The integration is meant to be generic so that you can plug in multiple email providers so, unfortunately, you lose certain functionality that may be available in an email provider specific integration. 

We really wanted to use Mailgun’s email templates which you don’t seem to be able to use via the usual Laravel email integration unfortunately 🙁 

woman shedding a tear

STEP IN!

Mailgun-PHP

Luckily for us Mailgun-PHP saved us!

Here’s how we started shooting out Mailgun template emails from Laravel.

  1. Install the following:
composer require mailgun/mailgun-php guzzlehttp/guzzle nyholm/psr7

NB: The Mailgun-PHP docs say to install kriswallsmith/buzz but it seems to cause an issue and the solution we found was to instead install guzzle.

2. Use Mailgun-PHP in your file:

use Mailgun\Mailgun;

3. Set up necessary config and functionality:

$mgClient = Mailgun::create(
     env('MAILGUN_SECRET'), // Mailgun API Key
     env('MAILGUN_ENDPOINT'), 
);
$domain = env('MAILGUN_DOMAIN');

4. Set up the params you need:

$params = array(
    'from' => 'mailgunner@50calibre.com',
    'to' => 'person@somewhere.com',
    'subject' => 'Roundhouse  KICK',
    'template' => 'showcase_mailgun_template',
    'v:username' => 'RichyRoundhouse',
    'v:company_name' => 'Bruski',
);

You can get more info on the params available to you here but the important one is template. This allows you to select a template that you have created from Mailgun that corresponds to the name of the template.

example of template in mailgun
mailgun template names MUST match 

Our template name is showcase_mailgun_template in Mailgun so our template should correspond to the same name.

5. Send the damn email :)!

$mgClient->messages()->send($domain, $params);

So all in all our function looks something like this:

<?php
use Mailgun\Mailgun;


class SendMailGunTemplate 
{
    private function sendMail( $username, $company_name) 
    {
      $mgClient = Mailgun::create(
          env('MAILGUN_SECRET'),
          env('MAILGUN_ENDPOINT'),
     );
     $domain = env('MAILGUN_DOMAIN');
     $params = array(
        'from' => 'mailgunner@50calibre.com',
        'to' => 'person@somewhere.com',
        'subject' => 'Roundhouse  KICK',
        'template' => 'showcase_mailgun_template',
        'v:username' => 'RichyRoundhouse',
        'v:company_name' => 'Bruski',
     );
   
     $mgClient->messages()->send($domain, $params);
    }
}

which appears in my inbox like this:

example inbox entry
email from Mailgun

and the email looks like this:

example email

which means this:

author smiling
Happy Developer 🙂

Nona helps funded businesses accelerate their software projects. If you’d like to soundboard your tech project or your development team, book a consultation with us and we can chat through it! 

Dom Bauer

Dom Bauer

Junior Developer - Nona

Add comment