How to set up an SMTP server in Laravel?

Laravel SMTP Setup Documentation (Using Gmail) This documentation explains how to configure SMTP in a Laravel application using Gmail.

Step 1: Gmail Account Preparation. Before using Gmail SMTP in your Laravel project, ensure the following: Enable 2-Step Verification

Generate an App Password

  • After enabling 2-Step Verification, scroll to App Passwords
  • Click on it. (If you don’t see it, make sure 2FA is enabled.)
  • Select app: Mail
  • Select device: Other → Enter: Laravel
  • Click Generate

A 16-character app password will appear. Copy this password. This will be used in the Laravel .env file.

Step 2: Configure the Laravel .env File

Open your Laravel project’s .env file and set the following:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_generated_app_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email@gmail.com
MAIL_FROM_NAME="Your App Name"

Replace your_email@gmail.com and your_generated_app_password accordingly.

Step 3: Clear Configuration Cache

After updating .env, run the following commands:

php artisan config:clear
php artisan config:cache

This ensures Laravel loads the updated SMTP settings.

eAGRfViN6bsAi6h5KV76ZvTQWiXACGqzrD3c4E24.jpg

Step 4: Sending a Test Email

You can send a test email using the following snippet:

use Illuminate\Support\Facades\Mail;
Mail::raw('This is a test email from Laravel using Gmail SMTP.', function ($message) { $message->to('receiver@example.com') ->subject('Test Email'); });