Laravel email works over a popular mailer library that is the SwiftMailer. This is an inbuilt library in Laravel. It allows us to integrate several mail drivers for sending emails. You can use SMTP and API based drivers to send emails in Laravel. I already shared tutorials for integrating the SMTP like Gmail and Mailgun. But, today, in this post, I will show you the integration of Twilio Sendgrid with Laravel 8. The SendGrid is an email service. By using the Sendgrid, you can send emails, marketing campaigns such as automation, etc. Also, it provides the features to track sent emails, activities on email like open, click, etc. So, let’s create a new project in Laravel 8 and integrate this SendGrid SMTP to send emails.
Prerequisites
Before creating this project, please make sure you have the below tools in your system. We are going to create a Laravel 8 project. Hence, you must have the specified version of these tools.
- PHP >= 7.3
- MySQL (version > 5)
- Apache/Nginx Server
- VS Code Editor
- Composer
Create Laravel 8 Project For SendGrid Integration
For creating this project in Laravel 8, I will be using the composer. So, open the terminal and hit the below command.
composer create-project --prefer-dist laravel/laravel send-email
Here, the project is creating. After finishing the setup we will move to the next step.
How to Send Email Using Mailgun in Laravel 8
Now, we have the project setup. So, let’s get the SendGrid SMTP credentials.
Sign Up For a New Account in SendGrid
You will have to go to the official website of the SendGrid. That is https://sendgrid.com Read out the features and the documentation for more details. This is the homepage that I am showing you here.
Then create a free account by clicking on the Sign up for free. Here, this is the signup page. Fill up the details and create an account.
How to Send Email in Laravel 8 Using Gmail SMTP
After successful signup, you will redirect to the dashboard page. Take a look at the dashboard options, it will look as showing below.
In the next step, you will have to create a sender identity. This sender identity will be used as a sender email. So, let’s create it.
Create Auth Using Jetstream and Intertia js in Laravel 8
Create a Sender For Sending Emails
The sender will require to send email with an identity. For creating a sender you can go through the dashboard. Otherwise, you can go through the sidebar menu.
Under the Sender Authentication menu, you will have the option to create a Sender. So, let’s create it.
After creating the sender, you will have to verify it. When your sender has verified that means you are ready to send emails.
Laravel 8 Ajax CRUD Application Tutorial For Beginners
Integrate SendGrid in Laravel 8
Firstly, you will have to generate the API key. So, that you can configure it with the Laravel 8 project. Inside the Settings, you can find the API Keys option.
You can integrate SendGrid in two ways. That is using the Web API or SMTP.
Here, I will integrate with the SMTP. So choose the SMTP option and then create an API Key. It will generate the SMTP credentials for you.
After creating the API Key, the application is ready to integrate with Laravel.
Copy the SMTP credentials and move back to the Laravel 8 application.
How to Create Auth with Jetstream and Livewire in Laravel 8
Configure SendGrid SMTP in Laravel 8
For the email configuration in Laravel, navigate to the .env file.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME={{ SENDGRID_USERNAME }}
MAIL_PASSWORD={{ SENDGRID_PASSWORD }}
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
So, we have integrated the Twilio SendGrid SMTP in Laravel 8. Now, let’s create a Mail class in the project.
Create Mail Class in Laravel 8
We will require to have a Mail class for operating the emails. So, create it by using the artisan command.
php artisan make:mail FirstEmail
The above command will create a mail class. You can find this file in app/Mail.
Now, we have the mail class, and it will require a view for a template. Add the below snippet there. Please notice one thing here, you will have to set the from email that should be the same as the sender you have created.
<?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("umesh.rana0269@gmail.com")->view('email-template');
}
}
Also, I have returned this mail function to a view. So, we will have to create a view. We will be using a static very basic email template.
Laravel 8 Multiple Images Upload with Validation
Create a View For Email Template
I am going to create a view with the name email-template.blade.php. After creating the view, add the below snippet.
<!doctype html>
<html lang="en">
<head>
<title>Send Email in Laravel 8 Using SendGrid | 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 SendGrid </h3>
<p> Hey, </p>
<p> Welcome to Programming Fields </p>
<p> This is a basic demo for sending email in Laravel 8 using SendGrid. </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>
We have the view ready. Now, we need a controller for calling the mail class.
Laravel 8 Form Validation Tutorial For Beginners
Create a Controller to Send Email Using SendGrid
Open the terminal and create a controller as showing below.
php artisan make:controller EmailController
After creating the controller, let’s add few lines of code inside it.
<?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 = "rajumesh52@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>";
}
}
}
At last, we will require to create a route for this function.
Create a Route in Laravel 8
For this web application, we will have to add the route in the web.php file.
<?php
use App\Http\Controllers\EmailController;
use Illuminate\Support\Facades\Route;
Route::get("send-email", [EmailController::class, "sendEmail"]);
Finally, we have completed all the steps. Now, our application is ready to send the emails using the SendGrid. So, let’s check it out.
Laravel 8 CRUD Application Tutorial From Scratch
Send Email Using SendGrid
Now, run the application to check the result. Hit the above URL and wait for the response.
At the result, I got the success response as showing below.
Now, let’s check for the email in the inbox. And yes, I have got the email in the inbox. You can see it is sent via the sendgrid.net.
Conclusion
Sending emails using SendGrid SMTP is pretty easier than the API based integration. We have easily configured and integrated SendGrid SMTP with Laravel 8 application. This email delivery service provides very awesome features. You can have the track of the emails. Also, you can track the clicks, and the open activity of the emails. So, I hope you can implement it in the Laravel applications. If you stuck in this integration steps then please let me know in the comment section. I will try to resolve your query. Thank you.
Leave a Reply