Decoding Swift_TransportException: Username and Password Not Accepted
When Laravel attempts to send an email via Gmail's SMTP server, a Swift_TransportException with error code 535 is one of the most common stumbling blocks for developers. The specific message "Username and Password not accepted" indicates that the authentication handshake failed at the protocol level, not because the connection was dropped, but because the credentials provided were rejected by the server.
Looking at the configuration you provided, several critical issues stand out immediately. The first is the mismatch between your MAIL_FROM_ADDRESS and your application's domain or identity. In your controller code, you set the sender to 'ToFromID@gmail.com'. If this address does not match the exact email address associated with your Google account credentials (or if it lacks verification), Gmail will reject the login attempt regardless of the password used.
The App Password Trap
The most frequent cause for this specific error when using Gmail is the use of a standard application password instead of an App Password, or conversely, trying to use your main Google account password with two-factor authentication (2FA) enabled.
If you have 2FA enabled on your Google account, your regular login password is no longer valid for SMTP connections. You must generate a dedicated 16-character App Password from your Google Account settings under Security > 2-Step Verification > App passwords. Using the wrong type of credential results in exactly the "535-5.7.8 Username and Password not accepted" error you are seeing.
Configuration Best Practices
Beyond the password itself, there are specific configuration adjustments required to align with modern Gmail security standards. The MAIL_ENCRYPTION setting in your .env file is currently set to tls. While this works for many setups, Gmail specifically recommends using ssl on port 465 or sticking to tls on port 587 with the correct authentication method.
However, simply changing the encryption string might not solve the problem if the underlying issue is the lack of verification on the sender address. Laravel needs to know that the email address you are claiming to be (ToFromID@gmail.com) is actually verified in your Gmail account. If it isn't, Gmail's SMTP server will reject the session immediately after authentication fails.
Verifying the Sender Address
Another subtle but critical detail lies in how you construct the from clause. Laravel allows you to specify a name and an address, but the address must be valid and verified. In your code:
$message->from('ToFromID@gmail.com', 'Ripon Uddin (Laravel Lover)');
Ensure that ToFromID@gmail.com is exactly what appears in your Gmail account's "Send mail as" settings. If you are using a custom domain for your application (e.g., noreply@yourapp.com), you cannot use a generic Gmail address without proper forwarding and verification setup, which often leads to authentication failures or spam folder delivery issues.
Troubleshooting Steps
To resolve this, follow these steps in order:
- Disable Two-Step Verification Temporarily: If you are unsure whether your password is an App Password, temporarily disable 2FA on your Google account. Try sending a test email with your standard password in the
.envfile. If it works, re-enable 2FA and generate a new App Password specifically for "Mail". - Update Credentials: Replace
MAIL_PASSWORDin your.envfile with the generated 16-character App Password. Never use your main account password if 2FA is active. - Verify Sender Address: Go to Google Account > Security > 2-Step Verification > App passwords. Ensure the email address listed there matches the one in your
MAIL_FROM_ADDRESS(which you define in the controller or via a.envvariable likeMAIL_FROM_ADDRESS). - Check for Typos: Ensure there are no extra spaces around the username or password in the
.envfile, though Laravel's syntax usually handles this well.
Final Considerations
If you continue to face authentication issues after verifying your App Password and sender address, consider checking your firewall settings. Some corporate networks block outbound connections on port 587, preventing Laravel from reaching smtp.gmail.com. Additionally, ensure that the PHP mailer package version supports the authentication method required by your specific Gmail configuration.
For more robust email handling, especially when dealing with high volumes or complex domain verification needs, you might want to explore dedicated transactional email services or consult resources like https://laravelcompany.com for deeper dives into Laravel's mail system architecture and best practices.