-1

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 
)
AnFi
  • 6,103
  • 1
  • 14
  • 27
Dushyant Joshi
  • 101
  • 1
  • 10
  • Do you have a way to check if the connection is working without PHP ? Like with "openssl" command ? Are you sure that the PHP have the ssl support enabled ? print_r(stream_get_transports()); – Dom Dec 21 '20 at 09:47
  • @Dom, Yes. Please check the update. – Dushyant Joshi Dec 21 '20 at 09:52
  • try enabling error_reporting(E_ALL) or check the error_log - it could be some DNS issue. Additionally as suggested try "openssl s_client -connect smtp.gmail.com:465" in a console to see if a connection can be established from your server – Dobromir Velev Dec 21 '20 at 12:30
  • This appears to be off-topic here. Programming questions should be asked on [so]. – Michael Hampton Dec 21 '20 at 15:00
  • @MichaelHampton, I edited the question also – Dushyant Joshi Dec 21 '20 at 16:07

1 Answers1

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.

Gerard H. Pille
  • 2,569
  • 1
  • 13
  • 11