CI 使用googlemail

url http://codeigniter.com/forums/viewthread/84689/P0/

 

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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值