CI框架使用PHPmail插件发送QQ邮件:
有助请顶,不好请评。
0:33 2016/3/12
CI框架使用PHPmail插件发送QQ邮件:
发送成功,不过修改了主机参数,还包含了一个phpmail中的一个另外的文件,详见下方:
参见:http://codeigniter.org.cn/forums/thread-11484-1-1.html
博文摘写:
不知道大家在使用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复制代码
<?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复制代码
<?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 */
复制代码
至於多重收件人之類的設定就要另外再變化了,這邊只是最入門的版本。
注意:我用的时候需要额外包含一个文件,另外,我用的QQ邮箱,所以需要修改主机参数:
        require_once('PHPMailer/class.smtp.php');
// $this->mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
        $this->mail->Host       = "smtp.qq.com";      // sets GMAIL as the SMTP server
修改后的一个函数:
    public function __construct()
    {
        // require_once('PHPMailer_5.2.0/class.phpmailer.php');
        // require_once('../../../PHPMailer/class.phpmailer.php');
        require_once('PHPMailer/class.phpmailer.php');
        require_once('PHPMailer/class.smtp.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->Host       = "smtp.qq.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');
        $this->mail->Username   = "********@qq.com";// GMAIL username
        $this->mail->Password   = "qnrxylzlojcobcej";       // GMAIL password
        $this->mail->AddReplyTo('******@qq.com', '测试的邮件回复人名称');
        $this->mail->SetFrom('*******@qq.com', '测试的发件人名称');
    }
参见上面这个博客成功发送。
另一个博客:在codeigniter的helper用phpmailer 发送邮件 参见:http://blog.csdn.net/jiaochangyun/article/details/7711656
CI框架使用PHPmail插件发送QQ邮件:的更多相关文章
- 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明
		
13:40 2015/11/20 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明 关键点:现在qq邮箱开通smtp服务后会给你一个很长的独立新密码,发邮件配置中的密码需要 ...
 - 【python】脚本连续发送QQ邮件
		
今天习得用python写一个连续发送QQ邮件的脚本,经过测试,成功给国内外的服务器发送邮件,包括QQ邮箱.163邮箱.google邮箱,香港科技大学的邮箱和爱丁堡大学的邮箱.一下逐步解答相关技巧. 首 ...
 - Java发送QQ邮件
		
面试的时候被问到这个问题,别人问我用Java发过邮件没有,被问得一脸懵逼.然后就研究了一下,不是很难,按照网上的方法折腾了几天就搞出来了. 首先,使用QQ邮箱发送邮件之前需要在邮箱里面配置,开启pop ...
 - python3:利用SMTP协议发送QQ邮件+附件
		
转载请表明出处:https://www.cnblogs.com/shapeL/p/9115887.html 1.发送QQ邮件,首先必须知道QQ邮箱的SMTP服务器 http://service.mai ...
 - java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)
		
java 无法发送邮件问题 java 发送qq邮件 报错:java mail Received fatal alert: handshake_failure (使用ssl) javax.mail.M ...
 - Quartz.NET浅谈一 : 简单Job使用(定时发送QQ邮件)
		
Quartz.NET是一个全功能的开源作业调度系统,可用于从最小的应用程序到大型企业系统. 直接上代码吧... 一.新建一个控制台项目 略过 二.安装Nuget包 三.创建发送邮箱辅助工具类 stat ...
 - 电子邮件协议及GO发送QQ邮件
		
目录 一.电子邮件的工作机制 1.1 SMTP 1.2 POP3 1.3 IMAP 二.邮件地址 三.MIME信息 四.使用golang发送qq邮件 一.电子邮件的工作机制 提供电子邮件服务的协议叫做 ...
 - java代码如何发送QQ邮件
		
近来想写一个qq之间互相发送邮件的工具.奈何一直报错服务错误: org.apache.commons.mail.EmailException: Sending the email to the fol ...
 - 使用python发送QQ邮件
		
这里用到了Python的两个包来发送邮件: smtplib 和 email . Python 的 email 模块里包含了许多实用的邮件格式设置函数,可以用来创建邮件“包裹”.使用的 MIMEText ...
 
随机推荐
- MySQL 应用优化
			
一.使用连接池 二.减少对MySQL的访问 (A) 避免对同一数据做重复检索. (B) 使用查询缓存,MySQL的查询缓存会存储SELECT查询的命令文本和相应的结果. (C) 增加CACHE层 三. ...
 - 微信小程序常见问题集合(长期更新)
			
最新更新: 新手跳坑系列:推荐阅读:<二十四>request:fail错误(含https解决方案)(真机预览问题 跳坑指南<七十>如何让微信小程序服务类目审核通过 跳坑六十九: ...
 - connect/express 的参考
			
1.Node.js[5] connect & express简介 对connect中间件的分类比较容易理解. http://www.cnblogs.com/luics/archive/2 ...
 - 不常用的SQL语句记录
			
只知道字段名,查询哪些表有该字段:假如字段名为Index select sysobjects.name as tablename,syscolumns.name as columnname from ...
 - oracleDBA-D2
			
1.超级管理员sys和system的区别: sys权限比system大,system无法查看到当前数据库是否运行在归档模式下,无法关闭数据库.sys是老大,system是老二. 2.OEM-oracl ...
 - 【转】Caffe初试(九)solver及其设置
			
solver算是caffe的核心的核心,它协调着整个模型的运作.caffe程序运行必带的一个参数就是solver配置文件.运行代码一般为 #caffe train --solver=*_solver. ...
 - 泛型T的类型获取
			
T.getClass()或者T.class都是非法的,因为T是泛型变量. 由于一个类的类型是什么是在编译期处理的,故不能在运行时直接在Base里得到T的实际类型. /** * 可以在service层直 ...
 - 如何在一台服务器上安装两个mysql或者更多
			
如何在一台服务器上安装两个mysql 1 前言 上篇写了在一台机器上源码编译安装一个mysql,那么如何在一台机器上源码编译安装两个mysql或者更多呢? 2 环境 mysql ...
 - inotify监控文件变化
			
1.安装inotify-tools yum install make gcc gcc-c++ #安装编译工具 inotify-tools下载地址:http://github.com/downloa ...
 - Python中内置数据类型list,tuple,dict,set的区别和用法
			
Python中内置数据类型list,tuple,dict,set的区别和用法 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, ...