Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Send Mail in Laravel Through Gmail SMTP

How to Send Mail in Laravel Through Gmail SMTP ?

Aman Jain, May 9, 2022May 10, 2022

You can easily configure Send Mail in Laravel Through Gmail SMTP by enabling the app password and less secure apps in your settings. Email is very common operation of any website like sending an email to users after registration, Send newsletters to users and many more. In this tutorial i will show you to Send Mail in Laravel Through Gmail SMTP. Laravel provides multiple drivers or services to send mail from different different providers.

Laravel gives wide range of options to send emails. We can queue an email for send on specific time using the queue drivers.

Following are the supported methods of Laravel mail

  1. Amazon SES
  2. MailGun
  3. Sendmail
  4. SparkPost
  5. SMTP

In this example we will learn to use Gmail SMTP driver of laravel.

Laravel Mailing supports blade views based templating system so you can easily configure the mail body layout same as other pages.

It also gives a easy to implements mail with attachment feature so let’s start with example

Step 1: Create a laravel project

First step is to create the Laravel 8 project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

composer create-project laravel/laravel example-app

Step 2 : Configure the mail configuration

We have two options to configure the mail from .env file or config/mail.php file and the recommended way to configure it is from .enc file

GMAIL SMTP

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

Step 3 : Setup Gmail Less Secure App

This is an important step before sending mail using Gmail SMTP since Gmail not allow to send emails until this setting is not enabled so enable this from following URL

Method :1

https://myaccount.google.com/lesssecureapps?pli=1

Before:

Screenshot 2022 05 09 at 6.41.56 AM
Before Enable

After:

Screenshot 2022 05 09 at 6.42.06 AM
After enable the setting of gmail

NOTE: This method is going to be deactivate after 30 MAY 2022, so i recommend you to use below method

Method 2:

Create & use App Passwords

If you use 2-Step-Verification and get a “password incorrect” error when you sign in, you can try to use an App Password.

  1. Go to your Google Account.
  2. Select Security.
    Screenshot 2022 05 09 at 7.00.31 AM
  3. Under “Signing in to Google,” select App Passwords. You may need to sign in. If you don’t have this option, it might be because:
    1. 2-Step Verification is not set up for your account.
    2. 2-Step Verification is only set up for security keys.
    3. Your account is through work, school, or other organization.
    4. You turned on Advanced Protection.
  4. At the bottom, choose Select app and choose the app you using and then Select device and choose the device you’re using and then Generate.
  5. Follow the instructions to enter the App Password. The App Password is the 16-character code in the yellow bar on your device.
    Screenshot 2022 05 09 at 7.00.21 AM
  6. Tap Done.
    Screenshot 2022 05 09 at 7.00.13 AM 1

Source :https://support.google.com/accounts/answer/185833#zippy=%2Cwhy-you-may-need-an-app-password

Update new app password

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

Step 3 : Generate Mailable Class

In laravel 8 or 9 every mail is implements mailable class thus first we need to create a mail class as follow

php artisan make:mail TestMail

This will create a class in following location App\Mail\TestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('test');
    }
}

Update the code as below

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;
    private $user;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user=$user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('info@readerstacks.com')
               ->subject('Mail from readerstacks.com')
               ->view('emails.test',["user"=>$this->user,"title"=>"Register"]);
    }
}

Step 4 : Create View for Mail

Create a simple view for mail body in resources/views/emails/test.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Readerstacks.com</title>
</head>
<body>
    <h1>Hello {{ $user['name'] }}</h1>
    
    <p>{{ $user['body'] }}</p>
    <p>Thanks to visit us.</p>
    
</body>
</html>

Step 5 : Create Route and Send Mail

Now, create a route and call Mail methods in inline function as below

<?php
use Illuminate\Support\Facades\Route;


Route::get('/send-mail',function(){
 $user = [
        'name' => 'Readerstacks',
        'body' => 'This is simple mail from Readerstacks'
    ];
   
    \Mail::to('receiver_email@domain.com')->send(new \App\Mail\TestMail($user));
   
});

Also Read : How to Send Mail in Laravel Through Sendmail and SMTP ?

Related

Php Laravel Laravel 9 emailgmaillaravelphpsendmailsmtp

Post navigation

Previous post
Next post

Related Posts

Php How to Get Current Url in Blade File Laravel

How to Get Current Url in Blade File Laravel ?

October 27, 2022March 16, 2024

Sometimes in our application we want to Get Current Url in Blade file Laravel because we want to add specific conditions to show the data on behalf of current url. It can be complete url, route name and query params too so that we can fetch the data accordingly and…

Read More
Laravel Laravel Components

Laravel artisan command to generate controllers, Model, Components and Migrations

August 22, 2021October 4, 2022

Laravel is robust framework of php which provides rapid development and security for low and high level projects. It have many features like Laravel Components, Validation, eloquent, migrations, artisnan security etc. Let’s have look artisan commands of laravel artisan. There is a command which gives you list of all available…

Read More
Php Get Json Post Data in Laravel

How to Get Json Post Data in laravel from Request ?

June 8, 2022February 8, 2024

In this article we will learn to get json post data in laravel from request. Laravel by default supports for form-data and x-www-form-urlencode which we can get easily using the Request class as follow $request->field_name or $request->get(‘field_name’). To retrieve the json post data in laravel we need to call json…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version