在CI中集成phpmailer,方便使用SMTP发送邮件
本帖最后由 裕波?忘记 于 2012-1-16 14:50 编辑不知道大家在使用CI的email类的时候,是否有遇到麻烦,特别是使用smtp方式的时候,我遇到的是只能使用126邮箱,QQ和gmail都发送不成功,很无懒,最后在我们另外一个站上直接使用了phpmailer,但是直接使用phpmailer的话,有时候不是很方便,特别你的很多功能都是基于CI完成的时候,要相互依赖就不方便了,所以在想,那是否可以将phpmailer集成到CI中呢,像使用email类这样使用他,功夫不负有心人,在网上居然有人分享了很多内容,但是之前的CI是支持插件功能的,所以很多文章都是说的基于插件的方式,现在CI有了新的调整,基于类的方式。最后找到一篇文章,可以帮助我们解决这个问题,将phpmailer集成到CI中,成为类,大家可以去到这个url查看详细的介绍:http://blog.qoding.us/2011/09/codeigniter-using-phpmailer-to-send-email-via-gmail/
我将他的部分内容拷贝如下:
最近要處理一個電子報系統,再用 CI 那跛腳 Email Class 大概會被客訴到瘋掉。所以還是認命改用老牌的 PHPMailer Library。稍微試一下,發現在 CI 裡使用 PHPMailer 相當無痛,先到官網下載一份 PHPMailer (本文完成時的最新版本是 5.2.0),解壓縮後把整個資料夾丟到 CI\application\libraries\PHPMailer_5.2.0。接著在 libraries 下建立新檔案,就叫 mailer.php 好了。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mailer {
var $mail;
public function __construct()
{
require_once('PHPMailer_5.2.0/class.phpmailer.php');
// the true param means it will throw exceptions on errors, which we need to catch
$this->mail = new PHPMailer(true);
$this->mail->IsSMTP(); // telling the class to use SMTP
$this->mail->CharSet = "utf-8"; // 一定要設定 CharSet 才能正確處理中文
$this->mail->SMTPDebug= 0; // enables SMTP debug information
$this->mail->SMTPAuth = true; // enable SMTP authentication
$this->mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$this->mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$this->mail->Port = 465; // set the SMTP port for the GMAIL server
$this->mail->Username = "YOUR_GAMIL@gmail.com";// GMAIL username
$this->mail->Password = "YOUR_PASSWORD"; // GMAIL password
$this->mail->AddReplyTo('YOUR_GAMIL@gmail.com', 'YOUR_NAME');
$this->mail->SetFrom('YOUR_GAMIL@gmail.com', 'YOUR_NAME');
}
public function sendmail($to, $to_name, $subject, $body){
try{
$this->mail->AddAddress($to, $to_name);
$this->mail->Subject = $subject;
$this->mail->Body = $body;
$this->mail->Send();
echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
}
}
/* End of file mailer.php */
接著在 Controller 裡呼叫這支 library 就可以了,範例如下。<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Epaper extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function send(){
$mail_body = "落落長的內文";
$this->load->library('mailer');
$this->mailer->sendmail(
'address@example.com',
'收件人',
'這是測試信 '.date('Y-m-d H:i:s'),
$mail_body
);
}
}
/* End of file epaper.php */
至於多重收件人之類的設定就要另外再變化了,這邊只是最入門的版本。
先试一下,看能否成功{:1_1:} 能用,谢啦,CI自带的太不好用了,我在linux发到QQ邮箱老到垃圾箱里去 我用126的邮箱发送 老是出现 连接不上smtp的提示 其他的都更改了,找不到原因 我的也不行(是按照你的代码改的---只改了自己的邮箱和密码,当然还有smtp,也参考了百科里的使用介绍,就是不行)。
错误提示:SMTP Error: Could not connect to SMTP host.
问下php.ini文件要做相应的配置吗? 本帖最后由 E.TAXI 于 2012-9-14 09:33 编辑
跟楼上一样,显示SMTP Error: Could not connect to SMTP host.
我用的是gmail,主机是godaddy主机.
首先请把$this->mail->SMTPDebug= 1; 打开debug。
得到SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (5) 。
表明服务器不支持ssl。。。结论就是换成默认端口,不要使用gmail。本人已用QQ邮箱测试成功
over 报错Could not instantiate mail function怎么解决啊,急~ $this->mail->SMTPSecure = "ssl"; // sets the prefix to the
我刚开始也出现BUG,修改了这个参数ssl://smtp.gmail.com,就成功了。 dahuilang 发表于 2012-8-28 16:17 static/image/common/back.gif
**** 作者被禁止或删除 内容自动屏蔽 ****
我解决这个问题了。
原因和作者有关系:作者原文中的$this->mail->Username = "YOUR_GAMIL@gmail.com";中有后缀@××××××
但是加上后缀后就会出现SMTP Error: Could not authenticate.
出现SMTP Error: Could not connect to SMTP host.可能原因是$this->mail->SMTPSecure = "ssl";将ssl改成tls试试。
myg_315 发表于 2013-6-23 17:22 static/image/common/back.gif
我解决这个问题了。
原因和作者有关系:作者原文中的$this->mail->Username = "YOUR_GAMIL@gmail.com"; ...
说的太好了,3Q!!!!
页:
[1]
2