10-Apr-2023
.
Admin
Hi Guys,
In this tutorial, I will learn you how to send mail using sendgrid in laravel 9. you can easily and simply send mail sendgrid in laravel 9.
Sendgrid is a very popular API to send email from our laravel application. It is very fast to send mail and also you can track sent mail. Tracking email is a very important feature of Sendgrid api and you can also see how many users open your mail, and 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
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip the following step.
composer create-project laravel/laravel example-app
Step 2: .env file
Check your .env file and configure these variables:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xyz@gmail.com
MAIL_PASSWORD=123456
MAIL_ENCRYPTION=tls
Step 3: Add Mail
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('test@gmail.com', 'Vector Global')
->subject($this->input['subject']);
}
}
Step 4: Add Route
Here, we need to add a simple route for postcontroller. so open your "routes/web.php" file and add the following route.
routes/web.php
<?php
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('mail/send-grid', [PostController::class, 'sendMail']);
Step 5: Add 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
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Validator;
use App\Mail\SendGrid;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function sendMail(Requset $request)
{
$input = ['message' => 'This is a test!'];
Mail::to('dummy@example.com')->send(new SendGrid($input));
}
}
Step 6: Add Blade Files
In the last step. In this step, we have to create just a blade file. so we need to create only one blade file as a 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
Run Laravel App:
All steps have been done, now you have to type the given command and hit enter to run the laravel app:
php artisan serve
Now, you have to open web browser, type the given URL and view the app output:
http://localhost:8000/mail/send-grid
It will help you...
#Laravel 9