SMTP error: failed to connect to server

"SMTP error: failed to connect to server" is a PHPMailer connection failure, not an RFC code. Fix blocked ports, port/TLS mismatch, OpenSSL, DNS and firewall.

Updated Jul 1, 2026

The short answer

"SMTP error: failed to connect to server" is a PHPMailer transport-layer failure: it could not open a TCP/TLS socket to your mail server before any SMTP conversation began — so it is not an RFC reply code. It almost always means a blocked port (25/465/587), a port-to-encryption mismatch, a missing OpenSSL extension, or a DNS/firewall problem. Fix it by testing the socket with openssl and matching the port to its encryption mode.

This message comes from PHPMailer (the SMTP->connect() routine), and it is worth being precise about what it is — and isn't. It is not a 3-digit SMTP reply code defined in RFC 5321 or an enhanced status code from RFC 3463. The server never got far enough to reply. It is a transport-layer error: PHPMailer could not open (or secure) a TCP socket to the host/port you configured, so the SMTP conversation never started. PHPMailer's own documentation is blunt about this: it is "almost always down to local DNS failure, firewall blocking ... local anti-virus software, or another issue on your local network." (PHPMailer Troubleshooting wiki)

What causes "SMTP error: failed to connect to server"?

Because it fires before authentication, the cause is environmental or configuration-level, not credentials:

  • A blocked outbound port. Many hosts and ISPs block outbound 25, 465, and 587. GoDaddy, for example, blocks outbound SMTP to all servers except its own. The connection times out and you see this error.
  • Port / encryption-mode mismatch. This is the most common self-inflicted cause. Port 465 uses implicit TLS (SMTPS), and port 587 uses STARTTLS. PHPMailer warns: "SMTPS on port 587 or SMTP_STARTTLS on port 465 will not work." Per RFC 8314 §3.3, both 465 and 587 are valid — 465 is not deprecated — but each is tied to its mode.
  • Missing OpenSSL extension. If openssl isn't loaded in PHP, any TLS/SSL connection fails silently at the socket layer.
  • DNS failure. PHPMailer notes "DNS failures are often seen as connection timeouts." If the host resolves to the wrong IP or nothing, the connect fails.
  • SELinux (on RHEL/CentOS) denying the web server outbound network/mail access.
  • A wrong Host value (including stray whitespace) or a local firewall/antivirus intercepting the socket.

How do I fix "SMTP error: failed to connect to server"?

Work outward from the network, since a code change can't fix a blocked port.

1. Test the socket directly (bypasses PHP entirely). For STARTTLS on 587:

openssl s_client -starttls smtp -crlf -connect smtp.yourprovider.com:587

For implicit TLS on 465:

openssl s_client -crlf -connect smtp.yourprovider.com:465

If this hangs or refuses, the problem is the port/firewall/DNS, not your code — contact your host or ISP to open the port, or switch to one they allow.

2. Match the port to its encryption mode in PHPMailer:

// Port 587 — STARTTLS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Port 465 — implicit TLS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;

3. Confirm OpenSSL is loaded: run php -m | grep openssl. If absent, enable the openssl extension in php.ini.

4. Enable verbose debugging to see exactly where it dies:

$mail->SMTPDebug = SMTP::DEBUG_CONNECTION;

5. On SELinux systems, allow the web server to send mail and make outbound connections:

sudo setsebool -P httpd_can_sendmail 1
sudo setsebool -P httpd_can_network_connect 1

6. Verify DNS and host: dig smtp.yourprovider.com should return the expected IP. Remove any leading/trailing whitespace from Host.

Gmail / Google Workspace note: These are two separate cutoffs, not one. Google removed "Less Secure Apps" access for personal Gmail accounts in May 2022 — do not look for that toggle. Separately, Google Workspace deprecated basic-auth SMTP (username/password) access entirely, with enforcement completing by May 2025 after phased delays — if you're on Workspace, basic-auth SMTP no longer works regardless of the Less Secure Apps setting. For both, authenticate with an App Password (requires 2-Step Verification) or OAuth2. A wrong password produces a different error after connecting; this one fires before auth, so it points at the port/TLS combination first.

If you'd rather not manage SMTP transport at all, sending through an API-based provider (such as Courier or any SES/SendGrid/Mailgun integration) sidesteps socket, port-blocking, and TLS-negotiation failures entirely.

References

Authoritative sources

DOCS

PHPMailer Troubleshooting wiki (official)

RFC

RFC 8314 — Cleartext Considered Obsolete: TLS for Email (ports 465/587)

RFC

RFC 5321 — Simple Mail Transfer Protocol

RFC

RFC 3463 — Enhanced Mail System Status Codes

DOCS

SiteGround — Fix SMTP connect() failed in PHPMailer

HELP

Google — Sign in with App Passwords

FAQ

Common questions

No. It is a PHPMailer transport-layer error raised before any SMTP conversation, so it is not a 3-digit reply code from RFC 5321 or an enhanced status code from RFC 3463. It means the underlying TCP/TLS socket to the mail host could not be opened.

Keep going

Related SMTP errors

SMTP error: could not connect to SMTP host

SMTP error: connection to server failed

SMTP timeout error

10060

SMTP connect error 10060

An unknown error occurred connecting to the SMTP server

View all errors →

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.

Start building freeRead the docs

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.

Multichannel Notifications Platform for SaaS

Products

Platform

Integrations

Customers

Blog

API Status

Subprocessors

© 2026 Courier. All rights reserved.