PHPMailer: SMTP Error: Could not connect to SMTP host

PHPMailer: SMTP Error: Could not connect to SMTP host

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

Stefan Bogdanescu

Founder & Senior Architect

PHPMailer: SMTP Error: Could not connect to SMTP host

The error message SMTP Error: Could not connect to SMTP host is one of the most common and frustrating issues in PHP email development. It usually points to a mismatch between your application's configuration and the actual requirements of the mail server, rather than a broken library. In this specific scenario, the discrepancy lies entirely in how you are handling connection security and port negotiation.

You mentioned that sending via Thunderbird works perfectly with mail.exampleserver.com on port 587 using STARTTLS, but PHPMailer fails. Your current code configuration uses SMTP_HOST, SMTP_PORT, and sets $mail->SMTPAuth = true. The immediate red flag is the missing security layer in your PHP code.

The Critical Missing Piece: SMTPSecure

The most likely culprit for a "Could not connect" error when switching from one server configuration to another is that you are attempting to use STARTTLS without enabling it in the PHPMailer instance.

Your Thunderbird settings explicitly show Connection Security: STARTTLS on port 587. In your PHP code, however, you have not set the $mail->SMTPSecure property. By default, if this property is omitted or set to null, PHPMailer assumes phpmailer.default_smtp_secure behavior, which often defaults to no encryption or attempts plain text connections that some modern servers reject immediately upon handshake.

To replicate your working Thunderbird configuration, you must explicitly tell PHPMailer to upgrade the connection to TLS after connecting:

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'mail.exampleserver.com'; // Replace with SMTP_HOST
$mail->Port = 587;                     // Must match your server
$mail->SMTPAuth = true;
$mail->Username = 'user@exampleserver.com'; // Replace with SMTP_USER
$mail->Password = 'your_secure_password';   // Replace with SMTP_PASSWORD
$mail->From = 'MAIL_SYSTEM';
$mail->FromName = 'MAIL_SYSTEM_NAME';

// THIS IS THE MISSING LINE FOR STARTTLS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; 

Without this line, the client attempts to connect on port 587 without encrypting the session. Many modern mail servers (including mail.exampleserver.com) are configured to reject unencrypted connections on port 587 or require the TLS handshake before accepting any data, resulting in an immediate connection refusal that manifests as "Could not connect."

SSL vs. STARTTLS Distinction

It is important to distinguish between your previous environment and the current one. Your last project used port 465 with SSL/TLS. This requires a different configuration entirely:

  • Port 465: Requires $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;.
  • Port 587: Requires $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;.

Mixing these up is a frequent mistake. If you copy-paste settings from an SSL environment (465) into a STARTTLS environment (587), or vice versa without adjusting the SMTPSecure property and the port number, the TCP handshake will likely fail. PHPMailer does not automatically negotiate protocol upgrades; it relies on explicit instruction via the SMTPSecure constant.

Authentication Pitfalls

While less common than security mismatches, another potential cause for connection issues is authentication mode. Your Thunderbird config shows "Secure Authentication: No". In legacy terms, this might imply PLAIN authentication over TLS. PHPMailer handles this by default when SMTPAuth is true, but ensure that your server does not require a specific capability string or that you aren't inadvertently forcing SSL on a port that demands plain STARTTLS.

If the connection still fails after adding SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS, check your firewall settings. Some servers block incoming connections to port 587 from external IPs unless they are properly authenticated, though this usually results in an authentication error rather than a connection timeout.

For further reading on implementing robust email delivery patterns in modern PHP stacks, including best practices for handling SMTP credentials securely and avoiding hardcoded secrets, you might explore resources like https://laravelcompany.com which often covers infrastructure setup details relevant to enterprise applications.

Our network