使用CodeIgniter框架发送QQ邮件

使用CodeIgniter框架发送QQ邮件

1.在【CodeIgniter(CI)框架】的根目录下的./application/libraries/下创建【自定义的发送QQ邮件类库:Send_Email.php文件】。


    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

        class Send_Email {
            //类库
            //保存为Send_Email.php。位于./application/libraries/Send_Email.php


            private $config = array();//私有属性:配置Email参数
            private $email = null;

            public function __construct(){
                //将原始的 CodeIgniter 对象通过引用方式赋给变量$CI。& get_instance()
                //使用 get_instance() 函数。这个函数返回一个CodeIgniter super object
                $CI =& get_instance();

                //加载原始email类库。位于框架根目录的./system/libraries/Email.php
                //参见CI手册【library加载器类】:
                //http://codeigniter.org.cn/user_guide/libraries/loader.html?highlight=load#CI_Loader::library
                //参见CI 3.x手册【Email类】:
                //http://codeigniter.org.cn/user_guide/libraries/email.html
                //CodeIgniter 拥有强大的 Email 类支持以下特性:
                //多协议:Mail、Sendmail 和 SMTP
                //SMTP 协议支持 TLS 和 SSL 加密
                //多个收件人
                //抄送(CC)和密送(BCC)
                //HTML 格式邮件 或 纯文本邮件
                //附件
                //自动换行
                //优先级
                //密送批处理模式(BCC Batch Mode),大邮件列表将被分成小批次密送
                //Email 调试工具
                //参见CI 2.x手册【Email类】:
                //http://codeigniter.org.cn/userguide2/libraries/email.html
                $CI->load->library('email');

                //Email实例对象
                $this->email = $CI->email;
                $this->initConfig();
            }

            public function initConfig(){
                //QQ邮箱开启SMTP服务方法:
                //PC端【登录QQ邮箱】->点击【QQ邮箱】顶部的【设置】->进入【邮箱设置】->点击【账户】->找到【POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务】并开启服务->手机扫码登录QQ并授权获取授权码

                //邮件发送协议
                $this->config['protocol'] = 'smtp';
                //SMTP 服务器地址。
                $this->config['smtp_host'] = 'ssl://smtp.qq.com';

                //发件人邮箱地址
                $this->config['smtp_user'] = 'xxxxxxxxx@qq.com';
                //腾讯QQ邮箱开启POP3/SMTP服务或者IMAP/SMTP服务时的授权码
                $this->config['smtp_pass'] = '授权码';
                //发件人名称
                $this->config['smtp_name'] = '发件人名称';

                //SMTP 端口
                $this->config['smtp_port'] = 465;
                //邮件类型。html网页或者text纯文本
                $this->config['mailtype'] = 'html';
                //字符集
                $this->config['charset'] = 'utf-8';
                //是否验证邮件地址
                $this->config['validate'] = true;
                //Email 优先级. 1 = 最高. 5 = 最低. 3 = 正常
                $this->config['priority'] = 1;
                //换行符. (使用 "\r\n" to 以遵守RFC 822).
                $this->config['crlf'] = "\r\n";
                //换行符. (使用 "\r\n" to 以遵守RFC 822).
                $this->config['newline'] = "\r\n";
                //初始化参数
                $this->email->initialize($this->config);
            }

            /**
             * @param $email_user 收件人邮箱地址
             * @param $msg 发送邮件的正文内容
             * @return TRUE:成功  FALSE:失败
             * */
            public function sendMsg($email_user,$msg){
                //设置:发件人邮箱地址、发件人名称
                $this->email->from($this->config['smtp_user'], $this->config['smtp_name']);
                //设置:收件人邮箱地址
                $this->email->to($email_user);
                //设置:邮件主题
                $this->email->subject('来自邮箱:'.$email_user.' 的邮件');
                //发送邮件正文
                $this->email->message($msg);
                //发送EMAIL. 根据发送结果,成功返回TRUE,失败返回FALSE。
                return $this->email->send();
            }

        }

    ?>

2.在【CodeIgniter(CI)框架】的根目录下的./application/controllers/下创建【自定义的发送QQ邮件控制器:test.php文件】。


    <?php
        if (!defined('BASEPATH'))
            exit('No direct script access allowed');

        class Test extends CI_Controller {
            //控制器
            //保存为test.php。位于./application/controllers/test.php

            public function __construct() {
                parent::__construct();

            }


            /**
             * sendMsg($email_user,$msg)
             * @param $email_user 收件人邮箱
             * @param $msg  邮件正文内容
             * @return TRUE:成功  FALSE:失败
             * */
            public function emailSend() {
                //设置需发送邮件的正文
                $data="这是发送QQ邮件的正文内容";

                //library($library[, $params = NULL[, $object_name = NULL]])
                //第三个参数也是可选的,如果为空,类库通常就会被赋值给一个与类库同名的对象。
                //如果希望使用自定义名称,可以通过第三个参数把它传递过去。
                //参见CI 3.x手册:
                //http://codeigniter.org.cn/user_guide/libraries/loader.html?highlight=load#CI_Loader::library
                $this->load->library('Send_Email', null, 'sendQqEmail');

                //设置收件人邮箱地址
                $request=$this->sendQqEmail->sendMsg('xxxxxxxxxx@qq.com', $data);
                //打印邮件发送的结果
                echo $request;
            }

        }

        /* End of file test.php */
        /* Location: ./application/controllers/test.php */  
    ?>

3.浏览访问【http://域名/index.php/test/emailSend】即可发送QQ邮件。

补充:

【CI框架中QQ邮箱发送邮件】
【利用CodeIgniter中的Email类发邮件】
【使用CI框架发送电子邮件】
【CI手册3.x Email类】
【CI 2.x手册Email类】
【CodeIgniter中国】

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值