Resolving Gmail SMTP Error 535 in Laravel: Username and Password Not Accepted

Dec 03, 2025 · Programming · 20 views · 7.8

Keywords: Laravel | Gmail SMTP | Error 535 | Two-Step Verification | App Password

Abstract: This article provides an in-depth analysis of error code 535 encountered when using Gmail SMTP to send emails in the Laravel framework, typically triggered by Google's security mechanisms. Based on a real-world case, it systematically explains the root cause—Google's restrictions on third-party app access—and offers a solution through enabling two-step verification and generating an app password. Step-by-step guidance on configuring environment variables and restarting the server ensures email functionality is restored. Additionally, it discusses alternative approaches like enabling 'less secure app access' and unlocking captchas, emphasizing two-step verification as the most reliable method. Aimed at developers, this article serves as a comprehensive troubleshooting guide to save time on similar issues.

When integrating Gmail SMTP services to send emails in the Laravel framework, developers may encounter error code 535, with a message indicating "Username and Password not accepted." This error is often not due to code logic issues but rather Google's security mechanisms blocking third-party app access. This article delves into the causes of this error and presents an effective solution.

Error Phenomenon and Context

When attempting to send emails via Gmail SMTP, the system might return an error similar to:

Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted."

This typically occurs during frequent send requests or when Google detects unusual activity, denying access even with correct credentials. For example, in Laravel's .env file configured as:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=talentscoutphil@gmail.com
MAIL_PASSWORD=mypasswordhere
MAIL_FROM=talentscoutphil@gmail.com
MAIL_NAME=talentscout

Despite correct configuration, the error may persist, indicating the issue stems from Google's security policies rather than local settings.

Root Cause Analysis

The core cause of error 535 is Google's restriction on third-party apps (e.g., Laravel applications) using standard passwords to access Gmail SMTP services, as a security measure. Google may view such access as a potential threat, especially when detecting rapid or bulk requests, triggering security mechanisms that lead to authentication failure. This explains why the problem can occur intermittently, even after the account owner confirms access.

Solution: Enable Two-Step Verification and App Passwords

The most effective solution is to enable Google's two-step verification and generate a dedicated app password. Here are the detailed steps:

  1. Visit Google's two-step verification page (e.g., https://www.google.com/landing/2step/) and enable the feature to enhance account security.
  2. In Google security settings (e.g., https://security.google.com/settings/security/apppasswords), generate an app password. Select the "Other (Custom name)" option, name the app (e.g., "Laravel Mail"), and click generate.
  3. Replace the MAIL_PASSWORD value in Laravel's .env file with the generated app password. For example:
    MAIL_PASSWORD=generatedapppassword123
    Ensure MAIL_USERNAME remains the Gmail address.
  4. Restart the web server (e.g., Apache) to apply the changes. After this, email sending functionality should resume immediately.

This approach works because app passwords are tokens designed for third-party apps, bypassing Google's restrictions on standard passwords while maintaining account security.

Alternative Potential Solutions

Beyond the above method, developers might try other approaches, but with limited effectiveness:

In contrast, two-step verification with app passwords offers a more stable and secure solution, recommended as the primary method.

Code Examples and Configuration Verification

In Laravel, ensuring correct mail configuration is crucial. Here is an example configuration demonstrating integration with an app password:

// In the .env file
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-generated-app-password  // Use app password instead of regular password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your-email@gmail.com
MAIL_FROM_NAME="Your App Name"

In code, emails can be sent via Laravel's Mail facade, for example:

use Illuminate\Support\Facades\Mail;

Mail::to('recipient@example.com')->send(new WelcomeMail());

If configured correctly, the send operation should succeed, avoiding error 535.

Summary and Best Practices

Error 535 is a common hurdle in interactions between Google's security mechanisms and third-party apps. By enabling two-step verification and using app passwords, developers can reliably resolve this issue while upholding account security. It is advisable to periodically review Google security settings and update app passwords as needed. For Laravel projects, always store sensitive information in the .env file and avoid hardcoding passwords in code. If issues persist, checking network connectivity or Google service status may also help. The steps provided in this article, based on a real-world case, aim to assist developers in efficiently troubleshooting and saving time on debugging.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.