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
CRUD With Image Upload In Laravel 10 Example
CRUD With Image Upload In Lara...

In this article, we will see crud with image upload in laravel 10 examples. Here, we will learn how to image upload with...

Read More

Mar-27-2023

Laravel 9 Socialite Login with Google Account
Laravel 9 Socialite Login with...

In this article, we will see laravel 9 socialite login with a google account. This post gives you an example of a larave...

Read More

Apr-15-2022

How To Generate QR Code In Angular 13
How To Generate QR Code In Ang...

In this article, we will see how to generate QR code in angular 13. In this example, we will use the angularx-qrcod...

Read More

Jun-09-2022

How To Add Watermark On Image In Laravel 10
How To Add Watermark On Image...

In this article, we will how to add a watermark to an image in laravel 10. Here we will learn about laravel 10 adding a...

Read More

Mar-31-2023