Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

fsockopen() cannot open a connection to the remote host and port

Your error

Warning: fsockopen(): Unable to connect to ssl://smtp.example.com:465 (Connection timed out)3 more lines it can show

Fix it fast

Most common cause, 1 of 4

The outbound port is blocked, so the attempt times out

Use a port the provider leaves open (465 with ssl://, or the provider's own relay), or ask the host to allow the destination.

Fix
$fp = fsockopen('ssl://smtp.example.com', 465, $errno, $errstr, 10);
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

fsockopen() warns Unable to connect to host:port (reason) when the socket never opens. Connection timed out means a firewall drops the outbound port, Connection refused means nothing listens there, and Unknown error after an SSL warning means the TLS handshake failed. Print $errno and $errstr, fix the port, service or CA bundle, and retry. Applies to PHP 8.2-8.6.

  • Applies to PHP 8.2 to 8.6, Linux (errno names from connect(2))
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Save this as check-socket.php and run it on the failing server, using the host and port your code dials. The text in $errstr is the reason in brackets, and it picks the cause below.

Run this first
<?php
$fp = @fsockopen('ssl://smtp.example.com', 465, $errno, $errstr, 10);
var_dump($fp !== false, $errno, $errstr);

Then find the line you see in this list and open its fix:

  1. 1The outbound port is blocked, so the attempt times out$errstr is Connection timed out, and the call hangs for the whole timeout before failing
  2. 2Nothing listens on that address and port$errstr is Connection refused, and the failure comes back at once rather than after the timeout
  3. 3The TCP connection opens but the TLS handshake failsTwo warnings come first, SSL operation failed with code 1. OpenSSL Error messages: ... certificate verify failed and Failed to enable crypto, then the reason reads Unknown error
  4. 4This PHP has no ssl transport at allThe reason in brackets is Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?

What does the error look like?

  • Warning: fsockopen(): Unable to connect to ssl://smtp.example.com:465 (Connection timed out)
  • Warning: fsockopen(): Unable to connect to 10.0.0.5:6379 (Connection refused)
  • Warning: fsockopen(): Unable to connect to ssl://api.example.com:443 (Unknown error)
  • Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?

Cause 1 of 4

The outbound port is blocked, so the attempt times out

You see$errstr is Connection timed out, and the call hangs for the whole timeout before failing

Check

Unable to connect ... (Connection timed out) means no reply reached PHP before the timeout ran out. connect(2) reports ETIMEDOUT for this, and a firewall that silently drops the packets looks exactly the same. Hosting and cloud providers often drop outbound mail ports: Google Compute Engine, for example, blocks connections to destination port 25 and leaves 587 and 465 open.

Check
<?php
foreach ([25, 465, 587] as $port) {
    $fp = @fsockopen('smtp.example.com', $port, $errno, $errstr, 5);
    echo $port, ': ', $fp ? 'open' : $errstr, PHP_EOL;
}

Fix

Use a port the provider leaves open (465 with ssl://, or the provider's own relay), or ask the host to allow the destination. Pass an explicit timeout so a blocked port costs seconds instead of the full default_socket_timeout.

Fix
$fp = fsockopen('ssl://smtp.example.com', 465, $errno, $errstr, 10);

Verify

The first-step script prints bool(true), int(0) and an empty string.

Verify
php check-socket.php

Confirmed by docs.cloud.google.com

Did this fix it?

Cause 2 of 4

Nothing listens on that address and port

You see$errstr is Connection refused, and the failure comes back at once rather than after the timeout

Check

Unable to connect ... (Connection refused) is the remote machine answering that no service is listening there, which connect(2) reports as ECONNREFUSED. The service may be stopped, may listen on another port, or may be bound only to 127.0.0.1 while PHP connects from another host or container.

Check
# on the machine that should accept the connection
sudo ss -ltnp | grep ':6379'

Fix

Start the service, correct the port in the PHP config, or bind the service to an address the PHP host can reach (and allow only that host in its firewall).

Fix
sudo systemctl start my-service
sudo ss -ltnp | grep ':6379'

Verify

ss lists a listener on the expected address, and the first-step script returns a stream.

Verify
php check-socket.php

Confirmed by man7.org

Did this fix it?

Cause 3 of 4

The TCP connection opens but the TLS handshake fails

You seeTwo warnings come first, SSL operation failed with code 1. OpenSSL Error messages: ... certificate verify failed and Failed to enable crypto, then the reason reads Unknown error

Check

With an ssl:// or tls:// prefix, PHP connects and then starts TLS. If the handshake fails, the transport leaves no error text, so fsockopen() fills the brackets with Unknown error. The OpenSSL lines above it name the real fault: usually PHP cannot find a CA bundle to verify the server, or the certificate name does not match the host you dialled (Peer certificate CN=... did not match expected CN=...).

Check
php -r 'print_r(openssl_get_cert_locations());'

Fix

Point PHP at the operating system's CA bundle with openssl.cafile in php.ini (it cannot be changed with ini_set()), keep that bundle updated, and connect using the host name the certificate was issued for. Leave verify_peer on, as it is by default.

Fix
; php.ini read by the web server (example path)
openssl.cafile = "/path/to/ca-bundle.pem"

Verify

The OpenSSL warnings are gone and the first-step script returns a stream.

Verify
php -r 'var_dump(ini_get("openssl.cafile"));'

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

This PHP has no ssl transport at all

You seeThe reason in brackets is Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?

Check

PHP looks up the transport named before :// and, when no OpenSSL support is built in or loaded, returns this text as the reason. The web server's PHP and the CLI can differ, so check the one that runs the failing code.

Check
php -m | grep -i openssl
php --ini

Fix

Use a PHP build with OpenSSL support (compiled --with-openssl, or the OpenSSL extension your distribution or Windows build ships), enable it in the php.ini that php --ini reports for the server, and restart PHP.

Fix
<?php
// served by the same web server as the app
var_dump(extension_loaded('openssl'));

Verify

The check prints bool(true), and the error moves on to a real connection result.

Verify
php -m | grep -i openssl

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Always pass $errno, $errstr and a timeout of a few seconds to fsockopen(), and log both values on failure.
  • Confirm a new outbound port from the production host before release, because many providers block mail ports by default.
  • Keep the server's CA certificates package updated and set openssl.cafile once in the main php.ini.
  • Use host names that match the remote certificate instead of IP addresses for TLS connections.

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 on the same stack: Spotless Laundry, EZ Estimater.

Next step

Want this handled for good?

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