How to Send Mail using Sendgrid in Laravel?
Sendgrid is very popular API to send email from our laravel application. It is very fast to send mail and also you can track sended mail. Tracking email is very important feature of Sendgrid api and you can also see how much user open your mail, click on your mail too. In this post i would like to show you how to setting of Sendgrid in our laravel 5 application. In this example you can learn to send simple mail using Sendgrid site. If you are use Sendgrid for sending email then you can save loading time and you can get mail fast.
First we will add configration on mail. i added my gmail account configration. so first open .env file and bellow code:
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=sendgridusername
MAIL_PASSWORD=sendgridpassword
Ok, now we need to add secret and domain of sendgrid api configration. So first create new account in sendgrid.com if you don't have before. After registeration active your sendgrid account and set username and password
Now we are ready to send mail for test so first create test route for email sending.
app/Http/routes.php
Route::get('mail', 'HomeController@mail');
Ok, now add mail function in HomeController.php file so 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');
}
At last create email template file for send mail so let's create mailEvent.blade.php file in emials folder.
resources/views/emails/mailEvent.blade.php
Hi, sendgrid testing.
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.