tags:

views:

469

answers:

1

I have use gmail smtp to send email from the web application that I wrote using CodeIgniter 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.

A: 

According to the PHP docu SSL and TLS were both added in version 4.3.

http://de2.php.net/manual/de/function.fsockopen.php

So, they should work in general.

I found this one and it worked for me:

http://tareq.wedevs.com/2010/01/sending-mail-with-gmails-smtp-server-with-fsockopen/

Maybe you could try that script too.

JochenJung
I have write in my question above that the script worked fine in my localhost before I upgraded to PHP 5.3. The same unchanged script also worked in my dreamhost hosting. CodeIgniter is using fsockopen internally, you can see it from the log error message.
Donny Kurnia