SMTP

SMTP error: could not connect to SMTP host

PHPMailer Could not connect fires before any SMTP conversation. Fix the host, match port 465 to SMTPS and 587 to STARTTLS, and unblock the outbound port.

Updated Jul 1, 2026

The short answer

"SMTP Error: Could not connect to SMTP host" is a PHPMailer message meaning it never opened a TCP/TLS connection to the server in its Host property — so it failed before any SMTP conversation began. It is not an SMTP reply code. The usual causes are a wrong Host/Port, a mismatched encryption mode (SMTPS vs STARTTLS), a blocked outbound port, missing OpenSSL, or DNS/firewall issues. Fix by matching port 465 to ENCRYPTION_SMTPS and 587 to ENCRYPTION_STARTTLS, confirming the port is reachable, and reading SMTPDebug output.

"SMTP Error: Could not connect to SMTP host" is emitted by PHPMailer (and libraries built on it) when it cannot open a network connection to the server named in its Host property. Per PHPMailer's own troubleshooting docs, it "means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why." It is a transport-layer failure, not an SMTP reply code from the server — the failure happens before any SMTP 220 greeting, AUTH, or message data is exchanged. That distinction matters: an authentication problem (wrong password, missing Gmail App Password) surfaces later as a 535 error, because that requires a connection to already exist.

What causes "Could not connect to SMTP host"?

The connection or TLS handshake never completes. The common roots:

  • Encryption mode does not match the port. This is the single most frequent cause. Using SMTPS on 587, or STARTTLS on 465, will hang or fail the handshake.
  • Wrong Host or Port, or a typo in the hostname that fails DNS resolution.
  • Outbound port blocked by a firewall, your host, or your ISP. Some providers (e.g. GoDaddy) block outbound 25/465/587 to all servers except their own; some (e.g. Digital Ocean) block SMTP over IPv6 while allowing IPv4.
  • OpenSSL extension not loaded — required for any encrypted connection; without it the STARTTLS/SMTPS phase fails.
  • SELinux preventing the web server from making outbound network connections.

How do I fix "Could not connect to SMTP host"?

1. Match the encryption mode to the port. Per RFC 8314 §3.3, both implicit TLS (465) and STARTTLS (587) are valid — but each pairs with exactly one PHPMailer mode. Do not mix them:

// Implicit TLS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // port 465
$mail->Port = 465;
// OR STARTTLS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // port 587
$mail->Port = 587;

2. Confirm the port is actually reachable from the sending server:

# Implicit TLS (465)
echo QUIT | openssl s_client -connect smtp.gmail.com:465
# STARTTLS (587)
echo QUIT | openssl s_client -starttls smtp -connect smtp.gmail.com:587

A 220 greeting and Verify return code: 0 (ok) means the path is open. No response means a firewall, host, or ISP is blocking the port — that must be fixed at the network level; PHPMailer cannot work around it. telnet smtp.host.com 587 is a quick reachability check.

3. Turn on debug output and read it — the server usually states the real problem:

$mail->SMTPDebug = SMTP::DEBUG_SERVER;

4. Verify OpenSSL is loaded:

echo extension_loaded('openssl') ? 'SSL loaded' : 'SSL NOT loaded';

5. DNS / IPv6 / SELinux checks. Confirm the host resolves (dig +short smtp.gmail.com). If your provider blocks SMTP over IPv6, force IPv4 — but note PHPMailer warns this breaks certificate name checks, so fixing the network is preferred. On SELinux hosts: sudo setsebool -P httpd_can_network_connect 1.

Note on Gmail / Google Workspace: Google removed "Less Secure Apps" for personal Gmail accounts in 2022; Google Workspace accounts kept limited legacy access until the final cutover on March 14, 2025 — do not look for that toggle on either account type today. Use an App Password (requires 2-Step Verification) or OAuth2. But a bad credential produces a 535 auth error after connecting, not "Could not connect to SMTP host." If you see this connect error against Gmail or Workspace, it is the port/encryption/firewall, not the password.

With Courier

If you route SMTP through Courier, you configure the host, port, and credentials once in the provider settings and Courier owns the connection and TLS handshake — removing the PHPMailer-level port/encryption mismatch class of failures from your application code.

FAQ

Common questions

No. It is a PHPMailer message indicating it could not open a TCP/TLS connection to the host in its Host property. The failure happens before any SMTP reply code (like 220 or 535) is exchanged, so it reflects a network, port, encryption, or DNS problem rather than a server-issued status code.

One API, every provider

Stop debugging raw provider errors

Courier connects to your email, SMS, and push providers, handles retries and failover, and surfaces delivery errors in plain language.

Reply-code definitions per RFC 8314 §3.3. Last reviewed Jul 1, 2026. Courier is not affiliated with third-party providers; error behavior may vary by implementation.