Laravel Gmail not working, "Username and Password not accepted. Learn more..."
You are staring at a Swift_TransportException in your Laravel logs. The error code is 535, and the message from Google explicitly states: "Username and Password not accepted." This specific error usually stems from a fundamental misunderstanding of how Gmail authentication has evolved over the last decade, rather than a misconfiguration within your Laravel application or SwiftMailer library.
The configuration snippet you provided relies on standard SMTP credentials using SSL on port 465. While this setup was perfectly valid for years, Google deprecated "Less Secure Apps" and subsequently tightened its security policies to require OAuth 2.0 for third-party applications attempting to access a Gmail account. If your Laravel application is running on an external server or even a local environment not explicitly trusted by Google, the standard username/password combination will be rejected regardless of whether you have disabled two-factor authentication in your Google Account settings.
The core issue lies in the distinction between sending email from your domain versus authenticating as a personal Gmail user. If you are trying to send emails using your own domain (e.g., support@yourcompany.com) but pointing the from address to a personal Gmail account, this is generally blocked by Google's spam protection mechanisms. However, if you intend to use a personal Gmail account as the sender, you must migrate from basic authentication to OAuth 2.0.
Switching to App Passwords or OAuth 2.0
If you cannot implement the full OAuth 2.0 flow immediately, the most direct fix is generating an App Password. This bypasses the need for your primary Gmail account password while satisfying Google's security requirements for legacy SMTP clients like SwiftMailer.
To resolve this without a code overhaul:
- Log in to your Google Account.
- Navigate to Security settings and ensure you are logged into the account you intend to use.
- Look for the option to generate an "App Password". You will need to select the app category as "Mail" and specify your device.
- Copy the generated 16-character password.
Update your .env file in Laravel accordingly:
MAIL_USERNAME=mymail@gmail.com
MAIL_PASSWORD=xxxx xxxx xxxx xxxx # Insert App Password here, not your main login password
The Case for OAuth 2.0
While App Passwords work, they are a transitional solution. The long-term architectural decision for any production Laravel application is to implement OAuth 2.0. This method provides a secure token rather than a static password, allowing Google to revoke access if the credentials are compromised.
Implementing OAuth in Laravel typically requires installing the barryvdh/laravel-dompdf or similar packages that handle the complex flow of obtaining tokens and refreshing them. Alternatively, you can use the google/plusone-api or specific Gmail API wrappers. The trade-off here is increased complexity in your backend logic; you must manage token expiration and refresh mechanisms.
Verifying Authentication Settings
Before assuming the authentication method is the sole culprit, verify two critical settings on the Google side:
- Two-Factor Authentication (2FA): If enabled, standard passwords will fail immediately unless an App Password is used.
- App Access: Ensure "Less secure app access" is explicitly disabled in your Google security settings. Even if you think you haven't changed it, this setting often defaults to "Off" by default now.
Using a Dedicated SMTP Service
If your Laravel application requires high volume or stability, relying on personal Gmail accounts as an SMTP relay introduces significant risk. Google frequently changes its API terms and rate limits. For professional applications, the industry standard is to migrate away from consumer email providers like Gmail for transactional emails. Instead, use dedicated services like SendGrid, Mailgun, or Amazon SES. These services provide built-in authentication via API keys and offer better deliverability guarantees without the headache of managing OAuth tokens or App Passwords.
If you are still committed to using a Gmail address for the sender identity, ensure that the email is verified in your Google Account settings under "Apps" > "Access apps & Google APIs." If you do not see your app listed, you may need to create a new project in the Google Cloud Console to generate credentials.
Ultimately, the 535 error is not a bug in SwiftMailer; it is a gatekeeper function from Google enforcing modern security standards. Adapting your authentication strategy to align with these standardsâvia App Passwords or OAuth 2.0âis the only path forward for reliable email delivery.