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.
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.
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:
Before we begin, ensure you have the following:
.env
file of your Laravel project.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"
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
.env
FileThe .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:
smtp
for Gmail.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'), ], ];
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.php
as 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'); } }
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>
To send a test email, create a route and a controller:
RouteIn routes/web.php:
use App\Http\Controllers\EmailController; Route::get('/send-email', [EmailController::class, 'sendTestEmail']);
ControllerGenerate 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.
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.
APP_DEBUG=true
MAIL_LOG_CHANNEL=stack