title: CodeIgniter and Gmail SMTP Timeout with SSL scoket
body: <p>I use Gmail SMTP to send email from the web application that I wrote using CodeIgniter with PHP 5.3 without a problems until recently. This is the snippet of my code:</p>
  
  <pre><code>  $config['protocol']   = 'smtp';
    $config['smtp_host']  = $this-&gt;smtp_host;
    $config['smtp_port']  = $this-&gt;smtp_port;
    $config['smtp_user']  = $this-&gt;smtp_username;
    $config['smtp_pass']  = $this-&gt;smtp_password;
    $config['mailtype']   = $this-&gt;email_type;
    $config['newline'] = "\r\n";
    $this-&gt;load-&gt;library('email', $config);
  
    $this-&gt;email-&gt;from($email_sender, $email_sender_name);
    $this-&gt;email-&gt;to($email_to);
    $this-&gt;email-&gt;subject($message_subject);
    $this-&gt;email-&gt;message($message_content);
    $result = $this-&gt;email-&gt;send();
  </code></pre>
  
  <p>When I use <code>"ssl://smtp.gmail.com"</code> as host and <code>465</code> as port, I got this error in log:</p>
  
  <pre><code>DEBUG - 2010-08-06 17:19:24 --&gt; Email Class Initialized
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fsockopen(): unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out) /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1652
  DEBUG - 2010-08-06 17:19:45 --&gt; Language file loaded: language/english/email_lang.php
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:19:45 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  </code></pre>
  
  <p>I don't know if this related, but before I upgrade my PHP into <code>PHP 5.3</code>, the same code worked just fine. If this is the firewall or internet connection problems, I can still send email using gmail smtp in Thunderbird.</p>
  
  <p>After I looking in various forums, I got a tips to do this test:</p>
  
  <pre><code>$ openssl s_client -starttls smtp -crlf -connect smtp.gmail.com:587
  CONNECTED(00000003)
  depth=1 /C=US/O=Google Inc/CN=Google Internet Authority
  verify error:num=20:unable to get local issuer certificate
  verify return:0
  ---
  Certificate chain
   0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com
     i:/C=US/O=Google Inc/CN=Google Internet Authority
   1 s:/C=US/O=Google Inc/CN=Google Internet Authority
     i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
  ---
  Server certificate
  -----BEGIN CERTIFICATE-----
  ...cut...
  -----END CERTIFICATE-----
  subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com
  issuer=/C=US/O=Google Inc/CN=Google Internet Authority
  ---
  No client certificate CA names sent
  ---
  SSL handshake has read 1901 bytes and written 335 bytes
  ---
  New, TLSv1/SSLv3, Cipher is RC4-MD5
  Server public key is 1024 bit
  Compression: NONE
  Expansion: NONE
  SSL-Session:
      Protocol  : TLSv1
      Cipher    : RC4-MD5
      Session-ID: 2CA08CABBE21653238292DEDF30D119428970FAA284263C92480DA5283AFE013
      Session-ID-ctx: 
      Master-Key: B3F6D4423DC14E24E894D7AD7107B4A640839F2BF90233714EC5BF0D139611E65655902B50AEA3BD67373A21338526B0
      Key-Arg   : None
      Start Time: 1281086948
      Timeout   : 300 (sec)
      Verify return code: 20 (unable to get local issuer certificate)
  ---
  250 ENHANCEDSTATUSCODES
  </code></pre>
  
  <p>while using same tool to test <code>ssl</code> connection give me timeout:</p>
  
  <pre><code>$ openssl s_client -ssl2 -crlf -connect smtp.gmail.com:465
  connect: Connection timed out
  connect:errno=110
  </code></pre>
  
  <p>I try to use <code>"tls://smtp.gmail.com"</code> as host and <code>587</code> as port in my code above, but now I get this error message:</p>
  
  <pre><code>DEBUG - 2010-08-06 17:26:10 --&gt; Email Class Initialized
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fsockopen(): SSL operation failed with code 1. OpenSSL Error messages:
  error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1652
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fsockopen(): Failed to enable crypto /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1652
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fsockopen(): unable to connect to tls://smtp.googlemail.com:587 (Unknown error) /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1652
  DEBUG - 2010-08-06 17:26:10 --&gt; Language file loaded: language/english/email_lang.php
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fwrite() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1795
  ERROR - 2010-08-06 17:26:10 --&gt; Severity: Warning  --&gt; fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
  </code></pre>
  
  <ol>
  <li>Can you help me find out what the
  cause of connection time out when
  I'm try to connect to
  <code>ssl://smtp.gmail.com:465</code></li>
  <li>Second,
  can I use <code>tls</code> in <code>PHP 5.3</code>? If yes, can
  you show me the code, so I can put
  it into CodeIgniter and test it.</li>
  </ol>
  
  <p>Thanks.</p>