2026-07-15

How to to send mail using gmail in Laravel?

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

How to to send mail using gmail in Laravel?

How to Send Mail Using Gmail in Laravel: A Comprehensive Guide

Sending emails programmatically from a Laravel application is a fundamental task, but configuring external services like Gmail via SMTP can often lead to frustrating authentication errors. If you are struggling to send mail using your Gmail account within Laravel, the issue is rarely with the Laravel code itself, but usually lies in the security settings of your Google account or the specific configuration parameters used for the SMTP connection.

As a senior developer, I’ve seen countless developers run into this exact roadblock. The key to successfully sending emails through Gmail—or any external SMTP service—is understanding how modern security protocols interact with older authentication methods.

This guide will walk you through the entire process, focusing on the necessary configuration steps and troubleshooting common pitfalls to ensure reliable email delivery from your Laravel application.

Understanding the Authentication Challenge with Gmail

Gmail, like many major email providers, has tightened security significantly. Simply using your regular Google password for SMTP authentication often fails because of Two-Factor Authentication (2FA) or stricter security policies. You cannot use your standard login password directly if 2FA is enabled on your account.

To resolve this, the most secure and recommended method for connecting external applications to your Gmail account via SMTP is by generating an App Password. This allows you to create a unique, restricted password specifically for your application, keeping your main Google account secure.

Step 1: Generating an App Password for Gmail

Before touching your Laravel configuration, you must ensure you have the correct credentials from Google.

  1. Enable 2-Step Verification: Ensure 2-Step Verification is turned on for your Google Account.
  2. Generate the App Password: Go to your Google Account Security settings and navigate to the App Passwords section.
  3. Create a New Password: Generate a new password specifically for an application. This generated string is what you will use in your Laravel configuration instead of your regular Gmail password.

Step 2: Configuring Laravel Environment Variables

Once you have the necessary App Password, you need to update your .env file to point Laravel toward the correct SMTP server details. Based on the configuration snippets you provided, here is how we structure this for a successful connection with Gmail’s SMTP server.

In your .env file, ensure the settings are accurate:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_full_email@gmail.com  # Use your full Gmail address
MAIL_PASSWORD=your_generated_app_password # Use the App Password here
MAIL_ENCRYPTION=tls

A Note on Security: Notice that we use tls for encryption, which is standard practice for secure SMTP communication over port 587. Always treat these credentials as sensitive information. For more information on structuring modern Laravel applications, exploring resources from laravelcompany.com is highly recommended.

Step 3: Reviewing the Mail Configuration File

Next, ensure your config/mail.php file reflects these environment settings correctly. The structure you provided is generally correct for an SMTP setup:

<?php

return [
    'driver' => env('MAIL_DRIVER', 'smtp'),

    'host' => env('MAIL_HOST', 'smtp.gmail.com'),

    'port' => env('MAIL_PORT', 587),

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'your_full_email@gmail.com'),
        'name' => env('MAIL_FROM_NAME', 'Laravel App'),
    ],

    'encryption' => env('MAIL_ENCRYPTION', 'tls'),

    'username' => env('MAIL_USERNAME'),

    'password' => env('MAIL_PASSWORD'),

    // ... other settings
];

The crucial part is ensuring that the username and password fields correctly pull the specific credentials you set in the .env file. If you are still facing errors (like connection timeouts or authentication failures), double-check:

  1. App Password Validity: Ensure the App Password has not expired or been revoked by Google.
  2. Firewall/Port Blocking: Verify that your local server or hosting environment is not blocking outbound traffic on port 587.
  3. Gmail Settings: Confirm that "Less secure app access" is disabled (as it is now) and that you are using the correct SMTP configuration for Gmail accounts.

Conclusion

Sending mail via Gmail in Laravel requires bridging the gap between your application's needs and Google's strict security protocols. By correctly generating an App Password and meticulously configuring the MAIL_USERNAME and MAIL_PASSWORD in your environment variables, you can establish a robust and reliable SMTP connection. Remember that secure development practices, including handling sensitive credentials properly, are central to building secure applications. For deeper insights into Laravel architecture and best practices, always refer to official documentation and community resources like those provided by laravelcompany.com.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.