How to Send Mail using Sendgrid in Laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i will show you How to Send Mail using Sendgrid in Laravel? We are know that sendgrid is very popular API for send email from our any laravel application. It is very fast for sending mail and also we can track the send mail. By using tracking the email is a very important feature of Sendgrid api and we can also see by using this how much users are open your mail, or click on your mail too. Now in this post i would like to show you how to setting of Sendgrid in our laravel application. So in this example we can learn how to send it simple mail by using Sendgrid site. If we are use the Sendgrid for sending the email then we can also save the loading time and we can get the mail fast.

So at first we need to add configuration on mail. Here i will add my gmail account on configuration file. So at first need to open .env file and peast the bellow code:

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=sendgridusername
MAIL_PASSWORD=sendgridpassword

Okay, now we have to add the secret and domain of sendgrid api configuration. So at first need to create new account in sendgrid.com. If you don’t have any account before. After the registration need to active our sendgrid account and set the username and password

Now hope we are ready for sending mail for test.

So first create the test route for email sending.

app/Http/routes.php

Route::get('mail', '[email protected]');

Ok, now need to add mail function in HomeController.php file so we can add this way :

app/Http/Controllers/HomeController.php

public function mail()
{
    $user = User::find(1)->toArray();
    Mail::send('emails.mailEvent', $user, function($message) use ($user) {
        $message->to($user->email);
        $message->subject('Sendgrid Testing');
    });
    dd('Mail Send Successfully');
}

Now we are at last step need to create email template file for the sending mail.

so let’s create a new file mailEvent.blade.php file in emails folder.

resources/views/emails/mailEvent.blade.php

Hi, 
This is My Test Mail.
Thank you..

Read Also: How to resolve – No supported encrypter found in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →