How To Send Mail Using Gmail In Laravel 9

Websolutionstuff | Aug-03-2022 | Categories : Laravel

In this article, we will see how to send mail using gmail in laravel 9. we will learn laravel 9 to send mail using gmail SMTP server.  Laravel and Symfony Mailer provide drivers for sending email via SMTP, Mailgun, Postmark, Amazon SES, and sendmail, allowing you to quickly get started sending mail through a local or cloud based service of your choice.

So, let's see send mail in laravel 9 using gmail.

Step 1: Install Laravel 9

Step 2: Setup .env file Configuration

Step 3: Create Mailable Class

Step 4: Create Blade File

Step 5: Create Route

 

Step 1: Install Laravel 9

In this step, we will create laravel application using the below command.

composer create-project laravel/laravel example-app

 

 

Step 2: Setup .env file Configuration

Now, we will set up mail configuration in the .env file.

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

 

Note: When 2-step Verification is turned on for an account, access to less secure apps is automatically disabled, unless users are in a configuration group that allows access to less secure apps. Go to Manage access to less secure apps below.

 

Step 3: Create Mailable Class

In this step, we will create a mailable class using the laravel artisan command.

php artisan make:mail GmailTestMail

This command will create a new file app/Mail/GmailTestMail.php.

<? php

namespace App\Mail;
   
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
  
class GmailTestMail extends Mailable{

    use Queueable, SerializesModels;

    public $user;

    public function __construct($user){

        $this->user = $user;

    }

    public function build(){

        return $this->subject('This is gmail testing')
                    ->view('emails.test');
    }
}

 

 

Step 4: Create Blade File

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

<!DOCTYPE html>
<html>
<head>
    <title>How To Send Mail Using Gmail In Laravel 9 - Websolutionstuff</title>
</head>
<body>
    <h5>{{ $user['name'] }}</h5>
    <p>{{ $user['info'] }}</p>
    <p>Thank you</p>
</body>
</html> 

 

Step 5: Create Route

In this step, we will create a route for sending emails.

routes/web.php

Route::get('send-mail', function () {

    $user = [
        'name' => 'Websolutionstuff',
        'info' => 'This is gmail example in laravel 9'
    ];

    \Mail::to('[email protected]')->send(new \App\Mail\GmailTestMail($user));

    dd("Successfully send mail..!!");

});

 


You might also like:

Recommended Post
Featured Post
How To Increase Session Lifetime In Laravel
How To Increase Session Lifeti...

In this article, we will see how to increase session timeout in laravel. In this example, we can see how to se...

Read More

Jun-28-2020

How to Install Zoom in Ubuntu 22.04 using Terminal
How to Install Zoom in Ubuntu...

Greetings Ubuntu enthusiasts! If you're ready to dive into the world of video conferencing on your Ubuntu 22.04...

Read More

Jan-19-2024

How To Encrypt And Decrypt String In Laravel 8
How To Encrypt And Decrypt Str...

In this example we will see how to encrypt and decrypt string in laravel 8 using crypt helper, As we all know laravel fr...

Read More

May-07-2021

Vue Js Sweetalert Modal Notification Tutorial
Vue Js Sweetalert Modal Notifi...

In this example, we will see vue js sweetalert modal notification tutorial. vue.js wrapper for sweetalert2. with su...

Read More

Jan-12-2022