Laravel 8 Send Email With Gmail SMTP Server Example

Hello Artisan,

This tutorial will cover laravel 8 send mail using the Gmail SMTP server. I am going to explain step by step to you the example of laravel 8 send mail using Gmail SMTP. It's a very simple example of laravel 8 sending mail gmail.

So if you don't know how to send email in laravel using Gmail SMTP server then this example is for you. We will learn example of laravel 8 send email using google gmail. This post will give you a complete example of Gmail SMTP send email in laravel 8.

In this tutorial, I will give you step-by-step instructions to send email using Google Gmail SMTP server in laravel 8. So let's start our tutorial:

 

Read also Gmail configuration setup to send mail in Laravel

 

Step 1: Configure .env

Change .env file like below

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_google_acc@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

 

Hope now your mail will be sent from your Laravel app. If you still face errors then visit here 

 

Login to Google - Sign in - Google Accounts

 

and then after visiting this link you will find a security option. Then click on it and scroll down from top to bottom.

smtp-server-does-not-work-for-send-email-in-laravel

Now just make sure that your Less secure app access is on.

gmail-configuration-setup-for-laravel

 

Step 2: Create Mail

In this step, we need to create mail for email sending using Gmail SMTP server. So let's run the below command.

php artisan make:mail MyTestMail

 

app/Mail/MyTestMail.php

namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
class MyTestMail extends Mailable
{
    use Queueable, SerializesModels;
  
    public $details;
  
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }
  
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Mail from codecheef.org')
                    ->view('emails.myTestMail');
    }
}

 

Step 3: Create Blade Template

In this step, we will create a blade view file and write an email that we want to send. now we just write some dummy text. create bellow files on the "emails" folder.

resources/views/emails/myTestMail.blade.php

 

Step 4: Create Route

Now at last we will create "MyTestMail" for sending our test email. so let's create the below web route for testing send an email.

routes/web.php

Route::get('send-mail', function () {
   
    $details = [
        'title' => 'Mail from codecheef.org',
        'body' => 'This is for testing email using smtp'
    ];
   
    \Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\MyTestMail($details));
   
    dd("Email is Sent.");
});

 

Now you need to visit this path to verify email http://localhost:8000/send-mail. Now hope it will work for you. Hope it can help you.

 

#laravel #laravel-8x