Part of the EllisLab Network
   
1 of 2
1
Gmail SMTP using SSL/TLS
Posted: 09 July 2008 08:16 PM   [ Ignore ]  
Summer Student
Total Posts:  6
Joined  07-09-2008

I only heard about codeigniter a couple days ago so forgive me if im repeating something found either on the forums or bugtracker etc.

I’ve been trying to use the included Email library in conjunction with my favourite Google Mail service and, like many others, i’ve been having many problems.

Google Mail requires encryption ( SSL/TLS ) for its SMTP access and it has been noted that this is not something that CodeIgniter currently supports. Understanding this I decided to go about either modifying or rewriting the CI_Email class to include these features.

After some reading it seems that SSL and TLS are in fact the same thing ( the protocol was renamed? ). I think its fair to say the most common misunderstanding of these two protocols are that SSL is encryption enabled during connection and that TLS is encryption enabled on the fly after a connection has been made ( hence the STARTTLS ESMTP command ).

In any case, after several hours of farting about, i’ve realised that CodeIgnighter will indeed work with the Gmail service without any modification of the core classes.

Firstly, your PHP installation must include support for openssl ( either compiled in, or with the php_openssl.dll extension loaded ). You will want to read the pages surrounding OpenSSL Installation within the PHP Manual.

With that done, you just need your standard use of the email library with a few noticable changes from the norm:

$config = Array(
    
'protocol' => 'smtp',
    
'smtp_host' => 'ssl://smtp.googlemail.com',
    
'smtp_port' => 465,
    
'smtp_user' => 'gmail.login@googlemail.com',
    
'smtp_pass' => 'your_password',
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

$this->email->from('gmail.login@googlemail.com', 'Your Name');
$this->email->to('recipient@destination.tld');

$this->email->subject(' CodeIgniter Rocks Socks ');
$this->email->message('Hello World');


if (!
$this->email->send())
    
show_error($this->email->print_debugger());
else
    echo
'Your e-mail has been sent!';

The important parts of the above code are the inclusion of the ssl:// protocol before the smtp hostname, and setting the newline variable to CRLF. The port value of 465 may also be important ( 25 doesnt work at least).

Everything is now working as expected here, hope this helps someone.

Also, I noticed that the documentation recommends using $this->email->initialize($config_array) for setting your mailing preferences however it does not take the nesecarry steps to enable authentication should smtp_user & smtp_pass be defined. The good news is that you can pass the exact same $config_array to your $this->load->library(); call like so:

$this->load->library('email', $config_array);

This will do exactly the same as initialize($config_array) but will properly configure for authentication should be it required. Perhaps there is a reason for initalize()‘s behaviour but the documentation is misleading and doesnt mention $this->load->library(‘email’, $config_array); at all.


I havnt decided if I will use CodeIgnigter as part of my usual dev toolset, its certainly an interesting framework, but i’ve been impressed with both it and its community so far. So keep up the good work ;D

Profile
 
 
Posted: 10 July 2008 09:08 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1142
Joined  08-06-2006

superb first post!

thanks for your hard work and feedback, wrs.

the bug with with email class is noted here.

since you are sending email you might be interested in an imap_pop class i wrote… imap_pop class on the wiki

cheers. here’s hoping you stick around! grin

 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 19 July 2008 12:01 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  9
Joined  07-18-2008

wrs, i did it, but didn’t work.
Following the error of $this->email->print_debugger():

“The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://mail.google.com/support/bin/answer.py?answer=14257 6sm2484862yxg.6”

Could you help me?

Tks

Profile
 
 
Posted: 21 July 2008 10:12 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  6
Joined  07-09-2008

Peri,

I rechecked the code I posted, all is working fine here. You told us the error message you received, but not what you did to receive it…

But i’m guessing you’ve fallen victim to the $this->email->initialize() bug I described above where, even if smtp_user and smtp_pass are set, SMTP authentication is not used.

If you are using the following code:

$this->load->library('email');
$this->email->initialize($config_array);

then consider changing it to:

$this->load->library('email', $config_array);
Profile
 
 
Posted: 01 August 2008 04:13 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  17
Joined  05-08-2007

Good job wrs, thank you for sharing it. It works perfectly.

Profile
 
 
Posted: 01 September 2008 11:50 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  27
Joined  09-16-2007

Thanks for this post. Working great.

I found that all I needed to do was to put the settings into the email.php config file (system/config/email.php), and use $this->email->set_newline(”\r\n”).

$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'username@mygoogleappsdomain.com';
$config['smtp_pass'] = '123xyz';
$config['smtp_port'] = '465';
Profile
 
 
Posted: 02 September 2008 11:32 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  19
Joined  08-27-2008

Excellent! I was just wondering about this myself and here is the answer. Great post!

 Signature 

PHP Screencast Tutorials

Profile
 
 
Posted: 15 January 2009 05:25 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Avatar
Total Posts:  4
Joined  06-15-2008

Thanks for sharing this information. Great, helpful post!

Profile
 
 
Posted: 15 January 2009 08:07 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  17
Joined  01-07-2009

Thanks, your post was very helpfull, just what i was looking for.

I am testing and developing locally with an XAMPP installation and Code Ignite and wanted to be able to send emails from my machine using SMTP and my gmail account.

Your post allowed me to get this set up.

The other thing I needed to do was to enable OpenSSL in my XAMPP installation. Thanks to http://w3it.com/blog/?p=7

Open /apache/bin/php.ini file

Find the line with ;extension=php_openssl.dll and uncomment it by removing the semi-colon from the start of the line.

Restart Apache.

Hope that helps somebody

Profile
 
 
Posted: 24 February 2009 08:06 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  287
Joined  01-25-2008

Great post.
Gawd I love CodeIgniter

 Signature 

http://PawshPal.com/ - Pets Rule

Profile
 
 
Posted: 07 April 2009 12:46 PM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  62
Joined  04-02-2008

Worked great and fixed my smtp problem!

Thanks!

Profile
 
 
Posted: 23 May 2009 07:44 AM   [ Ignore ]   [ # 11 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1142
Joined  08-06-2006

my ISP just blocked port 25 outgoing so i had to figure out how to get around it.

i just implemented this google smtp set up and it is working great - even using google apps with my own domain.

i had one problem that is worth mentioning… i forgot to add the crucial line:

$this->email->set_newline("\r\n");

and it resulted in an 500 “Internal Server Error” page and php_wrapper.cgi message in the php error log. so i ended up going on a wild goose chase. eventually, i realized that i had left out the set_newline() call.

moral of the story: make sure you set_newline() as described by the OP.

 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 06 July 2009 03:56 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  14
Joined  06-15-2009

Looks so great, just one small thing… I put all the params into email.php and the page “hangs” forever, even though the timeout is set to 30 secs. no echo with debug either (i used to get it before making other mistakes). any idea what can be happening?
thanks in advance!!!

email.php

$config['protocol']='smtp';
$config['smtp_host']='ssl://smtp.googlemail.com';
$config['smtp_port']='465';
$config['smtp_timeout']='30';
$config['smtp_user']='my gmail';
$config['smtp_pass']='my gmail pass';
$config['charset']='utf-8';
$config['newline']='\r\n';

the controller

$this->load->library(array('form_validation','recaptcha','email'));
[...]

$this
->email->from('suport@domain.com','tralalala');
$this->email->to($this->input->post('email'));
$this->email->subject('testing');
$this->email->message('live! damn it...');
$this->email->send();
echo
$this->email->print_debugger();
Profile
 
 
Posted: 06 July 2009 06:35 PM   [ Ignore ]   [ # 13 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1142
Joined  08-06-2006

you need to use double-quotes on your

$config['newline']="\r\n";

- single quotes do not work.

 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 06 July 2009 11:03 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  14
Joined  06-15-2009

So simple… thank you!!!  LOL

Profile
 
 
Posted: 07 July 2009 08:06 AM   [ Ignore ]   [ # 15 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1142
Joined  08-06-2006

you are welcome! and thank YOU for *posting code* so the problem was immediately apparent. i wish other people asking questions in these forums were as proactive! grin

 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 115022 Total Logged-in Users: 73
Total Topics: 122459 Total Anonymous Users: 6
Total Replies: 647355 Total Guests: 493
Total Posts: 769814    
Members ( View Memberlist )