PHPMailer - SMTP ERROR: Password command failed when send mail from my server
When attempting to configure PHPMailer for outbound email on a shared hosting or VPS environment, you will often encounter the SMTP ERROR: Password command failed exception. This specific error message usually points to an authentication failure rather than a network connectivity issue. In the scenario where your script connects to smtp.gmail.com using SSL on port 465 but receives the error 534-5.7.14, the root cause is almost certainly related to how Gmail handles legacy applications and password security policies.
The debug log reveals the exact sequence of events:
CLIENT -> SERVER: AUTH LOGIN
CLIENT -> SERVER: bW9vcnRoaS5tcmsxMEBnbWFpbC5jb20=
CLIENT -> SERVER: OTk0MTI0MTE0MA==
SMTP ERROR: Password command failed: 534-5.7.14
534-5.7.14 i-_eumA> Please log in via your web browser and then try again.
The server is explicitly asking you to "log in via your web browser." This is a direct signal from Google that your application cannot use the standard username and password combination anymore. Starting in mid-2022, Gmail deprecated the ability for third-party applications (including scripts running on external servers) to authenticate using the primary account password due to security concerns regarding "less secure apps."
If you attempt to send mail from a script using your actual Gmail login credentials with SMTPAuth set to true and Password set to your account password, the connection will fail immediately after the login attempt. The PHPMailer library correctly identifies that the authentication was rejected by the SMTP server and throws the exception you are seeing.
To resolve this on a shared server or VPS like VPS.mydomain.com, you must stop using your primary Gmail password for programmatic access. Instead, you need to generate an App Password. This is a 16-character string generated within your Google Account settings specifically for mail applications.
Follow these steps to fix the authentication failure:
- Log in to your Gmail account via a web browser.
- Navigate to Google Account > Security.
- Ensure that "Two-Step Verification" is enabled (this is usually a prerequisite).
- Under the section "How you sign in to Google," click App passwords.
- Select "Mail" as the app and choose "Other (Custom name)" and enter something like "MyVPS_Script".
- Click Generate.
Copy the generated password; it will look like a random string without spaces. Update your PHPMailer configuration in your script to use this specific App Password instead of your main account password:
$mail->Username = "moorthi.mrk10@gmail.com";
$mail->Password = "your_generated_app_password_here"; // Replace with the 16-char string
Once you update the credentials, the SMTP ERROR should disappear, and the script will successfully authenticate. It is also important to note that if your server IP address has been previously flagged as a spammer or part of a blacklist (which is common with shared servers), Gmail might still block the connection even with correct credentials. In such cases, you may need to whitelist your server's IP address in Google Postmaster Tools or consider using an alternative SMTP provider like Mailgun or SendGrid if Gmail restrictions persist.
Finally, ensure that your PHPMailer instance is properly initialized before sending. While the code snippet provided initializes the object correctly, always verify that SMTPDebug is set appropriately during development (e.g., $mail->SMTPDebug = 2;) to capture verbose logs if future authentication issues arise. This level of detail helps distinguish between SSL handshake failures and password rejection errors quickly.
