zoukankan      html  css  js  c++  java
  • CI(CodeIgniter)框架下使用非自带类库实现邮件发送

    在项目开发过程中,需要到了邮件提醒功能。首先想到的是CI自身带不带邮件发送类,查看帖子,发现CI本身自带,然后试着利用CI自身带的类库来实现,经过搜搜很多帖子,不少开发者反馈CI自身的Email类有问题,也有同僚给出解决方案,但是在我实际过程中并没有后解决。想到之前自己在使用TP3.2开发项目也做过邮件发送功能,就搬了之前的引用类库,应用的了CI里。下面详细介绍步骤。、

    一、下载类库并放入CI扩展目录中

    链接:https://pan.baidu.com/s/1yDSU-JIzwHc00Lwxf9_f9w 密码:olge  //百度云下载地址

    下载完成之后,把两个文件放入/system/libraries目录下。我这里把class.phpmailer.php文件命名成为Pemail.php

    然后编辑Pemail.php文件。

    <?php
    //这里我是参考CI扩展目录下的其它文件,也定义了常量,直接复制搬过来
    defined('BASEPATH') OR exit('No direct script access allowed'); 
    if (version_compare(PHP_VERSION, '5.0.0', '<') ) exit("Sorry, this version of PHPMailer will only run on PHP version 5 or greater!
    ");
    
    class CI_Pemail {  //把类名重命名,并前面加CI_

    二、简单使用方法(代码)

    public function SendMail() {
            $this->load->library('pemail');                         //加载CI的email类
            $this->pemail->IsSMTP();                                // 设置使用SMTP服务器发送Email
            $this->pemail->SMTPSecure = 'ssl';                      // 使用安全协议
            $this->pemail->CharSet ='UTF-8';                        // 设置邮件的字符编码,若不指定,则为'UTF-8'。这里或者设置GBK
            $this->pemail->SMTPDebug = 1;                           // 关闭SMTP调试功能 1 = errors and messages  2 = messages only
            $this->pemail->Host='smtp.qq.com';                      // 设置SMTP服务器。
            $this->pemail->Port = 465;                              // SMTP服务器的端口号
            $this->pemail->SMTPAuth=true;                           // 设置为"需要验证"
            
            $this->pemail->Username='645631686@qq.com';             //设置用户名
            $this->pemail->Password='vaxvhieq*******';              //设置授权码
    
            $this->pemail->AddAddress( '2*******@qq.com');          // 添加收件人地址,可以多次使用来添加多个收件人
            $this->pemail->AddAddress( '5******@qq.com');         // 添加收件人地址,可以多次使用来添加多个收件人
    
            
            $this->pemail->FromName ='phper';                        // 设置发件人名字
            $this->pemail->From ='645631686@qq.com';                 // 设置邮件头的From字段。
            $this->pemail->Subject ='PHP是世界上最美的语言';            // 设置邮件标题
            $this->pemail->Body ='这话没毛病~';                        // 设置邮件正文
            
    
            return $this->pemail->Send();                            // 发送邮件。
        }

    补充:关于设置QQ邮箱开启和调试过程中的一些BUG,可以从百度搜到解决方案。或者问我就帮你解决

     

  • 相关阅读:
    Win7 IIS FTP
    (转)Windows平台下git中文乱码的问题
    (转)如何在MySql中记录SQL日志(例如Sql Server Profiler)
    (转)Mysql 使用
    Xcode 快捷键
    (转)xcode 4.2 新建工程模板详解
    解决apache的the requested operation has failed
    ANDROID 获取SD卡剩余容量
    (转)struct tm 的应用 了解strtok应用
    (转)Ubuntu建立PHP服务器(apache+php+mysql)
  • 原文地址:https://www.cnblogs.com/wt645631686/p/9050532.html
Copyright © 2011-2022 走看看