Posts Tagged ‘sendemail’

OpenSSL Error in PHP

December 7, 2009 1 comment

I decided to play with Swiftmailer, the successor to phpMailer, but wanted to try something different — I wanted to use TLS. I was trying to setup a piece of test code for an internal project and just couldn’t get it to work in the office so I decided to try it from home as I had fewer variables to trip me up, i.e. no firewalls or hardened mail servers to play with.

I installed XAMPP v1.7.2 on my Windows PC which gave me PHP v5.3.0 with OpenSSL v0.9.8k. It also gave me TLS support as ‘Registered Stream Socket Transports’ on the PHPINFO() page said I had ‘tcp, udp, ssl, sslv3, sslv2, tls’ available. I then used a simple piece of code:

require_once 'lib/swift_required.php';

// Setup the mail transport
$transport = Swift_SmtpTransport::newInstance ('', 587, 'tls');
$transport->setUsername ('{username}');
$transport->setPassword ('{password}');

// Setup the mailer
$mailer = Swift_Mailer::newInstance ($transport);

// Build the message
$message = Swift_Message::newInstance();
$message->setSubject ('Testing');
$message->setFrom (array('{username}' => 'Test Sender'));
$message->setTo (array('{email address}' => 'Test Recipient'));
$message->setBody ('Here is the test message.');

//Send the message
$result = $mailer->send ($message);

You’ll have to change the {username}, {password} and {email address} though to make it work for you. And then I ran it.

BANG! — It failed with a cryptic error message “SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number”.

I Googled for an hour and found post after post but I got it in the end. It appears to be a version difference between the client and server, the PHP code is running SSL v3 whereas Google Mail is looking for SSL v2. If I changed the port from 587 to 465 it all went away though.

BTW The other tool I used to play with TLS was SendEmail, a command-line utility for sending emails. The following command worked perfectly for me on port 587 (annoyingly). It all needs to be on one line.

sendemail -f {username} -t {email address} -u Testing
-m "This is a test message." -s 
-xu {username} -xp {password} -o tls=yes