January 3rd, 2022  |  Victor Paredes

Setting Up Mailgun Email with Laravel

Setting up email can be a little confusing sometimes. This article will help clarify one of the most confusing aspects of this task: knowing where to find the information that you need! You can apply these principles across any email service provider, but I tend to use Mailgun for my sites. Let's dive in.

Setting up DNS

First, follow Krister Viirsaar's tutorial to get your DNS set up properly. His tutorial has been sponsored by Mailgun: Mailgun + DigitalOcean DNS Settings.

Looking at config/mail.php

Mail is set up in Laravel's config/mail.php file, but it is best practice to use our .env file to store authentication information. Because .env files are ignored by git (they had better be), we won't be sharing our information with the world if/when we push our codebase to GitHub or similar.

First, for context, let's take a look in our config/mail.php file to see how Laravel uses these variables. No need to edit anything here, just take a look:


'mailers' => [
        // smtp settings
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
        // api settings
        'mailgun' => [
            'transport' => 'mailgun',   // *
            'domain' => env('MAILGUN_DOMAIN'),
            'secret' => env('MAILGUN_SECRET'),

*ensure that the transport for mailgun is present or an error will occur.

Sending Options

There are two options for sending mail through Mailgun, SMTP and via their API. According to Mailgun, using the API is "faster and more flexible," but they basically both do the same thing if you are looking to send mail messages. Either way we will be adding variables to our .env file in order to make these connections. Let's see how to set these up.

Sending via SMTP



# smtp

Where to find SMTP info:

Sending via API



# api

For the api settings, you will need to Add a Sending Key to your domain:

Meet the Author

Victor Paredes is a web and print designer. When not hard at work, you can find him tinkering around with code, music, artwork, and archaic poetic structures. He is based in Portland, Oregon.