blog banner alt

How to Send and Receive Email in Laravel 5 Using Mailgun

Last updated May 7, 2017


Have you ever wondered “how exactly do some websites send emails to users automatically?”

For example, when you sign up to a newsletter, and you receive a thank you email.

If you are using Laravel, the process of integrating an API based mail drivers into your application is very easy.

In this tutorial, we’ll going to learn how to configure your Laravel application to send Emails through Mailgun, and we’ll also learn how to get an Email address of your own domain!

Let’s get started!

 

Step 1: Prerequisite

First things first; API mail drivers need the Guzzle HTTP library.

You can either install it with a composer command, or put the dependency in your composer.json file.

I recommend the latter, because if you were to migrate your application, or if you have different environments for development and production, it’s MUCH easier to manage the dependancies. And surely, you would not want to have the whole vendor directory as part of your version control.

Here is how:

composer.json
"require": {
        ….
        "guzzlehttp/guzzle": "^6.2"
}

Then you want to run a composer update in your command line:

composer update

 

Step 2: Get the Mailgun API

Sign up to Mailgun

If you don’t yet have a Mailgun account, it’s easy to sign up.

Mailgun signup Page
Figure 1: Mailgun signup Page

Important Note: Make sure to add your payment info, if you don’t, you can only send emails to authorized recipients, which means you will need to add their email manually one by one! Don’t worry, You won’t get charged if you send under 10,000 emails per month.

 

Add Your Domain

Mailgun add domain Page
Figure 2: Add Domain Page

I recommend using your root domain for this. Maligun’s recommendation to use a subdomain is a bit misleading. You can send emails through your root domain that way, but you can only receive your email with your subdomain.

So if you are using subdomain.mymail.com, and a user responds to your email from [email protected], you will never receive it.

After entering your domain, you’ll be redirect to a page with steps to verify your domain, it should be pretty straight forward to follow the steps.

Mailgun DNS verification Page
Figure 3: Verify Your DNS

Once you’ve updated your DNS records following the steps, click through the “Continue to Domain Overview”, you should see a page like the this:

Mailgun API Info
Figure 4: API Info

You might not be able to see the “State” being Active right way, as the time for verification is dependent on how fast your DNS server can update your records.

 

Here is how you can speed up the process.

On the same page, if you scroll down, you should see Domain Verification & DNS, click on the text:

Refresh

Then click on the “Check DNS Records Now”, keep refreshing it a few times to see if your DNS has updated.

Now we just need to put this information into our Laravel application in order to send emails!

 

Step 3: Configure Laravel Application

This step is very simple, just a few files we need edit and that’s it!

In your config/services.php file, add the following:

'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
    ],

You will mostly find the mailgun configuration already there, and if that’s the case you can leave the file as it is.

 

Next, you will need to go to your .env file, and replace the “MAIL_USERNAME”, “MAIL_PASSWORD”, “MAILGUN_DOMAIN” AND “MAILGUN_SECRET” with your own.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=2525
[email protected]
MAIL_PASSWORD=yourmailgunpassword
MAIL_ENCRYPTION=null

MAILGUN_DOMAIN=yourdomain.com
MAILGUN_SECRET=key-YourMailGunSecret

Last step, in the same file, you will need to also add, and replace the value with your own:

MAIL_FROM_ADDRESS: [email protected]
MAIL_FROM_NAME: John Doe

 

We are all set!

Now we can test our email by creating a new route in routes/web.php file and have a mail to send to ourselves, like so:

Route::get("/email", function() {
   Mail::raw('Now I know how to send emails with Laravel', function($message)
	{
		$message->subject('Hi There!!');
		$message->from(config('mail.from.address'), config("app.name"));
		$message->to('[email protected]');
	});
});

Then visit this page, and check your email, you should be getting an email from your own application!

 

With that done, there is still one problem. Our application can now send emails, but we are not able to receive emails, or sending emails with our custom domain.

The next step, which is a bonus to this article, will tell you all about it!

 

BONUS: Receiving Email and Send Email from your own domain!

Set Mailgun Forward Address

Now we need to go back to Mailgun site.

Click Routes from the top navigation bar, then Create Routes.

You should see a page like this:

mailgun forward routes
Figure 5: Mailgun Forward Routes Page

There are only a few field you need to fill:

  • Expression Type: Match Recipient;
  • Recipient: .*@yourdomain.com (this way, you can send or receive email from any email address you want!);
  • Action: tick forward, and fill in your Gmail address;

 

Configure Your Gmail Account

On your Gmail page, click the gear icon on the top left, then go to settings.

Then you want to go to Account and Import:

gmail settings page
Figure 6: Gmail Settings Page

Select Add another email address. Filling in your name and email address, you should see this:

gmail add custom domain
Figure 7: Add Custom Domain to Gmail Account

Recall the information we used to configure our Laravel application, we need to feed Gmail the same information.

  • The SMTP server should be smtp.mailgun.org;
  • You can leave the port number as 587
  • Username should be your mailgun login, something like: [email protected]
  • Password should be the random string from the “Default Password” section.

Click Add count, then shortly you should be getting an Email from Gmail with a confirmation code!

 

You Are all done! Now you can send email through Laravel, and also receiving emails, and you can also email your friends with your new email address. Try it out!