Sending Email Via Gmail SMTP Server In Laravel 6

If you want to use Gmail SMTP server for sending email in Laravel versions like laravel 5.7, 8, or Laravel 6? Sometimes PHP default mail() functions does not work for sending emails due to some server settings or configurations. In such case, you can use SMTP server for sending emails. In this article, we will discuss about how to send email using Gmail SMTP server in Laravel 6.

One more benefit of using SMTP server is, you can send email from your local system also. By this way, it will helpful for us to test the email functionality on the local server itself.

Setup Of Gmail SMTP Server In Laravel

1.  In this step you need to add chunk of code in mail.php

'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' =>false,
'verify_peer_name' => false,



Laravel 5.7 or 6 use config/mail.php file for storing details related to sending emails. This file contains settings like MAIL_DRIVER, MAIL_HOST, MAIL_PORT, etc. For sending email you need to provide this information.

To adding these setting’s, we don’t need to edit config/mail.php. You should store these details in the .env file.

2. Open your .env file which is located in your root directory of your project and you will find below code related to email settings.


You need to edit the above details as follows.


Here, you need set driver as smtp, host for gmail as, smtp port for gmail as 587 and encryption method to ssl. Make sure you have replaced your Gmail username and password.

If you are using Gmail SMTP, you need to change some settings into your Google account. Login to your Google account and click on My Account. Once you are on My Account page then click on Security. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.


Code For Sending Email In Laravel

At this stage, we are completed with all basic setup. Now, we need to write a Laravel code which will send an email.

Your code will something like as below.

$to_name = 'TO_NAME';
$to_email = 'TO_EMAIL_ADDRESS';
$data = array('name'=>"your name", "body" => "Test mail for project");
Mail::send('emails.mail', $data, function($message) use ($to_name, $to_email) {
    $message->to($to_email, $to_name)
            ->subject('Web Testing Mail');
    $message->from('FROM_EMAIL_ADDRESS','Artisans Web');

In the above code, we are using the view ’emails.mail’. It means we have to create folder and files as resources->views->emails->mail.blade.php.

mail.blade.php will contains code same like this.

Hi <strong>{{ $name }}</strong>,
<p>{{ $body }}</p>

You All done! Laravel in the background automatically use Gmail SMTP server and send an email.