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 requires the 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.
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 Auth Using Jetstream and Intertia js 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.
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.
To sign up for the account just click on Start Sending. You will a sign up page as showing below.
How to Create Auth with Jetstream and Livewire in Laravel 8
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.
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.
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 Ajax CRUD Application Tutorial For Beginners
Get SMTP Configuration of Mailgun
For email configuration in Laravel, you will require the username, password, SMTP settings, etc. In the dashboard of Mailgun, you will find these credentials inside the Sending->Domains option on the menu.
You will find the overview as showing below. This is based upon the selected domain from the list.
There are the two options of integrating the Mailgun with the application.
- API and
- SMTP
You can integrate Mailgun using API too. But, we will use the SMTP, because we have the application and we required only SMTP settings.
So, just select the SMTP option in the above list. Then you will have the settings of SMTP along with the username and password.
So, just copy the credentials, and move to the Laravel 8 project. We will put up these settings for the integration.
Laravel 8 Multiple Images Upload with Validation
Configure Mailgun in Laravel 8
For the configuration of the Mailgun driver in Laravel 8, navigate to the .env file.
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME={{ USERNAME_BY_MAILGUN }}
MAIL_PASSWORD={{ PASSWORD_BY_MAILGUN }}
MAIL_ENCRYPTION=tls
MAILGUN_DOMAIN={{ DOMAIN_CREATED_IN_MAILGUN }}
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
You can check the services file for the email available in the config/services.php.
Here the Mailgun service is configured. Now, take a look at the mail configuration. You will find the mail.php inside the config folder.
So, in the mail.php file, you can see Laravel supports the various mail drivers. After finishing the configuration, let’s create a mail class. So, that we can send emails.
How to Upload Image in Laravel 8 with Validation
Create Mail Class in Laravel 8
Laravel provides the mail class for operating the email functionality. You can create the email class using the artisan command.
php artisan make:mail FirstEmail
Here, I have created the Mail class for sending email using the Mailgun.
After creating the mail class, let’s add the below snippet.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class FirstEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from("admin@programmingfields.com")->view('email-template');
}
}
In the above mail class, I have passed the from email and a view. The view will be attached with the email.
Create a View For Email Template
We have put the view inside the mail class. So, it will require to have a view with the template design. So, we will create a view with the name email-template.php.
<!doctype html>
<html lang="en">
<head>
<title>Send Email in Laravel 8 Using Mailgun | Programming Fields</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xl-6 col-lg-6 col-sm-12 m-auto">
<h3> Send Email in Laravel 8 Using Mailgun </h3>
<p> Hey, </p>
<p> Welcome to Programming Fields </p>
<p> This is a basic demo for sending email in Laravel 8 using Mailgun. </p>
<p> I hope you will enjoy this post. </p>
<br/>
<br/>
<p> Best Regards</p>
<p> Team, Programming Fields </p>
</div>
</div>
</div>
</body>
</html>
In the above code, I have passed a simply message with very basic layout.
So, the mail class and the template is ready to send an email. Now, it will require to have the call from the controller. Inside the controller, we’ll write the method to send the email.
So, let’s create a controller first.
Laravel 8 Form Validation Tutorial For Beginners
Create a Controller to Send Email Using Mailgun
We will require to create a controller for sending emails. So, let’s create a controller.
php artisan make:controller EmailController
Now, we have the controller too. So, let’s add the function inside this controller.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\FirstEmail;
class EmailController extends Controller
{
public function sendEmail() {
$to_email = "umesh.rana0269@gmail.com";
Mail::to($to_email)->send(new FirstEmail);
if(Mail::failures() != 0) {
return "<p> Success! Your E-mail has been sent.</p>";
}
else {
return "<p> Failed! Your E-mail has not sent.</p>";
}
}
}
In the controller, I have included the Mail class as a namespace along with the mail facades.
Inside the function, I have passed a static email address to send the email. You can pass the data dynamically from a view. It will send the email and return a success message if the email has been sent.
Laravel 8 CRUD Application Tutorial From Scratch
Create a Route
For the above function, create a route to have a call. This route will be added in the web.php file.
<?php
use App\Http\Controllers\EmailController;
use Illuminate\Support\Facades\Route;
Route::get("send-email", [EmailController::class, "sendEmail"]);
Now, our application is ready to send emails. Let’s check the result.
Send Email and Check Result
Run the application to check the result. We have set the static message from the view that is created for the template.
In the response, you will get the success message as showing below.
Now, check the email inbox for the email. Here, I have received the email.
You can check the delivery status of the sent email in the Mailgun dashboard. Here, is the result, I have sent 6 emails and all emails are delivered. That’s a cool feature of the email. You can track the email and clicks as well.
How to Implement Column Filter in Laravel 7 Yajra Datatable
Conclusion
We have integrated Mailgun in Laravel 8 for sending emails. We created a free account in Mailgun. After the account activation, it registered a free domain for us. Then using that domain settings we are able to send emails. Mailgun provides the facility to track sent emails. Also, you can track the click on the email. The delivery response is faster than other SMTP server. You can check the delivery, bounce back of the emails in the dashboard. So, I hope you will enjoy this post and find it helpful.
Leave a Reply