2026-07-15

PHPMailer sends with TLS even when encryption is not enabled

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

PHPMailer sends with TLS even when encryption is not enabled

The PHPMailer Paradox: Why TLS Insists When Encryption is Disabled

As senior developers, we often encounter situations where a seemingly simple configuration leads to complex troubleshooting sessions. Recently, I came across a frustrating issue involving PHPMailer: attempting to send an email without encryption, yet the library insists on negotiating a secure connection (TLS/SSL), resulting in cryptic errors like "TLS not available."

This post dives deep into why this happens, analyzes the provided error log, and provides a robust solution for controlling SMTP security settings correctly.


Understanding the PHPMailer Security Mechanism

The issue stems from how SMTP (Simple Mail Transfer Protocol) servers handle communication, particularly when using common ports like 587 or 465. These protocols are designed to secure email transmission, and most modern mail servers default to requiring encryption.

When you configure $mail->SMTPSecure in PHPMailer, you are telling the client (PHPMailer) which security layer to use for the connection negotiation:

  • ssl (or SMTPSecure = 'ssl'): This forces a direct SSL/TLS encrypted connection from the start. This is typically used on port 465.
  • tls (or SMTPSecure = 'tls'): This initiates a plaintext connection and then issues the STARTTLS command to upgrade the connection to TLS encryption mid-session. This is the standard for port 587.

In your specific case, when you commented out or removed the security setting, PHPMailer likely defaulted to trying the standard secure negotiation (like STARTTLS on port 587). If the target server (site.com.br in this example) does not correctly respond to the STARTTLS command, as indicated by the error STARTTLS command failed: 454 TLS not available due to temporary reason, the entire connection fails, even if you intended for it to be unencrypted.

Analyzing the Error Log and Configuration

The log output clearly points to a server-side refusal during the handshake process:

CLIENT -> SERVER: EHLO www.johnmendes.com.br
CLIENT -> SERVER: STARTTLS
SMTP ERROR: STARTTLS command failed: 454 TLS not available due to temporary reason
SMTP Error: Could not connect to SMTP host.

This confirms that the server is actively rejecting the request to initiate the STARTTLS handshake because it either doesn't support this method for that connection or has an internal configuration blocking the request. The problem is less about PHPMailer failing to disable TLS and more about the server refusing the protocol requested by the client.

Practical Solution: Controlling the Connection Safely

If your goal is genuinely to send email over a plaintext connection (which is strongly discouraged for security reasons in production environments, but sometimes necessary for specific testing endpoints), you must ensure PHPMailer uses the correct setting that matches what the server expects.

Since you are using port 587, which traditionally implies STARTTLS encryption, and the server rejected it, the safest approach is to try forcing an explicit, non-encrypted connection if the SMTP service supports it (which many do not). If that fails, you must re-enable SSL/TLS, as this is the industry standard.

Here is how you should structure your configuration:

Option 1: Enforcing Standard Security (Recommended)

For almost all modern mail servers, enabling TLS is mandatory for security. If the connection fails with STARTTLS, it usually means the server requires a full SSL handshake instead.

$mail = new PHPMailer(true); // true enables exceptions
$mail->isSMTP();
$mail->Host       = 'site.com.br';
$mail->SMTPAuth   = true;
$mail->Username   = 'contact@site.com.br';
$mail->Password   = 'pass';
$mail->Port       = 465; // Use port 465 for explicit SSL/SMTPS connection
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Forces SSL/TLS encryption on Port 465
// ... rest of the setup

Option 2: Attempting Plaintext (Use with Extreme Caution)

If you absolutely must attempt a plaintext connection, try switching to port 25 or ensure that no security parameters are set. However, be aware that this path is highly likely to fail in modern hosting environments and should only be used for internal testing where security is not a concern.

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host       = 'site.com.br';
$mail->Port       = 25; // Try standard unencrypted port
$mail->SMTPSecure = false; // Explicitly disable the security layer

// ... rest of the setup

Conclusion

The mystery of PHPMailer insisting on TLS even when you try to disable it is a classic symptom of mismatched expectations between the client library and the server's protocol handling. The solution lies not in tricking PHPMailer, but in correctly aligning your configuration (SMTPSecure, Port) with what the specific SMTP server expects. Always prioritize security; configuring for TLS/SSL (Option 1) ensures reliable, secure email delivery, which is a core principle reflected in robust application design, much like the principles used when building applications on frameworks like those provided by Laravel. Debugging these low-level network interactions requires understanding the underlying protocols, not just the library syntax.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.