At a point in your application, you will need to send emails to users. It may also interest you to know that 90% of applications cannot do without the mail functionality, so as a developer it is essential to know how to integrate this functionality into web or mobile applications. In this easy-to-follow guide, I will teach you how to integrate Email sending – How to setup Mailgun for Laravel. You’ll also learn how to generate your Mailgun API key, configure your domain DNS and obtain your SMTP credentials.
Laravel is a great framework and it is loved by many including myself, I am proud to say I am a Lover of the Laravel framework and a preacher for laracast.com (Although I have not earned the badge yet, I will someday).
Definitely, that story about me, Laravel, and Laracast wasn’t funny and you’re in a hurry to get back to your coding. Here is a quick guide.
Configure Laravel to Send Email using Mailgun
Make sure you have Guzzle Http Install (if not already) run the code below
composer require guzzlehttp/guzzle
Step Two: Open .env and configure the following lines
MAIL_PASSWORD=your mail password
What you’re expected to enter into each line
Mailgun listen to the following port 25, 465, 587 but the service highly recommends that you use 587 for your setup.
This refers to the email address you used to create your Mailgun account
Please note that this does not refer to the password for your Mailgun account, You are to generate a special password for your domain and this must be kept confidential. Click here to navigate to how to generate SMTP credentials and mail passwords.
Laravel offers a variety of ways of sending mail and one of them is Mailgun, so on this line, we are instructing Laravel to use Mailgun as our primary source of sending mails.
Here you provide the domain you added to Mailgun e.g. www.yourdomain.com, remember to include www or any other characters used as a subdomain.
Mail provides a unique and secret API key for each user, this information is used to authenticate your request to make sure it’s really you making the request. See the section below on how to generate or locate your API key
The address that will be used to send your email to the client, an address commonly used here is email@example.com, this will tell the user not to reply to this email, whereas if you have a dedicated mail for your company, you should indicate here.
Indicate here the name you want the email to carry. I am sure you’re smart enough to have guessed this right.
Step three: For sanity check, open config/services.php and be sure you have this code there else add it.
'mailgun' => [ 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), 'endpoint' => env('MAILGUN_ENDPOINT', 'api.eu.mailgun.net'), ],
Step four: Create/Login to your account on Mailgun and replace the above credentials mentioned in step two with the ones you’ll be generating.
How to create an account on Mailgun
- Visit: mailgun.com
- click on start sending to begin your registration. You may choose to add payment info during registration or simply uncheck the Add payment info button.
- You may be asked to verify your work email, if so, I trust you’re smart enough to figure it out,
- Add your domain and as well verify it.
How to add and verify your domain on Mailgun
- Click on Sending: This can be found on the off-canvas menu to the left
- From the Dropdown, Click on Domains: The second option in the drop-down list
- Click on Add New Domain. All previously added domains will also be listed here
- Type your domain in the field provided in this format www.yourdomain.com, Mailgun requires that you enter your domain as a subdomain therefore, you may run into errors if entered like this yourdomain.com
- Again Click on Add Domain. Do not bother about the Advanced settings
How to Verify your domain
- Go to your DNS provider, note that if your site is hosted on Digital Ocean or Vultr, that is where you’ll go to configure your DNS otherwise visit your DNS providers to add the following records.
- As instructed on the Mailgun dashboard, create a DNS record with the value provided. The records are categorized into two (i) Records for sending and (ii) Records for tracking.
3. When you’re dong adding all DNS records, click on Verify DNS Settings button.
If done correctly, you should see information printed on the Current Value field else it will be written None found.
These topics might interest you
How to get your Mailgun Api Key (MAILGUN_SECRET)
We need this credential to setup Email Sending and or to set up Mailgun. But before looking at how we can get this value, first, you must understand that this value must be kept secret. When in the wrong hands, many bad things can be done in your domain or company name.
Follow this steps to get your Mailgun Api Key
- Click on Dashboard to go to the home if not already
- To the right of the dashboard, Click on API keys
This will open your private key but hidden in aesteric,
3. Click the Eye next to Private API Key to reveal the code and if you ever need to reset your Private API Key, Click of the double rotating arrows. Copy the code and return to Laravel and insert it in the right place.
3. You will notice you have a Private API key in two places, The second will expire in a certain amount of time, so stick with the first one provided, unless otherwise, you know what you’re doing.
How to obtain your Mailgun SMTP credentials
it is significant to note that for each domain you add to Mailgun, there’s a special SMTP credential for it. That is, each domain has different SMPT settings. Here is how to get the SMTP credentials for your specific domain.
- On the Mailgun dashboard https://app.mailgun.com/app/dashboard scroll down to the section where your domains are listed
- Click on domain settings next to the domain you wish to obtain SMTP settings for
- On the new page click on SMTP credentials, this is the third tab to the right.
- To get your domain password, click on Reset Password
- Confirm your password reset by clicking Reset Password in the pop-up.
- Your SMTP credential’s new password will be displayed on the top-right corner within a green pop-up. Save this password by clicking on the copy button and pasting it into a safe location, as it will not be revealed again.
Now you know how to integrate Mailgun into your Laravel project. Email Sending – How to setup Mailgun for Laravel 8. If you have any problem while trying these steps out, do not hesitate to leave a comment below and I will be happy to help.