Sendgrid Provides multiple features to send the email like bulk email, marketing email , APIs, Delivery reports, Templates and many more. In that way send gird is much interesting and capable of performing many tasks related to marketing and bulk mailing. So in this article we will learn to use Send Mail in Laravel Through Through Sendgrid by enabling APIs and SMTP details. Email is very common operation of any website like sending an email to users after registration, Send newsletters to users and many more. Laravel provides multiple drivers or services to send mail from different different providers.
Laravel gives wide range of options to send emails. We can queue an email for send on specific time using the queue drivers.
Following are the supported methods of Laravel mail
- Amazon SES
- MailGun
- Sendmail
- SparkPost
- SMTP
These are the supported drivers in laravel but now we will use sendgrid with SMTP
Laravel Mailing supports blade views based templating system so you can easily configure the mail body layout same as other pages.
It also gives a easy to implements mail with attachment feature so let’s start with example
Step 1: Create a laravel project
First step is to create the Laravel 8 project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations
composer create-project laravel/laravel example-app
Step 2 : Register on Sendgrid and Configure the mail
First go to https://sendgrid.com and
- click on signup button
- Enter all details
- Wait for email confirmation from Sendgrid
We have two options to configure the mail from .env file or config/mail.php file and the recommended way to configure it is from .enc file
SendGrid SMTP
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=sendgridusername
MAIL_PASSWORD=sendgridpassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=fromaddress
MAIL_FROM_NAME="${APP_NAME}"
If the website is secure ssl then change ‘encryption’ => ‘tls’
to ’encryption’ => ‘ssl’
and ‘port’ => 465
Step 3 : Generate Mailable Class
In laravel 8 or 9 every mail is implements mailable class thus first we need to create a mail class as follow
php artisan make:mail TestMail
This will create a class in following location App\Mail\TestMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class TestMail 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->view('test');
}
}
Update the code as below
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class TestMail extends Mailable
{
use Queueable, SerializesModels;
private $user;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($user)
{
$this->user=$user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('info@readerstacks.com')
->subject('Mail from readerstacks.com')
->view('emails.test',["user"=>$this->user,"title"=>"Register"]);
}
}
Step 4 : Create View for Mail
Create a simple view for mail body in resources/views/emails/test.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Readerstacks.com</title>
</head>
<body>
<h1>Hello {{ $user['name'] }}</h1>
<p>{{ $user['body'] }}</p>
<p>Thanks to visit us.</p>
</body>
</html>
Step 5 : Create Route and Send Mail
Now, create a route and call Mail methods in inline function as below
<?php
use Illuminate\Support\Facades\Route;
Route::get('/send-mail',function(){
$user = [
'name' => 'Readerstacks',
'body' => 'This is simple mail from Readerstacks'
];
\Mail::to('receiver_email@domain.com')->send(new \App\Mail\TestMail($user));
});
Also Read : How to Send Mail in Laravel Through Gmail SMTP ?