DEV Community

Cover image for How to Send Email Using Gmail SMTP in Laravel 13 with an App Password
Muhammad Yasir Amin
Muhammad Yasir Amin

Posted on Originally published at yasiramin.dev

How to Send Email Using Gmail SMTP in Laravel 13 with an App Password

To send email from Laravel 13 with Gmail, set the SMTP host to smtp.gmail.com, use port 587 with TLS (or 465 with SSL), and log in with your Gmail address and an app password, not your normal password. Google no longer allows "less secure apps", so the app password is the supported way. This guide has the exact .env values, a Mailable, a test route and fixes for typical errors.

Targets: Laravel 13 (PHP 8.3 to 8.5). Gmail SMTP is fine for testing and small projects; for production volume use a transactional email service.

Step 1: Create a Google app password

  1. Turn on 2-Step Verification for the Google account that will send the mail.
  2. Open the account security page and find App passwords. If you do not see it, the account may be managed by an organization or 2-Step Verification is not active.
  3. Create an app password with a name such as "Laravel" and copy the 16-character value.

Treat the app password like a real password. You can revoke it at any time from the same page.

Step 2: Configure .env

MAIL_MAILER=smtp
MAIL_SCHEME=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-address@gmail.com
MAIL_PASSWORD=your-16-character-app-password
MAIL_FROM_ADDRESS=your-address@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Enter fullscreen mode Exit fullscreen mode

Port 587 uses STARTTLS and is the usual choice. If you prefer port 465 with implicit TLS, use MAIL_PORT=465 and set MAIL_SCHEME=smtps. Do not put the spaces from the Google display into the password; paste it without spaces.

After changing .env, clear the cached configuration:

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Mailable

php artisan make:mail WelcomeMail
Enter fullscreen mode Exit fullscreen mode
<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

class WelcomeMail extends Mailable
{
    public function __construct(public string $name) {}

    public function envelope(): Envelope
    {
        return new Envelope(subject: 'Welcome to our app');
    }

    public function content(): Content
    {
        return new Content(view: 'emails.welcome');
    }
}
Enter fullscreen mode Exit fullscreen mode

Create the view at resources/views/emails/welcome.blade.php:

<p>Hello {{ $name }},</p>
<p>Thanks for signing up.</p>
Enter fullscreen mode Exit fullscreen mode

Step 4: Send a test email

use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;

Route::get('/send-test-mail', function () {
    Mail::to('recipient@example.com')->send(new WelcomeMail('Ali'));

    return 'Mail sent.';
})->middleware('auth');
Enter fullscreen mode Exit fullscreen mode

Open the route while logged in and check the recipient's inbox (and the spam folder). Remove the route afterwards so nobody can use it to send mail.

You can also test from the terminal:

php artisan tinker --execute="Mail::raw('Test message', fn (\$m) => \$m->to('recipient@example.com')->subject('SMTP test'));"
Enter fullscreen mode Exit fullscreen mode

How do I send email in the background?

SMTP calls take time. Queue the email so the user does not wait:

Mail::to($user)->queue(new WelcomeMail($user->name));
Enter fullscreen mode Exit fullscreen mode

Run a queue worker (php artisan queue:work) so queued messages are sent. If you want a Mailable to always be queued, implement ShouldQueue on it.

Gmail sending limits

Google limits how many messages an account can send per day, and limits are lower for free accounts than for Google Workspace accounts. If you exceed them, sending is blocked for a while. Check Google's current limits before using Gmail for bulk or customer email, and move to a transactional email provider for production.

Common errors and fixes

  • Username and Password not accepted: you used the normal account password, or 2-Step Verification is off. Create an app password.
  • Connection timed out: your host blocks outgoing SMTP ports. Try the other port or ask the host.
  • Changes to .env ignored: run php artisan config:clear, and restart workers with php artisan queue:restart.
  • Mail lands in spam: make sure the From address matches the Gmail account that sends it.
  • App password option missing: 2-Step Verification must be on, and some managed accounts disable app passwords.

FAQ

Can I use my normal Gmail password?

No. Google blocks normal-password SMTP logins for ordinary accounts. Use an app password, or OAuth through a dedicated package.

Should I use Gmail SMTP in production?

For small internal apps it can work. For customer-facing email, use a service built for it, which gives better deliverability and clearer limits.

How do I see the email without sending it?

Set MAIL_MAILER=log. Laravel then writes the message to the log file instead of sending it.

References


Originally published on yasiramin.dev.

Top comments (0)