Email Sending – How to setup Mailgun for Laravel 8

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

Step One:

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_PORT=587
MAIL_USERNAME=stechmaxservice@gmail.com
MAIL_PASSWORD=your mail password
MAIL_ENCRYPTION=tls

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=www.yoursite.com
MAILGUN_SECRET=key-34b3g4389979*****
MAIL_FROM_ADDRESS=donotreply@yourdomain.com
MAIL_FROM_NAME=Your Domain

What you’re expected to enter into each line

MAIL_PORT:

Mailgun listen to the following port 25, 465, 587 but the service highly recommends that you use 587 for your setup.
MAIL_USERNAME

This refers to the email address you used to create your Mailgun account
MAIL_PASSWORD

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.

MAIL_DRIVER

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.
MAILGUN_DOMAIN

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.

MAILGUN_SECRET

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

MAIL_FROM_ADDRESS

The address that will be used to send your email to the client, an address commonly used here is noreply@yourdomain.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.

MAIL_FROM_NAME

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

  1. Visit: mailgun.com
  2. 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.
  3. You may be asked to verify your work email, if so, I trust you’re smart enough to figure it out,
  4. Add your domain and as well verify it.

How to add and verify your domain on Mailgun

  1. Click on Sending: This can be found on the off-canvas menu to the left
  2. From the Dropdown, Click on Domains: The second option in the drop-down list
  3. Click on Add New Domain. All previously added domains will also be listed here
  4. 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
  5. Again Click on Add Domain. Do not bother about the Advanced settings

How to Verify your domain

  1. 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.
  2. 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.
Add dns records for sending

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.

bb

These topics might interest you

Best web hosting services of 2021: Top host providers for your website

“Signal” – A Better Alternative for WhatsApp?

5 BEST ONLINE DATING APPS IN 2021!!!

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

  1. Click on Dashboard to go to the home if not already
  2. To the right of the dashboard, Click on API keys
Mailgun api key sleettech

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.

Mailgun SMTP Credentials
  • 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.

Conclusion:

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.

1 thought on “Email Sending – How to setup Mailgun for Laravel 8”

Leave a Comment

%d bloggers like this: