CodeIgniter and Gmail SMTP Timeout with SSL scoket

13,775

If you've come to this page from Google.com looking for more details on Secure SMTP email, check out this patch at the CI Forums.

http://codeigniter.com/forums/viewthread/158882/

My SMTP provider (Exchange system) doesn’t accept SSL connections and requires the use of SMTP over TLS instead (STARTTLS, RFC 3207). Here is a small patch to the Email class which adds support for it. It is written against 1.7.2. If this is useful to others, you’re most welcome to incorporate it in a future release.

Usage: Specify server settings as a regular SMTP server (tcp://servername, typically port 25 or 587). Enable new setting in config ($config[‘starttls’] = TRUE;)

Depends on TLS being available (i.e., listed in the ‘Registered Stream Socket Transports’ section of phpinfo()).

Share:
13,775
Donny Kurnia
Author by

Donny Kurnia

I'm a web developer. I live in Bandung, Indonesia, GMT +7. My speciality is building websites using Ruby on Rails, Wordpress, Codeigniter, and Laravel framework. I use jQuery for frontend effect and enhancement.

Updated on June 04, 2022

Comments

  • Donny Kurnia
    Donny Kurnia almost 2 years

    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:

      $config['protocol']   = 'smtp';
      $config['smtp_host']  = $this->smtp_host;
      $config['smtp_port']  = $this->smtp_port;
      $config['smtp_user']  = $this->smtp_username;
      $config['smtp_pass']  = $this->smtp_password;
      $config['mailtype']   = $this->email_type;
      $config['newline'] = "\r\n";
      $this->load->library('email', $config);
    
      $this->email->from($email_sender, $email_sender_name);
      $this->email->to($email_to);
      $this->email->subject($message_subject);
      $this->email->message($message_content);
      $result = $this->email->send();
    

    When I use "ssl://smtp.gmail.com" as host and 465 as port, I got this error in log:

    DEBUG - 2010-08-06 17:19:24 --> Email Class Initialized
    ERROR - 2010-08-06 17:19:45 --> Severity: Warning  --> 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 --> Language file loaded: language/english/email_lang.php
    ERROR - 2010-08-06 17:19:45 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
    

    I don't know if this related, but before I upgrade my PHP into PHP 5.3, 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.

    After I looking in various forums, I got a tips to do this test:

    $ 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
    

    while using same tool to test ssl connection give me timeout:

    $ openssl s_client -ssl2 -crlf -connect smtp.gmail.com:465
    connect: Connection timed out
    connect:errno=110
    

    I try to use "tls://smtp.gmail.com" as host and 587 as port in my code above, but now I get this error message:

    DEBUG - 2010-08-06 17:26:10 --> Email Class Initialized
    ERROR - 2010-08-06 17:26:10 --> Severity: Warning  --> 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 --> Severity: Warning  --> fsockopen(): Failed to enable crypto /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1652
    ERROR - 2010-08-06 17:26:10 --> Severity: Warning  --> 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 --> Language file loaded: language/english/email_lang.php
    ERROR - 2010-08-06 17:26:10 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> 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 --> Severity: Warning  --> fgets() expects parameter 1 to be resource, boolean given /data/donny/Projects/workspace/codeigniter/libraries/Email.php 1818
    
    1. Can you help me find out what the cause of connection time out when I'm try to connect to ssl://smtp.gmail.com:465
    2. Second, can I use tls in PHP 5.3? If yes, can you show me the code, so I can put it into CodeIgniter and test it.

    Thanks.