How to Send Email with Gmail SMTP in Laravel

How to send email from Laravel through Gmail SMTP: create an app password, configure your .env file, send a test email and fix the usual errors.

Mahzaib MirzaMahzaib MirzaJanuary 9, 20234 min read1 comments
How to Send Email with Gmail SMTP in Laravel

To send email from Laravel through Gmail, turn on 2-Step Verification for your Google account, create an app password, and put it in your .env file with the host smtp.gmail.com and port 587. Your regular Gmail password will not work. This guide shows each step and how to fix the errors people usually hit.

Gmail SMTP is fine for development, small projects and personal tools. For a production application, a transactional email service is the better choice (see the last section).

Before you start

You need a Gmail account with 2-Step Verification turned on. Google only lets you create app passwords on accounts that have it. App passwords are not available if 2-Step Verification is set up only with security keys, if the account has Advanced Protection, or if you are signed in with a work, school or other organization account. Google also says app passwords are unnecessary in most cases and recommends signing in with Google where an app supports it. For a Laravel app sending through SMTP, an app password is the simplest option.

Step 1: Turn on 2-Step Verification

Open your Google Account, go to Security, and turn on 2-Step Verification if it is not already on. Follow the prompts to add your phone as a second step.

Step 2: Create an app password

Go to myaccount.google.com/apppasswords, give the app a name such as "Laravel", and create the password. Google shows a 16-character password. Copy it now and keep it somewhere safe. If you change your Google account password later, your app passwords are revoked and you will need to create new ones.

Step 3: Configure Laravel

Open your .env file and set the mail values:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=your-16-character-app-password
[email protected]
MAIL_FROM_NAME="Your App"

Enter the app password without spaces. Keep MAIL_FROM_ADDRESS the same as your Gmail address, because Gmail replaces a different sender address with the account you authenticated as.

Google lists two ways to connect: port 587 with STARTTLS, or port 465 with implicit SSL/TLS. If you want port 465, set MAIL_PORT=465. Recent Laravel versions choose the right connection type from the port, and the MAIL_SCHEME variable in newer projects (or MAIL_ENCRYPTION in older ones) lets you set it explicitly. If your .env already has one of them, leave it as it is for port 587.

After editing .env, clear the cached configuration so Laravel reads the new values:

php artisan config:clear

Step 4: Send a test email

The quickest test is Laravel Tinker. Start it with php artisan tinker and run:

use Illuminate\Support\Facades\Mail;

Mail::raw('This is a test email sent through Gmail SMTP.', function ($message) {
    $message->to('[email protected]')->subject('Gmail SMTP test');
});

If it returns without an error, check the receiver's inbox, and the spam folder. To send a proper HTML email from a mailable class and a Blade view, follow how to send email in Laravel. The mail settings above are the only Gmail-specific part.

Fixing common errors

Username and Password not accepted

This almost always means you used your normal Gmail password instead of the app password, 2-Step Verification is off, or the app password was revoked after a password change. Create a new app password and update MAIL_PASSWORD, then run php artisan config:clear.

The app passwords page is missing

The page is hidden when 2-Step Verification is off, when it is set up only with security keys, or when the account is a work, school or Advanced Protection account. Workspace administrators can also restrict it. Check those first.

Connection timed out

Some hosting providers and networks block outgoing SMTP ports. Try the other port (587 or 465), and ask your host whether outgoing SMTP is allowed.

Changes to .env have no effect

Run php artisan config:clear. If you cache configuration in production, rebuild the cache after changing .env.

Limits and when to use something else

Gmail limits how many messages an account can send per day, and it is not designed for bulk or transactional mail. If your application sends password resets, receipts or newsletters, use a dedicated email service and keep Gmail for development. Once sending works, you can send to multiple recipients and queue the emails.

Share:
Mahzaib Mirza

Written by

Mahzaib Mirza

Software developer & Founder of Coders Vibe.

Related Posts

Liked this post?

Get the next one in your inbox the moment it's published. No spam, unsubscribe anytime.

0 Comments

Leave a comment