Sending Email Via Gmail SMTP Server In Laravel

Are you looking for using Gmail SMTP server for sending email in Laravel? Sometimes PHP default mail() functions does not send emails due to some server settings or configurations. In such case, we can use SMTP server for sending emails. In this article, we discuss how to send email using Gmail SMTP server in Laravel.

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

Setup Of Gmail SMTP Server In Laravel

Laravel 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 we need to provide this information.

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

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

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Edit the above details as follows.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=ENTER_YOUR_GMAIL_USERNAME
MAIL_PASSWORD=ENTER_YOUR_GMAIL_PASSWORD
MAIL_ENCRYPTION=ssl

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

Next, you need to make some changes in your Gmail settings. Login to your Google account and click on My Account. Once you are on My Account page then click on Sign-in & Security.

Sign In Security Gmail

On the next page, scroll down to the bottom and you will find ‘Allow less secure apps’ settings. Set it to ON.

Less Secure Apps Gmail

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 email.

Your code will something like as below.

$data = array('name'=>"Sam Jose", "body" => "Test mail");
   
Mail::send('emails.mail', $data, function($message) {
    $message->to('[email protected]', 'Artisans Web')
            ->subject('Artisans Web Testing Mail');
    $message->from('[email protected]','Sajid Sayyad');
});

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.

Our mail.blade.php will contains code look like as below.

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

<p>{{ $body }}</p>

That’s it! Laravel in the background automatically use Gmail SMTP server and send email.

Related Articles

If you liked this article, then please subscribe to our Youtube Channel for video tutorials.

Recommended Tutorials For You

10 thoughts on “Sending Email Via Gmail SMTP Server In Laravel

  1. (1/1) Swift_TransportException
    Process could not be started [The system cannot find the path specified.
    ]

    how to fix this?

Leave a Reply

Your email address will not be published. Required fields are marked *