Laravel 8 Send Mail using Sendgrid Tutorial

Laravel 8

Nicesnippets

30987

24-09-2020


Laravel 8 Send Mail using Sendgrid Tutorial

Hi Guys,

In this tutorial,I will learn you how to send mail usen sendgrid in laravel 8.you can easy and simply send mail sendgrid in laravel 8.

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.

First we will add configration on mail. i added my gmail account configration. so first open .env file and bellow code:

Step 1: Download Laravel 8 Fresh New Setup


First, we need to download the laravel fresh setup. Use the below command and download fresh new laravel setup :

composer create-project --prefer-dist laravel/laravel blog

Step 2: .env file

Check your .env file and configure these variables:

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

[email protected]

MAIL_PASSWORD=123456

MAIL_ENCRYPTION=tls

Next you need to create a Mailable class, Laravel's CLI tool called Artisan makes that a simple feat. Open CLI, go to the project directory and type:

php artisan make:mail sendGrid --markdown=emails.sendGrid

This command will create a new file under app/Mail/SendGrid.php and it should look something like this:

app/Mail/SendGrid.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

class SendGrid extends Mailable

{

use Queueable, SerializesModels;

public $input;

/**

* Create a new message instance.

*

* @return void

*/

public function __construct($input)

{

$this->input = $input;

}

/**

* Build the message.

*

* @return $this

*/

public function build()

{

return $this->markdown('emails.sendGrid')

->with([

'message' => $this->input['message'],

])

->from('[email protected]', 'Vector Global')

->subject($this->input['subject']);

}

}

Step 3: Create Route

Here, we need to add simple route for postcontroller. so open your "routes/web.php" file and add following route.

routes/web.php

<?php

use App\Http\Controllers\PostController;

Route::get('mail/send-grid', [PostController::class, 'sendMail']);

Step 4: Create Controller

In this step, now we should create new controller as PostController. So run bellow command and create new controller.

php artisan make:controller PostController

So, let's copy bellow code and put on PostController.php file.

app/Http/Controllers/PostController.php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller;

use Illuminate\Http\Request;

use Validator;

use App\Mail\SendGrid;

class PostController extends Controller

{

public function sendMail(Requset $request)

{

$input = ['message' => 'This is a test!'];

Mail::to('[email protected]')->send(new SendGrid($input));

}

}

Step 5: Add Blade Files

In last step. In this step we have to create just blade file. so we need to create only one blade file as sendGrid.blade.php file.

resources/views/sendGrid.blade.php

@component('mail::message')

# Introduction

{{ $message }}

@component('mail::button', ['url' => ''])

Button Text

@endcomponent

Thanks,

{{ config('app.name') }}

@endcomponent

It will help you...


Recommended Posts