Mailgun with Laravel Mailables

Introduction

The PHP Laravel framework is one of the most popular PHP frameworks for large and small applications alike. It’s one of my personal favorites, and using it is so easy. In this tutorial, I’ll explain the steps to take to get Mailgun working nicely with your application. This tutorial assumes you’ve already got a Laravel app setup and an active Mailgun account.

For those that aren’t familiar with Mailgun, it’s an incredibly easy API that lets you send emails from your applications. They have SDKs in every relevant language, and a publicly exposed API.

Driver Setup

Laravel provides a clean, easy API over SwiftMailer that includes drivers for various mail services, including Mailgun. This does require that you have Guzzle installed in your project.

composer require guzzlehttp/guzzle

After you have added Guzzle to your project, make sure to set the driver option to mailgun in the config/mail.php file. Then, add your Mailgun domain and secret to config/services.php

  'mailgun' => [
    'domain' => 'your-mailgun-domain',
    'secret' => 'your-mailgun-key',
  ]

Add A Mailable

Each type of email you send from your Laravel application will have a mailable class in the app/mail directory. Thankfully, artisan will help us make this:

php artisan make:mail HelloMail

Configuring Mailables

To configure mailables, you’ll want to use the build method. This method is what Laravel will use to configure your email. From here, you can access the other associated methods like from, subject, view, and attach. For example, if we wanted to configure our Mailable to send from test@abinaryworld.com, we would configure the mailable like this:

  public function build() {
      return $this->from('test@abinaryworld.com')
                  ->view('emails.hello.mail);
  }

This snippet sets the from address and returns the emails blade template emails.hello.mail. This template is rendered to produce the email that the receiver will actually see.

You can, of course, specify a global from address in your config/mail.php file.

  'from' => ['address' => 'test@abinaryworld.com', 'name' => 'A Binary World'],

Add Your Email Template

Typically, your Laravel templates will use the Blade templating enginer. This makes life a lot easier. Create a new view at /emails/hello.mail.blade.php. For the sake of this tutrial, we won’t make anything too fancy. Just a simple “Hello World” message.

  <h3>Hello, world!</h3>

Send Your Mail

Now that your mailable is configured and your template is available, we should send it! From your controller, just invoke the send method referencing the mailable we just created. A simple Hello World controller that sends this mail could look somehting like this:


namespace App\Http\Controllers; use App\Order; use Illuminate\Http\Request; use Illuminate\Support\Facades\Mail; use App\Http\Controllers\Controller; class HelloWorldController extends Controller { public function sendEmail(Request $request) { Mail::to($request->user())->send(new HelloWorld(); } }

This calls the static Mail object’s to function and specifies the user that should receive the HelloWorld mail. It then invokes the send method to actually send the mail through the driver (in this case Mailgun) that you have setup for your application.

Conclusion

That’s it! It really is as simple as that to send Mailgun emails through your Laravel application. In a later post, I’ll explain how to pass dynamic content (like a user’s name, and the current date, etc) to your Mailable.

If you found this tutorial useful, please share and let me know below! Question? Requests? Also put them below

Your email address will not be published. Required fields are marked *

*