Sending Email in Laravel using Gmail SMTP Server

Sohail · · 2807 Views

In this post, we talk about how to send email using the Gmail SMTP server in Laravel. Well, there's no uncertainty the need to send emails in your web application will emerge, and you need to know how to get this done, properly? Not to worry, in this article, you will learn how easy it is to set up email in your Laravel application using Gmail SMTP Server! One more advantage of utilizing an SMTP server is, you can send emails from your local server also.

Setup Of Gmail SMTP Server In Laravel

Laravel uses config/mail.php file for storing configuration to sending emails. This file contains configuration options like MAIL_DRIVER, MAIL_HOST, MAIL_PORT, and so forth. We need to provide this config data for sending emails.

To add these configurations, we don't have to edit config/mail.php. We should store these values in the .env file.

Open your .env file, and update the mail configuration details as follows.


Here, we set the driver as smtp, host for Gmail as, SMTP port for Gmail as 465, and encryption method to ssl. Ensure you have replaced your Gmail username and password.

Configure your Google Account

To utilize the Gmail SMTP server, you need to change a few settings on your Google account. So, log in to your Google account and click on 'Account.' When you are on the 'Account' page, click on 'Security.' Scroll down to the bottom, and you will discover 'Less secure app access' settings. Set this to ON.

Sending Email in Laravel using Gmail SMTP Server

Send Emails from your Laravel Application

Now, all the basic setup has been finished. We would now be able to write some codes in Laravel to send an email.

For this tutorial, I am going to use Laravel's 'Mail' class to send the email. 

Here is an example code:

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

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

That is it! Now, in the background Laravel will automatically utilize the Gmail SMTP server and send your emails.


Please login or create new account to add your comment.

You may also like:

Part #3: Rule objects based custom validation in Laravel

Laravel comes with multiple ways to add custom validation rules to validate form request inputs. I have already explained some of the ways in the following article links:
Harish Kumar

Part #2: How to use Laravel's Validator::extend method for custom validation

Validation is important in any application as it validates a form before performing actions on it. It allows the user to know their input is accurate and confident about the operation (...)
Harish Kumar

Part #1: Closure-based Custom Laravel Validation

While I was working with Laravel, validation using closure came to my mind, and I know it will be helpful to you. This tutorial assists you with all what is the difference between (...)
Harish Kumar

How to use the enumerations(Enums) of PHP 8.1 in Laravel?

The release of PHP 8.1 brings native enumerations to PHP. There is no more requirement for custom solutions in your Laravel projects since the Laravel v8.69 release has you back. (...)
Harish Kumar

Mobile App Development Process

With businesses adopting a mobile-first approach and the growing number of mobile apps, successful mobile app development seems like a quest. But it’s the process that determines (...)
Narola Infotech

What are Laravel Macros and How to Extending Laravel’s Core Classes using Macros with example?

Laravel Macros are a great way of expanding Laravel's core macroable classes and add additional functionality needed for your application. In simple word, Laravel Macro is an (...)
Harish Kumar