Decoding SMTP Error 535-5.7.8 on Gmail
When attempting to send an email through Laravel's SwiftMailer using Gmail as the transport, encountering the error "Expected response code 250 but got code '535', with message 535-5.7.8 Username and Password not accepted" is a common frustration. This specific SMTP response indicates that the server rejected the authentication credentials provided in the EHLO/STARTTLS sequence. It is rarely a bug in the Laravel framework itself or a misconfiguration of the env file variables like MAIL_HOST, MAIL_PORT, or MAIL_USERNAME. Instead, it usually points to a security policy enforced by the mail providerâin this case, Google's strict stance on application authentication.
The core issue often stems from the legacy nature of standard SMTP authentication. Historically, developers would pass their Gmail account password directly into the MAIL_PASSWORD environment variable. However, Google deprecated third-party application passwords and mandatory 2FA support years ago, effectively blocking any application attempting to log in with a regular Google account password over unencrypted or standard TLS connections without explicit permission.
If your application is running on a server that does not have an IP address whitelisted by Google, or if the connection is not properly secured, Gmail will reject the handshake. The error message explicitly states the username and password were not accepted, which can be misleading if you are sure your credentials are correct. In many scenarios involving Gmail, the "password" in the code is not your actual account password anymore.
To resolve this, you must leverage Google's Application Passwords feature. This process involves two steps: enabling Two-Factor Authentication (2FA) on your personal Google account and then generating a dedicated 16-character application-specific password. Once generated, you replace the contents of MAIL_PASSWORD in your .env file with this specific token, not your main account password.
Here is how the corrected configuration should look:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=talentscoutphil@gmail.com
MAIL_PASSWORD=abcd efgh ijkl mnop
MAIL_ENCRYPTION=tls
MAIL_FROM=talentscoutphil@gmail.com
MAIL_NAME=talentscout
Note the inclusion of MAIL_ENCRYPTION=tls. While Gmail supports SSL on port 465 and TLS on port 587, explicitly setting the encryption method in older versions of Laravel (prior to 10.x) or when using SwiftMailer directly via createTransport is often necessary to ensure the connection is upgraded correctly before sending credentials. If you are using a newer version of Laravel where MAIL_ENCRYPTION defaults to null and relies on the host configuration, ensuring port 587 is used with TLS is critical.
Another frequent pitfall involves IP reputation and rate limiting. As mentioned in similar incident reports, Google may temporarily block an account if it detects suspicious activity, such as sending too many emails in a short window from an unknown server. If you recently enabled 2FA or changed your app password but still see the 535 error, check for a temporary suspension via Google's security dashboard. In some cases, simply waiting a few hours after confirming identity allows the block to lift.
If you are managing multiple applications and cannot change passwords frequently, consider using an SMTP service that relays mail for you, such as Mailgun or SendGrid. These services handle the TLS handshakes and authentication complexity, abstracting away the need to manage Gmail's specific security quirks. You can configure Laravel to use these services by simply changing the MAIL_HOST and MAIL_PORT in your environment file.
Finally, ensure you are not inadvertently exposing credentials. The standard .env file should be in your .gitignore. Never commit the actual application password or account password to version control, as this creates a significant security liability regardless of the SMTP error status.
For those looking for deeper integration patterns or best practices regarding mail delivery in modern PHP stacks, resources like https://laravelcompany.com offer additional context on setting up reliable background job queues that handle email dispatching asynchronously, preventing rate-limiting issues entirely by staggering send times.
