title: Using Gmails Outgoing SMTP from PHP using TLS
body: <p>I'm sending email from PHP through the Gmail SMTP server. I've been using the CakePHP email component with SMTP settings set. I originally had it all working fine using SSL over port 465 but have found my web host doesn't allow outgoing traffic over 465. They did however tell me that outgoing connections over port 587 is allowed. </p>
  
  <p>After reading <a href="http://mail.google.com/support/bin/answer.py?answer=13287" rel="nofollow">http://mail.google.com/support/bin/answer.py?answer=13287</a> I thought it would be as easy as changing the port number and protocol but I can't get it to work.</p>
  
  <p>The offending line of code seems to be a fsockopen call:</p>
  
  <pre><code>fsockopen("ssl://smtp.gmail.com", 465, $errNum, $errStr, 30); // WORKS
  fsockopen("tls://smtp.gmail.com", 587, $errNum, $errStr, 30); // FAILS
  </code></pre>
  
  <p>The errors given are:</p>
  
  <pre><code>Warning: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages:
  error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number in Command line code on line 1
  
  Warning: fsockopen(): Failed to enable crypto in Command line code on line 1
  
  Warning: fsockopen(): unable to connect to tls://smtp.gmail.com:587 (Unknown error) in Command line code on line 1
  </code></pre>
  
  <p>This is with PHP 5.3, phpinfo shows OpenSSL is enabled. Any help would be greatly appreciated.</p>