2
votes

I'm trying to use php function fsockopen on a smtp server using ssl on port 465 on my local server with apache. It works well with php command line but I get a SSL error when the script is running in my browser.

php.ini

extension=php_openssl.dll line is not commented

phpinfo through Apache

Loaded Configuration File : D:\localhost\php-5.4.11\php.ini
openssl
OpenSSL support enabled
OpenSSL Library Version OpenSSL 0.9.8x 10 May 2012
OpenSSL Header Version  OpenSSL 0.9.8x 10 May 2012

phpinfo with command line

Loaded Configuration File => D:\localhost\php-5.4.11\php.ini
openssl
OpenSSL support => enabled
OpenSSL Library Version => OpenSSL 0.9.8x 10 May 2012
OpenSSL Header Version => OpenSSL 0.9.8x 10 May 2012

My script

$smtp = fsockopen('ssl://in.mailjet.com', 465, $errno, $errstr, 30);
$response = fgets($smtp, 4096);
if(empty($smtp)){
   echo $response;
   return false;
}
echo $response . '<br/>';
fclose($smtp);

Output under apache

Warning: fsockopen(): SSL: crypto enabling timeout in XXX on line 2
Warning: fsockopen(): Failed to enable crypto in XXX on line 2
Warning: fsockopen(): unable to connect to ssl://in.mailjet.com:465 (Unknown error) in XXX on line 2

Output with command line

220 srv12.mailjet.com ESMTP Mailjet
<br/>

Platform: I'm using PHP 5.4.11 / Apache 2.2.22 (Win32) on Windows 8 64

-- Edit -- I've tried using TLS as suggested:

$smtp = fsockopen('tls://in.mailjet.com', 567, $errno, $errstr, 30);

And I get

E_WARNING: fsockopen(): in XXX on line XXX

Error message is blank and $errno = (int) 0 and $errstr = (string) ''

2
You may want to look at using a library like SwiftMailer instead of rolling your own SMTP client. However, try using the tls:// wrapper instead of ssl://. If that doesn't work, try connecting using the tcp:// wrapper and manually enabling crypto on the stream with stream_socket_enable_crypto(). The error message says there is a timeout occurring - how long after you start the script do you get the error message (i.e. how long is the timeout?) - DaveRandom
I've tried with this : code fsockopen('tls://in.mailjet.com', 567, $errno, $errstr, 30); code and I get E_WARNING: fsockopen(): in XXX on line XXX - syl.fabre
You get a blank error message? - DaveRandom
@DaveRandom the timeout is 30 seconds as specified in the 5th argument of fsockopen. I reduced it down to 5 seconds as the script is fast in CLI version, and I get the error about 5 seconds after refreshing the page in my browser. I try your tip with tcp:// wrapper and come back with feedbacks - syl.fabre
@DaveRandom Yes I get a blank error message which is confusing. Arguments 3rd and 4th return : $errno = (int) 0 and $errstr = (string) '' - syl.fabre

2 Answers

0
votes

I updated my Apache version from 2.2 to 2.4 and it works now.

I've followed this tutoriel : http://lifeofageekadmin.com/how-install-apache-2-4-php-5-4-and-mysql-5-5-21-on-windows-7/ to get the new Apache version.

0
votes

I was having a similar problem with a contact form which uses captcha, getting the error

fsockopen(): unable to connect to ssl://mail.google.com:443

This is on a FreeBSD vm, with Joomla installed.

Searching, I found http://php.net/manual/en/function.fsockopen.php which mentioned certificate validation.

Installed the FreeBSD port ca_root_nss which includes certificate validation for Root certificates from certificate authorities included in the Mozilla NSS library.