Setting Up Gmail SMTP in Laravel: A Comprehensive Guide

Email is a crucial part of modern web applications. Whether you need to send user registrations, password resets, notifications, or newsletters, a reliable email system keeps communication smooth.

One of the best ways to send emails in Laravel is by using Gmail SMTP. It’s a popular choice among developers because it’s secure, reliable, and easy to integrate. With Gmail SMTP, you can send emails safely from your Laravel app without much hassle.

Laravel is a powerful PHP framework that makes email configuration simple with its built-in SMTP support. Setting up Gmail SMTP in Laravel is easy—it just requires configuring your Gmail account and updating Laravel settings.

Setting Up Gmail SMTP in Laravel

In this guide, we’ll show you the exact steps to configure Gmail SMTP in Laravel, ensuring reliable email delivery. Whether you're sending notifications or user verification emails, using Gmail SMTP with Laravel lets you leverage Gmail’s secure email infrastructure.

This guide will also help you troubleshoot common Gmail SMTP issues in Laravel, follow best practices for email delivery, and keep your email system secure.

By the end of this tutorial, you’ll have Gmail SMTP fully set up in your Laravel app, improving email functionality and user experience. With Laravel’s simplicity and Gmail SMTP’s reliability, your app will be ready to handle all email communications smoothly.


What is Gmail SMTP, and Why Should You Use It?

SMTP (Simple Mail Transfer Protocol) is the standard protocol for sending emails over the internet. Gmail provides an SMTP service that allows developers to send emails using their Gmail accounts securely and efficiently.

Here’s why Gmail SMTP is a popular choice:

  • Security: Uses SSL/TLS encryption for safe transmission.
  • Reliability: Backed by Google’s global infrastructure.
  • Ease of Use: Minimal setup required; natively supported in Laravel.
  • Cost: Free tier supports up to 500 emails/day—sufficient for most apps.

Prerequisites

  • A Gmail account
  • A Laravel project (8.x, 9.x, 10.x, or 11.x)
  • Access to your project’s .env file
  • Basic understanding of PHP and Laravel

Step 1: Configure Gmail for SMTP Access

Google no longer supports “Less Secure App Access.” Instead, you must use an App Password.

Generate an App Password

  1. Go to your Google Account.
  2. Enable 2-Step Verification (required).
  3. Navigate to Security > App passwords.
  4. Select Mail as the app and Other as the device.
  5. Click Generate and copy the 16-character password.

Step 2: Configure the .env File

Update your .env file with the following settings:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-16-char-app-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your-email@gmail.com
MAIL_FROM_NAME="Your Application Name"

Replace your-email@gmail.com and your-16-char-app-password with your actual Gmail and App Password.


Step 3: Update Laravel Mail Configuration

Laravel’s config/mail.php file should already use the .env values by default. Verify it includes:

'mailers' => [
    'smtp' => [
        'transport' => 'smtp',
        'host' => env('MAIL_HOST', 'smtp.gmail.com'),
        'port' => env('MAIL_PORT', 587),
        'encryption' => env('MAIL_ENCRYPTION', 'tls'),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
        'timeout' => null,
    ],
],
'from' => [
    'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
    'name' => env('MAIL_FROM_NAME', 'Example App'),
],

Step 4: Create a Mailable Class

php artisan make:mail TestEmail

Edit app/Mail/TestEmail.php:

namespace App\Mail;

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

class TestEmail extends Mailable
{
    use Queueable, SerializesModels;

    public function build()
    {
        return $this->view('emails.test')
                    ->subject('Welcome to Laravel Email Setup');
    }
}

Step 5: Create an Email View

mkdir -p resources/views/emails
touch resources/views/emails/test.blade.php

Add this to resources/views/emails/test.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome Email</title>
</head>
<body>
    <h1>Welcome to Our Application!</h1>
    <p>This is a test email sent using Gmail SMTP in Laravel.</p>
</body>
</html>

Step 6: Test Your Email Setup

Route

In routes/web.php:

use App\Http\Controllers\EmailController;
Route::get('/send-email', [EmailController::class, 'sendTestEmail']);

Controller

php artisan make:controller EmailController

Edit app/Http/Controllers/EmailController.php:

namespace App\Http\Controllers;

use App\Mail\TestEmail;
use Illuminate\Support\Facades\Mail;

class EmailController extends Controller
{
    public function sendTestEmail()
    {
        Mail::to('recipient@example.com')->send(new TestEmail());
        return 'Email sent successfully!';
    }
}

Visit http://your-app.test/send-email to trigger the test.


Conclusion

Configuring Gmail SMTP in Laravel is a crucial skill for any developer working on applications that depend on email functionality. Whether you're building an e-commerce platform, a blog, or a corporate system, emails are essential for user communication and engagement.

By following this guide, you’ve learned how to set up Gmail SMTP in Laravel, create mailable classes, customize email templates, and send test emails with accuracy.

This setup ensures secure and reliable email delivery, using Gmail’s trusted infrastructure. With app passwords, SSL/TLS encryption, and proper Laravel settings, your system remains both secure and efficient.

Mastering this skill will make email management easier in your projects and prepare you for more complex email configurations in the future. If you run into issues, revisit this guide for troubleshooting tips. For advanced users, services like Mailgun or SendGrid may be worth exploring as your application scales.

Now, you have the knowledge and confidence to integrate Gmail SMTP into your Laravel applications. Keep optimizing and experimenting to enhance your email functionality and deliver a seamless user experience.


Frequently Asked Questions

Do I need to enable 2FA to use Gmail SMTP in Laravel?+
Why is my Gmail SMTP email not sending?+
Can I use my regular Gmail password instead of an App Password?+
What port should I use for Gmail SMTP in Laravel?+
Where are Laravel email templates stored?+