How To Send Emails In Laravel Using Mailgun

7 Step Guide to Send Email with Laravel Using Mailgun

Tara Prasad Routray
Enlear Academy

--

Sending emails has become a crucial part of application development. Whether building a website or a mobile application, you need to send emails to your users or customers. For example, if you're building an e-commerce store, you must be sending transactional emails for successful registrations, account verification, resetting passwords, sending invoices, sending order updates, and much more.

Laravel framework makes this task easy for us, as Mailgun comes integrated by default. So have a look at what you're going to explore ahead.

  1. Introduction to Mailgun
  2. Create an Account at Mailgun
  3. Configure Mailgun in Laravel
  4. Create Mail Class in Laravel
  5. Create a View For Email Template
  6. Create a Controller to Send Email
  7. Create a Route
  8. Send Email

Introduction to Mailgun

Mailgun is one of the most popular email API solutions built for developers. You can send transactional or bulk emails effortlessly with Mailgun's SMTP and HTTP API.

Since Mailgun exposes both an HTTP API and an SMTP endpoint, you can choose to send the way you like. Both have their advantages. SMTP is simple to set up and often plug and play, but API is much more stable.

You can visit the official website and register for a free account. After successful registration, you will get 5000 free emails to send. If you need more, then you must opt for a paid plan. Let's have a look at how to register a free account at Mailgun in the next section.

Step 1 — Create an Account at Mailgun

Visit the official website by clicking on the link below.

Press the "Get Started" button displayed on the website's banner, and you will be redirected to the signup page.

Once you are on the signup page, you will see a similar form, as shown in the following screenshot. Again, fill in all the required fields and proceed to create an account.

After successful registration, you will get redirected to the Mailgun's dashboard page. Then, click on the "Overview" link from the left sidebar under the section labeled "Sending." On this page, you will see two ways to send emails, i.e., API and SMTP.

If you click on the API box and then press the PHP tab, a new section will appear, where the following details are available for you:

a) API Key
b) API base URL

You will see a similar section as presented in the following screenshot.

If you click on the SMTP box, a new section will appear, where the following details are available for you:

a) SMTP hostname
b) Port
c) Username
d) Default password

You will see a similar section as presented in the following screenshot.

If you want to test the emailing feature, you can add a recipient email to the "Authorized Recipients" tab available on the "Overview" page. For example, you can refer to the following screenshot.

Note: The email feature is restricted to authorized recipients for free accounts without any credit card details.

Since you have successfully set up your account and assembled all the essential details, you will learn how to enter those details in your Laravel application and start sending emails in the next section.

Step 2 — Configure Mailgun in Laravel

For the configuration of the Mailgun driver in your Laravel application, navigate to the .env file, and update the following keys to include Mailgun SMTP details.

Now the Mailgun service has been configured.

Step 3 — Create Mail Class in Laravel

Laravel provides a mail class for working with emails. You can create an email class by running the following artisan command.

php artisan make:mail HelloEmail

After running the above command, a new folder named "Mail" will be created under the "app" folder. Inside the "Mail" folder, a new file will be created with "HelloEmail.php."

See the following code snippet to update your "HelloEmail.php" file.

Step 4— Create a View For Email Template

Since you have added the name of the view inside the "HelloEmail.php," you should create a view file inside the folder "resources/views" with the name "email-template.blade.php." Then, refer to the following code snippet to update your email template.

Step 5— Create a Controller to Send Email

Now you have to create a controller for sending emails. Run the following command to create a controller.

php artisan make:controller EmailController

After running the above command, a new file will be created with "EmailController.php" under the "app/Http/Controllers" folder. Now, since the controller has been created, let's add a function inside this controller. Refer to the following code snippet to update your controller file.

Step 6 — Add a Route

You need to add a route inside the file "web.php" which is available under the "routes" folder. For example, refer to the following code snippet.

Step 7 — Send Email

Now, open your web browser and access the following URL "/send-email" by navigating into your web application. After accessing the above URL, you will see a success message on your screen. You can check your Mail inbox to see the new email.

Kudos! You have successfully configured Mailgun with Laravel for sending emails. If you have any questions, feel free to ask in the comment section.

If you enjoyed reading this post and have learnt something new, then please give a clap, share it with your friends, and follow me to get updates for my upcoming posts. You can connect with me at LinkedIn.

--

--