How To Send Email Using SendGrid In Laravel 9

Websolutionstuff | Jul-25-2022 | Categories : Laravel

In this article, we will see how to send email using SendGrid in laravel 9. Laravel provides a clean API over the popular SwiftMailer library with drivers for SMTP, PHP's mailsendmail and more. For this example, we'll be sending an email with SendGrid using the SMTP Driver. SendGrid is a cloud-based SMTP provider that allows you to send email without having to maintain email servers.

SendGrid Documentation: Send Email with Laravel & SendGrid | Twilio

So, let's see, laravel 9 send email using SendGrid, and send mail in laravel 9 using SendGrid.

Step 1: Setup .env file

In this step, we will configure the .env file.

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"
[email protected]

You can send 100 messages per SMTP connection at a time.

 

 

Step 2: Create Mailable

Next, you need to create a Mailable class using the below command.

php artisan make:mail TestEmail

This command will create a new file under app/Mail/TestEmail.php and it's looks like this.

<?php

namespace 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 = '[email protected]';
        $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'] ]);
    }
}

 

 

Step 3: Create Blade File

Let's create a file under app/resources/views/emails/test.blade.php and add this code.

<!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>

 

Step 4: Sending Mail

In this step,  we will use a mailable class and send a test mail.

<?php
    use App\Mail\TestEmail;

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

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

 


You might also like:

Recommended Post
Featured Post
Cron Job Scheduling In Laravel
Cron Job Scheduling In Laravel

In this article, we will see cron job scheduling in laravel. Many times we require to run some piece of code in a specif...

Read More

Sep-28-2020

How To Create Candlestick Chart In Laravel 9 Using Highcharts
How To Create Candlestick Char...

In this article, we will see how to create a candlestick chart in laravel 9 using highcharts. A candlestick is a ty...

Read More

Oct-06-2022

How to Create a Livewire Image Upload in Laravel 10?
How to Create a Livewire Image...

Livewire has revolutionized the way developers build interactive web applications with its real-time capabilities. And w...

Read More

Aug-02-2023

How to Upload File on the FTP Server Using PHP
How to Upload File on the FTP...

In this small post i will show you how to upload file on the ftp server using php. As we know there are many ftp functio...

Read More

May-20-2021