在CI中集成phpmailer,方便使用SMTP发送邮件
直接使用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.2。接著在 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.2/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.163.com"; // sets 163 as the SMTP server
//$this->mail->Port = 465; // set the SMTP port for the 163 server
$this->mail->Username = "xxxxxx@163.com";// 163 username
$this->mail->Password = "xxxxxxxxx"; // 163 password
/// $this->mail->AddReplyTo('@163.com', 'YOUR_NAME'); //回复地址(可填可不填)
//$this->mail->SetFrom('YOUR_GAMIL@163.com', 'YOUR_NAME');
}
public function sendmail($to, $to_name, $subject, $body){
try{
$this->mail->From = 'xxxx@163.com';
$this->mail->FromName = 'xxxxxx';
$this->mail->AddAddress($to, $to_name);
$mail->WordWrap = 50; // Set word wrap to 50 characters
$this->mail->IsHTML(true); // 使用html格式
$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
class Message extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function send(){
$mail_body = "这是主题的内容";
$this->load->library('mailer');
$this->mailer->sendmail(
'it6780@qq.com',
'charles',
'這是測試信 '.date('Y-m-d H:i:s'),
$mail_body
);
}
}
下。
在CI中集成phpmailer,方便使用SMTP发送邮件的更多相关文章
- 【军哥谈CI框架】之CI中集成百度UEditor
Hello,各位亲,新的一周来临啦,很高兴这么快又跟大家伙见面!话说上一回,军哥带大家用JQuery写了一个城市级联菜单的例子 ,不知道亲们学会了多少,是否自己可以独立写出来了呢. 军哥很是期待大家学 ...
- 将PHPMailer整合到ThinkPHP 3.2 中实现SMTP发送邮件
本内容转载出处:http://my.oschina.net/BearCatYN/blog/299192 并对以下内容做了一处说明. ThinkPHP没有邮件发送的功能,于是,我就想了想,就将PHPMa ...
- [原创]CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接
近年来,由于开源项目.社区的活跃热度大增,进而引来持续集成(CI)系统的诞生,也越发的听到更多的人在说协同开发.敏捷开发.迭代开发.持续集成和单元测试这些拉风的术语.然而,大都是仅仅听到在说而已,国内 ...
- 在Thinkphp3.2 中使用PHPMailer 发送邮件
phpmailer发送邮件是php开发者首选的一个邮件发送插件了,下面我来介绍怎么集成phpmailer到thinkphp框架了,有需要了解的朋友可参考. phpmailer发送邮件功能很强大,今天真 ...
- CI框架使用PHPmailer发送邮件找回密码
之前用PHP+Mysql+jQuery结合ThinkPHP做了一个用户验证邮箱找回密码功能<ThinkPHP之PHP+Mysql+jQuery发送邮箱找回密码>,现在分享一下用CI框架结合 ...
- CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接
原文地址https://www.cnblogs.com/kevingrace/p/5651447.html 近年来,由于开源项目.社区的活跃热度大增,进而引来持续集成(CI)系统的诞生,也越发的听到更 ...
- 【补充】Gitlab 部署 CI 持续集成
上一篇:<劈荆斩棘:Gitlab 部署 CI 持续集成> 上一篇所配置的.gitlab-ci.yml: stages: - build - test before_script: - ec ...
- GitLab CI持续集成配置方案
目录 1. 持续集成介绍 1.1 概念 1.2 持续集成的好处 2. GitLab持续集成(CI) 2.1 简介 2.2 GitLab简单原理图 2.3 GitLab持续集成所需环境 2.4 需要了解 ...
- **【ci框架】PHP的CI框架集成Smarty的最佳方式
因为CI自带的模板功能不是很方便,所以大家普遍采用集成Smarty的方式来弥补CI这方面的不足. 本人在网上看了不少CI集成Smarty的教程,包括咱们CI论坛里面的一个精华帖子 http://cod ...
随机推荐
- python 学习笔记3(循环方式;list初始化;循环对象/生成器/表推导;函数对象;异常处理)
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 16. 循环方式笔记: 1)range(0, 8, 2) #(上限,下限,步长) 可以实现对元素或者下标的 ...
- BZOJ-1305 dance跳舞 建图+最大流+二分判定
跟随YveH的脚步又做了道网络流...%%% 1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 2119 S ...
- Linux文件权限;ACL;Setuid、Setgid、Stick bit特殊权限;sudo提权
相关学习资料 http://blog.sina.com.cn/s/blog_4e2e6d6a0100g47o.html http://blog.csdn.net/aegoose/article/det ...
- 获取本机IP_考虑多网卡的情况
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Java 对象的串行化(Serialization)
1.什么是串行化 对象的寿命通常随着生成该对象的程序的终止而终止.有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复.我们把对象的这种能记录自己的状态以便将来再生的能力.叫作对象的持续性(pe ...
- 运行 appium 自带实例报错:unresolved import:webdriver
python demo 中from appium import webdriver报错unresolved import:webdriver 之所以会报这样的error是因为没有装clientclie ...
- PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明
PHP函数篇详解十进制.二进制.八进制和十六进制转换函数说明 作者: 字体:[增加 减小] 类型:转载 中文字符编码研究系列第一期,PHP函数篇详解十进制.二进制.八进制和十六进制互相转换函数说明 ...
- [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal
前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...
- linux 多线程 LinuxThreads(转)
Linux 线程实现机制分析 http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 上面文章分析的非常透彻 按照教科书上的定义,进程是 ...
- MySQL Cluster(MySQL 集群) 初试(转)
作/译者:叶金荣(imysql#imysql.com>),来源:http://imysql.com,欢迎转载. 作/译者:叶金荣(Email: ),来源:http://imysql.cn,转载请 ...