PHP fsockopen giving error with SSL

I am getting the error while connecting to fsockopen.

echo $result = fsockopen('ssl://smtp.gmail.com', 465, $error_no, $error_message, 5);
//$result = fsockopen('tls://smtp.gmail.com', 587, $error_no, $error_message, 5);
if ($result === false) {
  echo "error no: $error_no error message: $error_message";
  echo print_r($result, true);
} else {
  echo 'success';
}

error no: 0 error message:

I checked the manual here which says

If the value returned in errno is 0 and the function returned false, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket.

But I do not know how to resolve this. I have an AWS Ubuntu 18.04 instance and the website is https.

The problem is I am trying to set up gmail smtp. This settings works fine on local as well as other AWS instances where SSL isn't installed

UPDATE 1:

print_r(stream_get_transports());

The above statement gives

Array (
    [0] => tcp
    [1] => udp
    [2] => unix
    [3] => udg
    [4] => ssl
    [5] => tls
    [6] => tlsv1.0
    [7] => tlsv1.1
    [8] => tlsv1.2 
)

Solution 1:

You don't receive an error, as you can see: $error_no is zero.

Your command

echo $result = fsockopen(...)

makes no sense, or certainly not what you want. You echo the contents of the result variable, followed by an equals sign, followed by - if fsockopen is really executed - the file variable returned by fsockopen. See PHP manual fsockopen for the correct usage.