How To Send Email Using SendGrid In Laravel 9

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="Websolutionstuff"
MAIL_FROM_ADDRESS=from@example.com
php artisan make:mail TestEmail
<?phpnamespace App\Mail;use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestEmail extends Mailable
{
use Queueable, SerializesModels;
public $data; public function __construct($data)
{
$this->data = $data;
}
public function build()
{
$address = 'janedeo@example.com';
$subject = 'This is a demo!';
$name = 'Jane Doe';
return $this->view('emails.test')
->from($address, $name)
->cc($address, $name)
->bcc($address, $name)
->replyTo($address, $name)
->subject($subject)
->with([ 'test_message' => $this->data['message'] ]);
}
}
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
</head>
<body>
<h2>How To Send Email Using SendGrid In Laravel 9 - Websolutionstuff</h2>
<p>{{ $test_message }}</p>
</body>
</html>
<?php
use App\Mail\TestEmail;
$data = ['message' => 'This is a test!']; Mail::to('john@example.com')->send(new TestEmail($data));

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Websolutionstuff

Websolutionstuff

96 Followers

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.