SMTP Error: Could not authenticate in PHPMailer
When PHPMailer throws the error "SMTP Error: Could not authenticate," specifically returning code 5.7.1 Username and Password not accepted, it is rarely a simple case of incorrect credentials. The fact that the same username and password work in Thunderbird confirms your account is active and your login details are valid. The issue lies in how PHPMailer interacts with Gmail's SMTP server under modern security protocols.
The root cause is almost always related to Application Passwords and the deprecation of less secure apps, rather than SSL/TLS configuration errors. Even if you have verified that OpenSSL is enabled in your php.ini and Apache has mod_ssl running, these are prerequisites for establishing a connection, not the authentication mechanism itself. The error message explicitly states that Gmail rejected the credentials provided by the script.
In modern environments, standard account passwords are no longer permitted for programmatic access to Gmail accounts. This is a security measure implemented by Google to prevent unauthorized applications from accessing your mailbox using shared or compromised login data. If you attempt to authenticate with your primary email password via SMTP port 587 (TLS) or 465 (SSL), Gmail returns the 535 5.7.1 error, regardless of whether the SSL/TLS handshake succeeds.
To resolve this, you must generate a dedicated App Password. Follow these steps:
- Log in to your Google Account using a browser.
- Navigate to Security and ensure 2-Step Verification is enabled. This requirement is non-negotiable for App Passwords.
- Go to the App passwords section (usually found under "Signing in to Google").
- Select the app type as "Mail" and the device as "Other (Custom name)" or similar.
- Generate the password. It will be an 16-character code displayed on one line.
Update your PHP script to use this specific App Password instead of your regular account password:
<?php
require_once 'vendor/autoload.php'; // Use Composer for modern setup, or include class files directly
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$Correo = new PHPMailer(true);
try {
$Correo->isSMTP();
$Correo->Host = "smtp.gmail.com";
$Correo->SMTPAuth = true;
$Correo->Username = "foo@gmail.com";
// CRITICAL: Replace this with the 16-character App Password generated in Google Account settings
$Correo->Password = "xxxx xxxx xxxx xxxx";
$Correo->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$Correo->Port = 587;
// Optional: Debugging to ensure the connection is established before auth fails
// Uncomment to see raw SMTP handshake logs if issues persist
// $Correo->SMTPDebug = 2;
$Correo->setFrom('foo@gmail.com', 'De Yo');
$Correo->addAddress('bar@hotmail.com');
$Correo->Subject = 'Prueba con PHPMailer';
$Correo->Body = '<H3>Bienvenido! Esto Funciona!</H3>';
$Correo->isHTML(true);
$Correo->send();
echo "Message Sent!";
} catch (Exception $e) {
echo "Send failed... Error info: {$Correo->errorInfo[0]}";
}
?>
Why Port 465 and TLS Both Fail with Standard Passwords
You mentioned trying both port 465 (SSL) and port 587 (TLS) without success. This is expected behavior when using a standard account password. Google treats the 535 error identically for both protocols in this context; they simply reject the credential type. Port 465 historically required SSL, while 587 requires STARTTLS, but the underlying authentication failure remains the same: "Application Password Required."
Handling Exceptions and Debugging
If you are still seeing errors after switching to an App Password, check your php.ini for OpenSSL configuration. While less common now due to library updates, older PHP versions might require explicit extension loading. However, given that Thunderbird works, the issue is strictly credential-based.
For production environments, relying on hardcoded passwordsâeven App Passwordsâis a security risk. If you are using Laravel or another framework, consider leveraging environment variables for sensitive data:
// .env file
MAIL_PASSWORD=xxxx-xxxx-xxxx-xxxx
And load it in your script:
$Correo->Password = env('MAIL_PASSWORD');
This approach aligns with modern secure coding practices. By switching to App Passwords, you bypass the 5.7.1 restriction entirely, allowing PHPMailer to authenticate successfully via TLS on port 587.