Laravel, PHP, Software Development

Laravel 5.7 Mailgun Configuration To Send Mail

In Laravel, we can set up Mailgun with easy configuration. We only need an account at mailgun.com and some recipient email addresses that we authorized for local testing. The rest is a walk in the park! However, we need to know which Mailgun account or user credentials to use and how.

Laravel Mailgun Configuration

First, we modify the .env file and update some properties relating to the Mailgun configuration. Consider the following changes.

Laravel Mailgun Configuration

These values are available in our mailgun account. For example, we have values for SMTP Hostname, user name, password, API key, and API Base URL.

Laravel Mailgun Configuration

Authorize Recipients in Mailgun

When we create an account at Mailgun, we get a sandbox environment. We can use it for testing, but it requires us to authorize recipients first. Meaning, we can only send mail to the email addresses we registered. This process involves providing the email addresses for Mailgun to send verification emails. Then, the recipient uses have to click the verification links in the emails.

Laravel Mailgun Configuration

For our purpose, we are using a sandbox domain and email recipients we authorized to receive emails. With all these Laravel Mailgun configurations, we can now send emails.

Laravel Mailgun Sample Codes

After setting up Mailgun in Laravel, we can create codes to send a test email to the email we registered with Mailgun. Consider the following PHP codes that use the Mail facade from Laravel.

When we send an email from Laravel, we generally need a view for that email. For example, we have the following view or email template.

Sample Laravel Mailgun Email

We tried the codes and received the following email.

Moreover, the mail has the following details.

Although we used Laravel 5.7 for this post, we can still use a similar Mailgun configuration for later versions unless the Laravel team made a major code revamp.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like