Laravel provides simple API to send emails over the SwiftMailer library. This library provides lots of drivers for the email configuration. The driver is like SMTP, Mailgun, Postmark, Amazon SES, and Sendmail. The best thing is the API based drivers are much faster than the SMTP servers. Also, it requiresthe Guzzle Http library to be installed in Laravel. You can configure it in the same way as the SMTP. So, this is not a difficult task to send email using Mailgun. In the previous tutorial, I have configured Gmail SMTP to send email in Laravel 8. Hence, if you have followed that post, then you would have a better understanding of the email drivers. Today, in this post, I will use Laravel Mailgun to send emails in Laravel 8. But before moving to the project, let’s know about the Mailgun.

Contents

  • 1 Mailgun
  • 2 Prerequisites
  • 3 Create Project For Sending Email Using Mailgun
  • 4 Integrate Mailgun with Laravel 8
  • 5 Add and Verify Domains
  • 6 Get SMTP Configuration of Mailgun
  • 7 Configure Mailgun in Laravel 8
  • 8 Create Mail Class in Laravel 8
  • 9 Create a View For Email Template
  • 10 Create a Controller to Send Email Using Mailgun
  • 11 Create a Route
  • 12 Send Email and Check Result
  • 13 Conclusion

Mailgun

Mailgun is an email delivery service that provides the API for developers. You can send any transactional emails from their website. They guarantee to send your emails safely. Also, you can track the click on emails.

So, let’s dive into this post.

Prerequisites

You must have the below tools for creating this Laravel 8 project. We will create a new project in Laravel 8 using the composer.

  • PHP >= 7.3
  • MySQL (version > 5)
  • Apache/Nginx Server
  • VS Code Editor
  • Composer

So, if you are ready then let’s create a project.

Create Project For Sending Email Using Mailgun

At very first, we will have to create a new setup for Laravel 8. Also, you can use your existing project to configure the Mailgun. It depends upon you and your requirements. Here, I will be creating a new project. So, in the terminal or the command prompt I hit the below command.

composer create-project --prefer-dist laravel/laravel send-email

The above command will start creating a new project folder. Then it will install the Laravel 8 dependencies inside the folder.

Create Project in Laravel 8Create Project in Laravel 8

Now, I have the project and it is ready to set up the Mailgun. But before the Mailgun setup, we will require to have the Guzzle Http package in this project. From the Laravel 6 version, this package is inbuilt with the project setup. So, we don’t need to install it.

You can find the package in the composer.json file.

composer.json file

composer.json file

Integrate Mailgun with Laravel 8

Before integrating the Mailgun with Laravel, you will have to sign up with a free account. Visit the official website and just sign up for an account.

Mailgun Homepage

Mailgun Homepage

To sign up for the account just click on Start Sending. You will a sign up page as showing below.

Mailgun Homepage

Mailgun Sign Up Page

After the successful sign up, you will redirect into the dashboard page. You will have to verify your email for activating the account. So, just follow up all the required steps over there.

Mailgun Dashboard

Mailgun Dashboard

After verifying your email, you will have to verify your Mobile number. So, you will have to finish these steps

Add and Verify Domains

In the next step, you will have to verify a domain for sending emails. So, with the free sign up, you will have a default domain setup. Here, I am showing you.

Domain Setup in Mailgun

So, you cannot change the domain for a free account. If you want to set up the custom domain, then you will require to upgrade the account. That will be chargeable.

#laravel 8 #laravel #php #web-development #developer

How to Send Email using Mailgun in Laravel 8
37.90 GEEK