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 ...
随机推荐
- ubuntu
mongoChef: http://3t.io/mongochef/download/core/platform/#tab-id-3 背景色改成豆沙绿: /usr/share/themes/Ambia ...
- Python 3.x 连接数据库(pymysql 方式)
==================pymysql=================== 由于 MySQLdb 模块还不支持 Python3.x,所以 Python3.x 如果想连接MySQL需要安装 ...
- TI PDK3.0 qt 交叉编译环境设置
上午剑锋给的方法,成功在qtcreator上面加入TI官方的qmake以及他的交叉编译器. 1. 步骤如下: 1. 我假设TI PDK3.0 安装目录为顶层目录. 2. 进入linux-devkit ...
- latex公式编号
1 \begin{flalign*} 2 % In this way (this arrange of &), the equation will in the center and alig ...
- Uiautomator--断言的使用
一.断言函数的使用 1.用例结构: 自动化用例结构,一般可以分成一个用例集,然后用例集下面会有非常多的用例组成,我们可以从多个用例中抽出一些用例组成测试套件. 2.用例的标准结构: setUp:初始化 ...
- magento后台使用POST表单时,要使用必要参数form_key才能正常通讯
<form action="<?php echo $this->getSaveUrl() ?>" method="POST" encty ...
- CSS 中Font Awesome 图标(附码表)
HTML中缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. Font Awesome 传送门:http://fontawesome.das ...
- brew 安装 mysql
在网上看到各种教程,都会出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.soc ...
- JS 模块化和打包方案收集
1.这里想讨论的是拆分规则,不是在问哪个工具怎么使用.2.这里没有在想找正确答案,因为感觉这个问题要结合具体业务场景. 随着项目开发越来越大,一开始代码全打包到一个文件的方式,让文件越来越大,接下来要 ...
- 手机开发中的AP与BP的概念
转自:http://blog.csdn.net/macong01/article/details/15504611 手机的AP和BP: AP:ApplicationProcessor,即应用芯片 BP ...