"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)
Because it fires before authentication, the cause is environmental or configuration-level, not credentials:
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.openssl isn't loaded in PHP, any TLS/SSL connection fails silently at the socket layer.Host value (including stray whitespace) or a local firewall/antivirus intercepting the socket.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 1sudo 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
PHPMailer Troubleshooting wiki (official)
RFCRFC 8314 — Cleartext Considered Obsolete: TLS for Email (ports 465/587)
RFCRFC 5321 — Simple Mail Transfer Protocol
RFCRFC 3463 — Enhanced Mail System Status Codes
DOCSSiteGround — Fix SMTP connect() failed in PHPMailer
HELPGoogle — Sign in with App Passwords
FAQ
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.
One API, every provider
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.
© 2026 Courier. All rights reserved.