"Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer

"Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why SMTP Authentication Fails on Shared Servers When Sending via Gmail

You are encountering a classic environment-specific failure mode: code that functions perfectly in your local XAMPP environment suddenly breaks when deployed to a production host. The error 535 Incorrect authentication data combined with the specific "not permitted to relay" message indicates that while PHPMailer is attempting to connect, the shared hosting server is rejecting the credentials or blocking the connection entirely.

The root cause rarely lies within your PHP script's syntax; instead, it almost always stems from how the hosting provider manages server-level security policies. When you run the script from your localhost, your machine can establish a direct SSL/TLS handshake with smtp.gmail.com. However, many shared hosting providers operate their outbound mail relays differently. The error message explicitly states: [205.234.141.238]:50958 is not permitted to relay through this server without authentication.

This specific phrasing often triggers a defensive mechanism in the hosting environment known as an open relay protection or IP blacklisting block. If your host has previously allowed unauthenticated mail traffic from their IP range, or if they have flagged your account for sending bulk emails, they may be blocking outbound SMTP connections to Gmail's servers entirely to prevent abuse. The 535 error confirms that the server received a login attempt but rejected it.

The Shared Hosting Restriction Trap

The most common reason this works on localhost but fails on the host is not your code, but the network path. When you test on XAMPP, you are connecting from your home IP address using your own computer's SSL certificate validation (unless explicitly disabled). On a shared server, the connection originates from the hosting provider's data center.

Some hosting providers restrict outbound traffic to specific ports or domains. If the host has flagged your domain or IP for spam activity in the past, they might have blacklisted you at the network level before your PHP script even runs. The error Please turn on SMTP Authentication is a standard SMTP response code, but when paired with "not permitted to relay," it suggests the server is refusing to trust any connection originating from that host's IP range for Gmail authentication.

You cannot fix this by tweaking $mail->SMTPDebug or adjusting the charset in your PHPMailer configuration. The issue exists outside the application layer. You must verify whether your hosting provider allows outbound connections to smtp.gmail.com on port 465 (SSL) or requires you to use a different port like 587 (TLS).

Implementing Appropriate Authentication Methods

Your current code relies on the SSL prefix ($mail->SMTPSecure = "ssl" and Port 465). While this is valid, modern hosting environments often prefer TLS on port 587. Switching to port 587 with SMTPSecure = 'tls' can sometimes bypass legacy firewall rules that block direct SSL handshakes from specific IP ranges.

Additionally, hardcoding credentials in a global variable passed to the function is a significant security risk. You should utilize environment variables or a .env file structure if your host supports it (e.g., Laravel's config/app.php or standard PHP configuration). Never commit these values to version control. If you are using a framework like Laravel, ensure that the MAIL_MAILER, MAIL_HOST, and related settings in your .env file match the requirements of your specific hosting plan.

The "App Password" Requirement

A critical detail often overlooked when moving from local development to production is Gmail's own security policies. If you are using a standard account password, Gmail will reject the connection if Multi-Factor Authentication (MFA) is enabled on that account. This is a strict security feature enforced at the SMTP level.

If your account has 2-Step Verification enabled, the server-side 535 error is almost certainly because you are sending a regular password instead of an "App Password." You must generate a specific App Password within your Google Account settings under Security > 2-Step Verification > App passwords. This generates a unique string specifically for mail applications. If you are using OAuth2 authentication (less common with PHPMailer's standard implementation unless using the latest Composer packages), the flow differs entirely, but for legacy class.phpmailer, an App Password is the only viable solution if MFA is active.

Final Verification Steps

If switching ports and using App Passwords does not resolve the issue, you must contact your hosting provider's support team directly. Show them the full error log, specifically the part mentioning "not permitted to relay." They may need to whitelist smtp.gmail.com in their outbound firewall rules or unblock your specific IP address from their spam blacklist. There is no configuration setting within your PHP script that can override a network-level block imposed by your hosting provider.

Our network