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.

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.

Setting Up Gmail SMTP in Laravel: A Comprehensive Guide

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, or Simple Mail Transfer Protocol, is the protocol used to send 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:


Prerequisites

Before we begin, ensure you have the following:


Step 1: Configure Gmail for SMTP Access

Gmail, by default, blocks less secure apps from accessing your account. To use Gmail SMTP, you must either allow less secure app access or, preferably, generate an App Password for enhanced security.

Option 1: Enable "Less Secure App Access"

  1. Log in to your Gmail account.
  2. Go to Google Account Settings > Security.
  3. Scroll to the "Less Secure App Access" section.
  4. Turn it ON.

Important Note: This option is being deprecated by Google, so it’s better to use the App Password method.

Option 2: Generate an App Password

  1. Log in to your Gmail account.
  2. Navigate to Google Account Settings > Security.
  3. Enable 2-Step Verification if it’s not already enabled.
  4. Under the "Signing in to Google" section, click on App
  5. Select Mail as the app and Other as the device.
  6. Generate an app password and copy it.

Step 2: Configure the .env File

The .env file in Laravel is where you store sensitive configuration settings like email credentials. Open your .env file and update the email configuration with the following:

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

Explanation of Each Field:


Step 3: Update Laravel Mail Configuration

Laravel's mail settings are located in the config/mail.php file. Open this file and ensure it reflects the .env configuration:

return [ 'default' => env('MAIL_MAILER', 'smtp'), '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', 'example@example.com'), 'name' => env('MAIL_FROM_NAME', 'Example'), ], ];

Step 4: Create a Mailable Class

Laravel’s Mailable Class allows you to structure your email content. To create a new mailable class, use the following Artisan command:

php artisan make:mail TestEmail

Customize the Mailable Class

Edit the file app/Mail/TestEmail.phpas follows:

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

Create a Blade template for the email content. Run the following command to create the file:

touch resources/views/emails/test.blade.php

Add the following content to 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

To send a test email, create a route and a controller:

Route

In routes/web.php:

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

Generate a new 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-laravel-app.test/send-email in your browser to test the setup.

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.

FAQ

Ensure you’ve enabled app passwords in your Gmail account, double-check your `.env` file for typos or errors, and confirm that your hosting provider allows outgoing SMTP connections.

Enable the following in your `.env` file for detailed logs:
APP_DEBUG=true
MAIL_LOG_CHANNEL=stack

Yes, if your custom domain is connected to Google Workspace.

Free accounts are limited to 500 emails/day, while Google Workspace accounts support up to 2,000 emails/day.

Use app passwords instead of your main Gmail password, set appropriate permissions for your `.env` file, and always enable SSL/TLS encryption for secure communication.
Previous: How to Debug Laravel SQL Queries in API Requests: A Developer's GuideNext: Mastering Laravel Login System: A Comprehensive Guide

Share