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 showFix it fast
Most common cause, 1 of 4The 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.
$fp = fsockopen('ssl://smtp.example.com', 465, $errno, $errstr, 10);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.
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.
<?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:
- 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 - 2Nothing listens on that address and port
$errstr is Connection refused, and the failure comes back at once rather than after the timeout - 3The TCP connection opens but the TLS handshake fails
Two 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 - 4This PHP has no ssl transport at all
The 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.
<?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.
$fp = fsockopen('ssl://smtp.example.com', 465, $errno, $errstr, 10);Verify
The first-step script prints bool(true), int(0) and an empty string.
php check-socket.phpConfirmed by docs.cloud.google.com
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.
# 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).
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.
php check-socket.phpConfirmed by man7.org
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=...).
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.
; 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.
php -r 'var_dump(ini_get("openssl.cafile"));'Confirmed by raw.githubusercontent.com
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.
php -m | grep -i openssl
php --iniFix
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.
<?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.
php -m | grep -i opensslConfirmed by raw.githubusercontent.com
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.