How to send an Email in Laravel using Gmail SMTP Server

Ogbonna Vitalis
Feb 16, 2019 · 3 min read

Have you been wondering how to send an email in Laravel using its default SMTP settings without having an SMTP account or purchasing an SMTP Plan?

Have the PHP default mail() functions, failed you due to some server settings and configurations?

Sending Email for Laravel Applications

In this tutorial we will discuss you how to configure our Laravel applications to send emails using your Gmail account as Gmail SMTP server with the default Laravel SMTP configurations.

The benefits of using SMTP server cannot be over emphasized, with an SMTP server we can send email from our local server. Thus, giving us the ability to test the email functionality on the local server itself. Mails sent via SMTP are not flagged as SPAM by Gmail as seen in mails sent using the PHP default mail() function.

To get started, we follow the following simple steps:

Step 1: Configure Gmail SMTP Server in Laravel Application

Laravel uses config/mail.php file for storing details used in mail sending. This file contains settings like MAIL_DRIVER, MAIL_HOST, MAIL_PORT, etc. In order to successfully send an email, we need to provide these information.

To add these required information, we need not edit this config/mail.php file, rather we would supply these details accordingly in the .env file.

Thus, open your .env file which is located in your root directory of our application and checkout for this settings:

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

Now edit the details above as follows.


MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=ENTER_YOUR_EMAIL_ADDRESS(GMAIL)
MAIL_PASSWORD=ENTER_YOUR_GMAIL_PASSWORD
MAIL_ENCRYPTION=ssl

In the above settings , we configured MAIL_DRIVER as smtp, MAIL_HOST for Gmail as smtp.googlemail.com, MAIL_ PORT for Gmail as 465 and MAIL_ENCRYPTION method as ssl.

The MAIL_USERNAME and PASSWORD should be replaced with your Gmail Email address and Password respectively.

Since we are using Gmail SMTP, we need to change some security settings on our Google account, to give access to less secured applications.

Step 2: Configure your Google Account

Login to your Google Email Account and click on Google Account Button. This button is display when you click on the profile picture in your Gmail Dashboard as shown.

Once you are on My Account Page then click on Security and scroll down to the bottom and you will find ‘Less secure app access’ settings. Click on the radio button to set it ON.

Step 3: Send Emails from your Laravel Application

At this point, all the basic setup has been completed. We can now write some Laravel PHP codes to send an email.

To get started, create any controller of choice where the mail sending logic will be handled, then in this controller write your codes using the code snippet below as a guide.

$to_name = ‘RECEIVER_NAME’;
$to_email = ‘RECEIVER_EMAIL_ADDRESS’;
$data = array(‘name’=>”Ogbonna Vitalis(sender_name)”, “body” => “A test mail”);
Mail::send(‘emails.mail’, $data, function($message) use ($to_name, $to_email) {$message->to($to_email, $to_name)->subject(Laravel Test Mail’);$message->from(‘SENDER_EMAIL_ADDRESS’,’Test Mail’);});

In the above code, we are using our mail template as ’emails.mail’ file . hence we need to create an ‘emails’ folder and the mail.blade.php file at resources\views\emails\mail.blade.php

Our test mail template mail.blade.php should just contains a few test codes as shown below.

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

Hurray, we are done, simply create any route of your choice to and start sending mails from your Laravel application.

I believe this brief tutorial will be of much help. Do not forget to share this tutorial your friends, follow me on twitter and do visit my corner for more updates.

Don’t forget to leave your comments below, if you have any. Love Leads!!!


The original copy of this tutorial was published at my blog vivvaa’s corner, you can check it out for other enriching contents

Ogbonna Vitalis

Written by

Functional Engineering and Design of Elegant Web Based Systems are my deepest passion and greatest skill. I am obsessed with learning, writing, growth and tech.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade