PHP Tutorial - How to check if the connection to Gmail SMTP was established

I want to know if the connection to Gmail's SMTP was successful or not.
I'm using:
$fp = fsockopen("smtp.gmail.com", 587, $errno, $errstr, 30);
if ($fp) {
    echo "Its Ok";
}
else
{
    echo "No";
}
But it didn't work.

Answer:

Your code works for me.
Did you check your registered stream socket transports in phpinfo() as below?
Registered Stream Socket Transports tcp, udp, ssl, sslv3, sslv2, tls, tlsv1.0, tlsv1.1, tlsv1.2
I think the problem may result from php version(My is 5.6.8). Please refers to the php explanation:
The ssl:// and tls:// transports (available only when openssl support is compiled into PHP) are extensions of the tcp:// transport which include SSL encryption. Since PHP 4.3.0 OpenSSL support must be statically compiled into PHP, since PHP 5.0.0 it may be compiled as a module or statically.