直接使用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发送邮件的更多相关文章

  1. 【军哥谈CI框架】之CI中集成百度UEditor

    Hello,各位亲,新的一周来临啦,很高兴这么快又跟大家伙见面!话说上一回,军哥带大家用JQuery写了一个城市级联菜单的例子 ,不知道亲们学会了多少,是否自己可以独立写出来了呢. 军哥很是期待大家学 ...

  2. 将PHPMailer整合到ThinkPHP 3.2 中实现SMTP发送邮件

    本内容转载出处:http://my.oschina.net/BearCatYN/blog/299192 并对以下内容做了一处说明. ThinkPHP没有邮件发送的功能,于是,我就想了想,就将PHPMa ...

  3. [原创]CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接

    近年来,由于开源项目.社区的活跃热度大增,进而引来持续集成(CI)系统的诞生,也越发的听到更多的人在说协同开发.敏捷开发.迭代开发.持续集成和单元测试这些拉风的术语.然而,大都是仅仅听到在说而已,国内 ...

  4. 在Thinkphp3.2 中使用PHPMailer 发送邮件

    phpmailer发送邮件是php开发者首选的一个邮件发送插件了,下面我来介绍怎么集成phpmailer到thinkphp框架了,有需要了解的朋友可参考. phpmailer发送邮件功能很强大,今天真 ...

  5. CI框架使用PHPmailer发送邮件找回密码

    之前用PHP+Mysql+jQuery结合ThinkPHP做了一个用户验证邮箱找回密码功能<ThinkPHP之PHP+Mysql+jQuery发送邮箱找回密码>,现在分享一下用CI框架结合 ...

  6. CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接

    原文地址https://www.cnblogs.com/kevingrace/p/5651447.html 近年来,由于开源项目.社区的活跃热度大增,进而引来持续集成(CI)系统的诞生,也越发的听到更 ...

  7. 【补充】Gitlab 部署 CI 持续集成

    上一篇:<劈荆斩棘:Gitlab 部署 CI 持续集成> 上一篇所配置的.gitlab-ci.yml: stages: - build - test before_script: - ec ...

  8. GitLab CI持续集成配置方案

    目录 1. 持续集成介绍 1.1 概念 1.2 持续集成的好处 2. GitLab持续集成(CI) 2.1 简介 2.2 GitLab简单原理图 2.3 GitLab持续集成所需环境 2.4 需要了解 ...

  9. **【ci框架】PHP的CI框架集成Smarty的最佳方式

    因为CI自带的模板功能不是很方便,所以大家普遍采用集成Smarty的方式来弥补CI这方面的不足. 本人在网上看了不少CI集成Smarty的教程,包括咱们CI论坛里面的一个精华帖子 http://cod ...

随机推荐

  1. 查询条件Where

    1.字符串 $condition = 'name=\'Lily\' and age>10'; 2.数组 ['type' => 1, 'status' => 1] //生成 (type ...

  2. Oracle定时执行存储过程

    首先查看 SQL> show parameter job NAME                                 TYPE        VALUE-------------- ...

  3. SQLServer错误:过程 sp_addextendedproperty,第 xxx 行对象无效。'dbo.xxx.xxx' 不允许有扩展属性,或对象不存在。

    上传数据库到虚拟主机,在执行SQL脚本的时候出现以下的错误: 消息 15135,级别 16,状态 8,过程 sp_addextendedproperty,第 37 行 对象无效.'dbo.Messag ...

  4. 2-SAT开坑

    Reference:http://blog.csdn.net/jarjingx/article/details/8521690 其中伍昱的ppt不错. 2SAT最裸的模型: 一国有n个党派,每个党派在 ...

  5. Uva11729 Commando War

    相邻两个士兵交换顺序,不会对其他的有所影响,贪心考虑两两之间交换策略即可. sort大法好.印象中这类排序题里有一种会卡sort,只能冒泡排序,然而到现在还没有遇到 /**/ #include< ...

  6. API与软件架构

    http://blog.csdn.net/horkychen/article/details/46612899 从架构设计的角度来看(所谓的组成论),软件系统就是模块和接口. 模块(层次/组件)决定分 ...

  7. 代码重构-3 用Tuple代替 out与ref

    返回单一值是良好的编程习惯 原代码: public LotteryViewModel ValidateLottery(LotteryBaseData baseData, int authTime, o ...

  8. MyEclipse------PreparedStatement使用方法

    testPreparedStatement.jsp <%@ page language="java" import="java.util.*" pageE ...

  9. Common Pitfalls In Machine Learning Projects

    Common Pitfalls In Machine Learning Projects In a recent presentation, Ben Hamner described the comm ...

  10. ssh中getHibernateTemplate()的方法

    spring接着又把业务类中的查询也封装成了find() //用来实现分页 /*HibernateTemplate ht=this.getHibernateTemplate(); DetachedCr ...