Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

PHPMailer cannot connect to the SMTP host (Gmail and others)

ErrorSMTP 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.

  • Applies to PHPMailer 6, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 timeouts
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2Network is unreachable, or no answer from dig.The hostname does not resolve, or the server cannot be reachedFix
    3. 3The 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. 4Plain connections work, encrypted ones fail.The openssl extension is not loadedFix
    5. 5The 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. 6Connecting by IPv4 works, while the hostname fails.The host blocks IPv6 and the name resolves to an IPv6 address firstFix

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.php

2Fix

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 = 465

3Verify

Send one test message with debug output on.

php send_mail.php

Confirmed by raw.githubusercontent.com

Did this fix it?

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.com

2Fix

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.conf

3Verify

dig returns addresses, then the test message sends.

dig +short smtp.gmail.com

Confirmed by github.com

Did this fix it?

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 587

2Fix

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 panel

3Verify

nc shows a 220 banner from the real server.

nc -v smtp.gmail.com 465

Confirmed by github.com

Did this fix it?

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 openssl

2Fix

Enable the extension in the php.ini that the web server loads and restart PHP.

; php.ini
extension=openssl

3Verify

The module list shows openssl.

php -m | grep -i openssl

Confirmed by github.com

Did this fix it?

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_sendmail

2Fix

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 on

3Verify

Send the test message again through the web request.

getsebool httpd_can_network_connect

Confirmed by github.com

Did this fix it?

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 587

2Fix

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 only

3Verify

The test message sends.

php send_mail.php

Confirmed by github.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.