Failed to authenticate on SMTP server error using gmail

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Troubleshooting Mail Authentication Issues in Laravel App Using Gmail SMTP Body:

When setting up your first email for a Laravel project, you might come across an authentication error while using Gmail as the SMTP server. This problem is quite common and may seem intimidating to developers who are new to working with email systems. In this article, we will provide a comprehensive guide on how to troubleshoot and resolve mail authentication issues related to Gmail SMTP in Laravel applications.

Understanding the Authentication Error

The error you encounter could be due to several factors, such as incorrect SMTP server configuration or authentication problems. Here's an example of the error message: "Swift_TransportException in AuthHandler.php line 181: Failed to authenticate on SMTP server with username 'your_email' using 3 possible authenticators."

Checking Your SMTP Settings

The first thing you need to check is your SMTP server information. Ensure that the username and password provided in your Laravel mail configuration (settings within .env file) are correct. To verify this, follow these steps:

1. Sign into your Gmail account. 2. Go to My Account, then click on "Signing in to Google" under the Sign-in & Security section. 3. Verify that 'Allow less secure apps: ON' is enabled if you use an older Gmail account. If not, enable it or create a new application-specific password by clicking "Set up app-specific password." 4. Copy your username and password for your Gmail account as they might be different from the email address. 5. Update the mail settings in the .env file with the correct credentials:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME='your-email'
MAIL_PASSWORD='app-specific password or Gmail password'
MAIL_ENCRYPTION=tls

Configuring the Access to Less Secure Apps Setting

If you have already enabled the 'Allow less secure apps,' your Laravel application might still encounter issues due to Google's stricter security measures. In this case, you will need to create a new app-specific password for your Gmail account and use it in your Laravel project:

1. Go back to My Account, click on "Signing in to Google," then choose 'App Passwords.' 2. Click 'Select App,' select 'Other (Custom name)' and enter a relevant name, like 'Laravel Emailer.' 3. Select the app as you see fit (either Mail or Manage your email). 4. Copy the generated 16-character code which should be used as your new 'MAIL_PASSWORD' in your .env file.

Testing Your SMTP Configuration

Now that you have made changes to your SMTP configuration, test if the error persists:

1. Run php artisan config:clear and restart your Laravel application. 2. Send a test email using your new credentials by running the command php artisan tinker or php artisan mail:test. 3. Check your Gmail inbox to see if you receive the test message. If not, ensure that you have enabled the option to allow less secure apps and used a valid app-specific password or email credentials.

Other Potential Solutions

If the issue persists, try these additional solutions:

1. Update your .env file with Gmail IMAP settings by changing 'MAIL_DRIVER=smtp' to 'MAIL_DRIVER=imap.' This may require changes in your email configuration (config/services.php and config/mail.php) as well. 2. Use a third-party SMTP service, such as Mailgun or SendGrid, for improved delivery rates and better security features. 3. If you're using Docker, make sure your .env file is included in the docker-compose.yml file, and run docker-compose up to update the application with the new environment variables.

Conclusion

Troubleshooting SMTP authentication errors can be time-consuming, but by following these steps, you should have a better understanding of how to configure your Laravel app for successful email sending using Gmail as the SMTP server. Keep in mind that if all else fails, turning off "Allow less secure apps" and generating an application-specific password or email credentials might resolve your issues.

Additional Resources

Laravel Mail Documentation