Hello Dev.
On this article,i will examine you how to send mail usen sendgrid in laravel 8. you may smooth and clearly send mail sendgrid in laravel 8.
Sendgrid is very famous API to send email from our laravel application. it is very speedy to send mail and additionally you could track sended mail. tracking email may be very crucial feature of Sendgrid api and you could also see how much user open your mail, click in your mail too.
First we will add configration on mail. i added my gmail account configration. so first open .env file and bellow code:
So let's start the lesson...
First of all we want to get clean and new Laravel eight version application the use of bellow command, now open your terminal OR command prompt and then fire bellow command and run it:
composer create-project --prefer-dist laravel/laravel post
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
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 have to appearance something like this:
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']);
}
}
Right here, we need to add simple route for PostController. so open your "routes/web.php" file and add following route.
web.php
<?php
use App\Http\Controllers\PostController;
Route::get('mail/send-grid', [PostController::class, 'sendMail']);
On this step, now we ought to create new controller as PostController. So run bellow command and create new controller.
php artisan make:controller PostController
app/Http/Controllers/PostController.php
<?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));
}
}
In last step. on this step we have to create just blade file. so we want to create most effective one blade file as sendGrid.blade.php file.
sendGrid.blade.php
@component('mail::message')
# Introduction
{{ $message }}
@component('mail::button', ['url' => ''])
Button Text
@endcomponent
Thanks,
{{ config('app.name') }}
@endcomponent
I hope it assists you, thanks for visiting my article if you like my article then share it with your friends on the social media platform.
Happy Coding.....