Laravel Gmail SMTP Configuration Made Easy – Send Email Notifications Step by Step

If you want your Laravel application to send notifications, password resets, or user confirmations by email, setting up Laravel Gmail SMTP configuration is one of the easiest and most reliable solutions. With this setup, you can send secure and verified emails directly through Gmail’s SMTP server, ensuring your messages reach the recipient’s inbox safely.

In this tutorial, you’ll learn how to configure Laravel with Gmail SMTP step by step — from enabling app passwords in Gmail to testing your email delivery using Laravel’s built-in Mail functionality.

By the end of this guide, you’ll have a fully working Laravel Gmail SMTP configuration ready to send email notifications, perfect for beginners who want a simple, free, and secure way to handle outgoing emails in Laravel.

Step 1: Create a New Laravel Project

Open your terminal and create a new Laravel project:

You’ll first run:

composer global require laravel/installer

(You only need to run this once globally).

Then you can run this command to get the interactive experience and create your project.

laravel new laravel-project-name

Go to the project folder:

cd laravel-project-name

Run the migration command

php artisan migrate

Then start the Laravel development server:

php artisan serve

After that, open another terminal and run these commands:

npm install
npm run dev

Visit http://localhost:8000 — you should see the Laravel welcome page.

Step 2: Enable 2-Step Verification in Your Gmail Account

Start your Laravel Gmail SMTP configuration by enabling 2-Step Verification in Gmail. This allows you to create an App Password that Laravel will use for secure email authentication.

  1. Go to https://myaccount.google.com/security.
  2. Enable 2-Step Verification under the “Signing in to Google” section.
  3. Once enabled, go back to the same page and choose App passwords.
  4. Create a new app password:
    • Select “Mail” as the app.
    • Select your device (or choose “Other”).
    • Google will generate a 16-character password — keep this safe.

You’ll use this password in your Laravel .env configuration instead of your normal Gmail password.

Step 3: Update Your .env File

Next, update the .env file in your Laravel project with Gmail SMTP credentials. This step is the core of your Laravel Gmail SMTP configuration and ensures Laravel connects to Gmail properly.

MAIL_MAILER=smtp
MAIL_SCHEME=smtps
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=your_generated_app_password
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

Step 4: Update the Mail Configuration (Optional)

Laravel reads mail settings from .env, but you can verify or adjust them in config/mail.php. This optional step fine-tunes your Laravel Gmail SMTP configuration for reliable email delivery.

return [
    'default' => env('MAIL_MAILER', 'smtp'),

    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'scheme' => env('MAIL_SCHEME'),
            'url' => env('MAIL_URL'),
            'host' => env('MAIL_HOST', '127.0.0.1'),
            'port' => env('MAIL_PORT', 2525),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'local_domain' => env('MAIL_EHLO_DOMAIN', parse_url((string) env('APP_URL', 'http://localhost'), PHP_URL_HOST)),
        ],
    ],

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],
];

You usually don’t need to modify this file — Laravel automatically reads the .env settings.

Step 5: Create Mail Notification

Create a new notification class in Laravel to send emails. This step shows how to use the Notification system with your Laravel Gmail SMTP configuration.

Example notification class:

php artisan make:notification EmailNotification

Then edit app/Notifications/EmailNotification.php and update toMail() function:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class EmailNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->subject('Email Notification Testing')
            ->line('Thank you for checking our step-by-step tutorials!')
            ->action('Visit Website', url('/'))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            //
        ];
    }
}

Step 6: Send Mail Notification

est your setup by sending an email notification. This confirms your Laravel Gmail SMTP configuration works and that Gmail SMTP is sending emails correctly.

In your web.php file, add:

use App\Notifications\EmailNotification;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Route;

Route::get('send-email', function () {

    Notification::route('mail', '[email protected]')->notify(new EmailNotification());

    return 'The email was successfully sent!';
});

Final Step: Running and Testing Your Project

To test your project, run the Laravel server:

php artisan serve

If your project also uses Vue.js, run the frontend in a separate terminal:

npm run dev

After both are running, open your browser and visit:

http://127.0.0.1:8000/send-email

Make sure all pages load and your features work as expected.

If everything is set up correctly, you’ll receive an email in the recipient’s inbox.

Send Mail Notification

Optional Step: Customizing the Default Email Notification Template

Once your Laravel Gmail SMTP configuration works, customize Laravel’s default mail template. Add your brand logo, colors, or footer text to make emails look professional.

The first step is to bring Laravel’s default email template components into your application’s resources/views directory. This allows you to edit them without changing core Laravel files.

php artisan vendor:publish --tag=laravel-mail

This command will create the directory structure at resources/views/vendor/mail/, which contains two key sub-folders:

  • html: Contains the HTML components (like header.blade.php, footer.blade.php, button.blade.php, etc.).
  • text: Contains the plain-text versions of the templates.

Optional Step: Fixing the SSL Certificate Error in Laravel Gmail SMTP Configuration

If you see an SSL certificate error when sending mail, this step helps you fix it. You’ll update your PHP settings so your Laravel Gmail SMTP configuration connects securely to Gmail.

Common Error Message

You might see this error in your terminal or browser response:

Connection could not be established with host "ssl://smtp.gmail.com:465": 
stream_socket_client(): SSL operation failed with code 1. 
OpenSSL Error messages: error:0A000086:SSL routines::certificate verify failed

Why This Happens

This error usually means that your PHP environment or system cannot verify Gmail’s SSL certificate.
It often occurs when:

  • You’re using an older Windows or WAMP/XAMPP environment without updated SSL certificates.
  • Your local machine’s OpenSSL certificate bundle is outdated or missing.
  • The PHP configuration doesn’t know where to find the trusted certificate store (cacert.pem).

How to Fix It

Follow these simple steps to solve the issue:

1. Download the Latest Certificate File

Go to the official cURL CA certificate page and download the latest cacert.pem file:
👉 https://curl.se/docs/caextract.html

Save it somewhere on your system, for example:

C:\xampp\php\extras\ssl\cacert.pem

2. Update Your PHP Configuration

Edit your php.ini file (you can find it via phpinfo() or in your XAMPP/WAMP installation folder).

Search for these two lines and update them to point to your downloaded file:

; Under the [curl] section
curl.cainfo = "/path/to/your/cacert.pem"

; Under the [openssl] section
openssl.cafile = "/path/to/your/cacert.pem"

Save the file and restart Apache or your PHP server.

Conclusion

You’ve now completed your Laravel Gmail SMTP configuration and can send email notifications easily. For more ways to send emails in Laravel, explore our related tutorials.

Once your project grows, you can switch to a professional email service like Mailgun, SendGrid, or AWS SES, but Gmail SMTP remains a great starting point for learning and development.

If you want to explore more notification methods, check out our guide on How to Send SMS from Laravel – Step-by-Step Guide for Beginners to combine both email and SMS into your app’s notification system.

Senghok
Senghok

Senghok is a web developer who enjoys working with Laravel and Vue.js. He creates easy-to-follow tutorials and guides to help beginners learn step by step. His goal is to make learning web development simple and fun for everyone.

Articles: 35

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

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