Home > Uncategorized > OpenSSL Error in PHP

OpenSSL Error in PHP

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 ('smtp.gmail.com', 587, 'tls');
$transport->setUsername ('{username}@gmail.com');
$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}@gmail.com' => '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}@gmail.com -t {email address} -u Testing
-m "This is a test message." -s smtp.gmail.com:587 
-xu {username}@gmail.com -xp {password} -o tls=yes
  1. July 7, 2011 at 6:39 am

    Thank you. You saved me many hours of frustration.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: