Integrate SendGrid Email tool in laravel * DevRohit Think simplified

Rohit urane
2 min readApr 6, 2022

--

Hi guys,

SendGrid sends email in two ways:- SMTP relay and Web API. SendGrid provides client libraries in many languages. SMTP has many features by default but is hard to set up. It also provides a tracking mail API. SendGrid manages all the technical details, from scaling the infrastructure to ISP outreach and reputation monitoring to services and real-time analytics.

Step by step guide on integration of SendGrid:

If you don’t have an account on the SendGrid platform. you should create one Click Here

You can update the env file with SendGrid credentials. Look at the below code after revising the env file.

MAIL_DRIVER=smtp MAIL_HOST=smtp.sendgrid.net MAIL_PORT=587 MAIL_USERNAME=XXXXXXXXXXX MAIL_PASSWORD=XXXXXXXXXXX

You can run the below command to make a Mail class.

php artisan make:mail WelcomeMail

Above command, create a Mail class on the app/Mail directory. You can modify them according to your requirement.

<?php 
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
public $details;
public function __construct($details)
{
$this->details = $details;
}
public function build()
{
return $this->subject('Welcome To Devrohit')
->view('emails.welcomeMail');
}
}

You can create a blade file inside the emails folder.

resources/views/emails/welcomeMail.blade.php

<html> 
<head>
<title>Welcome to Devrohit</title>
</head>
<body>
<p>Hi {{$details['name']}}, </p>
<p>Thank you for registering our portal. You will get the best knowledge on this platform. </p>
{{ $deails['body']}}
<p>Thank you</p>
</body>
</html>

you can call send method when a user registers to your portal.

$details = [ 
'name'=> $user->username,
'body'=>'You are welcome to our family.'
];
\Mail::to($user->email)->send(new \App\Mail\WelcomeMail($details));

You can add the above code to the register function. Now your user will get welcome mail when they register for your application. You measure business impact on your email campaigns with a/b testing.

Now your user will get welcome mail when they register for your application. Do you want to check which mail tools best for your application? Click Here. I hope that this post (Integrate Sendgrid Email tool in laravel) has clarified how to integrate sendgrid in laravel. If you have any questions, please leave a comment and I will respond as soon as possible.

Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers

Originally published at https://www.devrohit.com on April 6, 2022.

--

--

Rohit urane
Rohit urane

Written by Rohit urane

enthusiastic full stack developer to build application

No responses yet