PHPMailer cannot connect to the SMTP host (Gmail and others)
SMTP Error: Could not connect to SMTP host.Short answer
This error means PHPMailer could not open a network connection to the host in Host. It does not say why. Turn on SMTPDebug, then check the pair of port and encryption (465 with SMTPS or 587 with STARTTLS for Gmail), DNS, firewall, the openssl extension and SELinux. Test the port outside PHP first.
Fix it fast
1Find out which cause you have
Turn on server-level debug output and look at the first lines. They show the host and port PHPMailer tried and how the attempt ended.
use PHPMailer\PHPMailer\SMTP; $mail->SMTPDebug = SMTP::DEBUG_SERVER; // use DEBUG_CONNECTION for timeouts2Match what you see, then jump to its fix
- 1
The debug output ends right after the connect attempt, and the config mixes 465 with STARTTLS or 587 with SMTPS.Port and encryption setting do not belong togetherFix - 2
Network is unreachable, or no answer from dig.The hostname does not resolve, or the server cannot be reachedFix - 3
The port test below prints nothing, or the banner names your ISP instead of the mail server.A firewall, the host or the ISP blocks the SMTP portFix - 4
Plain connections work, encrypted ones fail.The openssl extension is not loadedFix - 5
The script works from the command line but not under the web server, on a Red Hat family system.SELinux stops PHP from opening network connectionsFix - 6
Connecting by IPv4 works, while the hostname fails.The host blocks IPv6 and the name resolves to an IPv6 address firstFix
- 1
What does the error look like?
SMTP Error: Could not connect to SMTP host.SMTP connect() failed.Network is unreachable (101)
Cause 1 of 6
Port and encryption setting do not belong together
1Check
PHPMailer documents implicit TLS (ENCRYPTION_SMTPS) with port 465 and STARTTLS (ENCRYPTION_STARTTLS) with port 587. Print what the code sets.
grep -n "SMTPSecure\|Port\|Host" send_mail.php2Fix
Use one of the two valid pairs for the Gmail host smtp.gmail.com.
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// or: PHPMailer::ENCRYPTION_SMTPS with Port = 4653Verify
Send one test message with debug output on.
php send_mail.phpConfirmed by raw.githubusercontent.com
Cause 2 of 6
The hostname does not resolve, or the server cannot be reached
1Check
PHPMailer's own troubleshooting guide starts with DNS and reachability from the machine that runs PHP, not from your laptop.
dig +short smtp.gmail.com
ping -c 3 smtp.gmail.com2Fix
Fix the resolver or the Host value. If the name does not resolve on the server, fix the server's DNS first.
cat /etc/resolv.conf3Verify
dig returns addresses, then the test message sends.
dig +short smtp.gmail.comConfirmed by github.com
Cause 3 of 6
A firewall, the host or the ISP blocks the SMTP port
1Check
Connect to the port from the same server. A healthy mail server answers with a banner that begins with 220.
nc -v smtp.gmail.com 5872Fix
Open the outbound port in the firewall or hosting panel, or use the provider's documented relay. If the host blocks all outbound SMTP, you cannot fix it in PHP.
# allow outbound TCP 587 and 465 in the firewall or hosting panel3Verify
nc shows a 220 banner from the real server.
nc -v smtp.gmail.com 465Confirmed by github.com
Cause 4 of 6
The openssl extension is not loaded
1Check
Encrypted connections need PHP's openssl extension in the PHP that runs the script.
php -m | grep -i openssl2Fix
Enable the extension in the php.ini that the web server loads and restart PHP.
; php.ini
extension=openssl3Verify
The module list shows openssl.
php -m | grep -i opensslConfirmed by github.com
Cause 5 of 6
SELinux stops PHP from opening network connections
1Check
On RedHat-family systems SELinux can block the web server's network access.
getsebool httpd_can_network_connect httpd_can_sendmail2Fix
Enable the booleans the guide names. With PHP-FPM the setting may need to apply to the fpm daemon.
sudo setsebool -P httpd_can_network_connect on3Verify
Send the test message again through the web request.
getsebool httpd_can_network_connectConfirmed by github.com
Cause 6 of 6
The host blocks IPv6 and the name resolves to an IPv6 address first
1Check
Some providers block outbound SMTP over IPv6. See which address families the name returns.
dig +short AAAA smtp.gmail.com
nc -4 -v smtp.gmail.com 5872Fix
Fix the network for IPv6. Setting Host to an IPv4 address works as a stopgap but can break certificate name checks, so it is the worse option.
$mail->Host = gethostbyname('smtp.gmail.com'); // stopgap only3Verify
The test message sends.
php send_mail.phpConfirmed by github.com
How do I stop it coming back?
- Keep SMTPDebug off in production but log failures, so a blocked port is seen at once.
- Test the port with nc from the real server after every host or firewall change.
- For Gmail, authenticate with the full address and an app password, with the 465 or 587 pair documented by Google.
- Pin the pair (host, port, encryption) in one config file and reuse it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
PHPMailer cannot connect to the SMTP host from a GoDaddy server
SMTP connect() failed.
PHPMailer reports Could not connect to SMTP host when the TCP connection to the mail server fails.
2 causes, each with a fixCodeIgniter email does not send and shows no error message
$email->printDebugger() prints an empty result
CodeIgniter signals a failed email only by send() returning false, with no exception.
4 causes, each with a fixLaravel tries to send mail to mailpit:1025 instead of your real SMTP server
Symfony\Component\Mailer\Exception\TransportException
Laravel is still using the development mail host from the starter .env, mailpit on port 1025, which only exists inside Laravel Sail.
3 causes, each with a fixLaravel mail fails because an address does not comply with RFC 2822
Email "Jane Doe <jane@example.com>" does not comply with addr-spec of RFC 2822.
Symfony Mailer checks every address when the Address object is created.
3 causes, each with a fixLaravel says the email was sent, but nothing arrives
Mail::to($user)->send() returns normally
Mail::send returns without an error when the message was accepted by the configured mailer, not when it was delivered.
3 causes, each with a fixLaravel mail fails with 'Unable to connect with STARTTLS' and SSL operation failed with code 1
stream_socket_enable_crypto(): Peer certificate verification failed
The mail server offered STARTTLS, but PHP could not verify the certificate it presented.
3 causes, each with a fix