用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2085|回复: 1
收起左侧

[库 Library] 分享一个PHPMailer和CI配合的代码

[复制链接]
发表于 2018-4-24 23:46:59 | 显示全部楼层 |阅读模式
分享一个PHPMailer和CI配合的代码
以前应该有人发过,我整理了一下

然后顺便把可用的PHPmailer一块放上来

希望大家可以多多交流
我觉得CI超级棒!
这是放在\application\libraries\下,新建一个mailer.php的文件
PHP复制代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Mailer {
 
    var $mail;
 
    public function __construct()
    {
        require_once("PHPMailerLib/class.smtp.php");
        require_once('PHPMailerLib/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->Subject = 'From Amazone Server';
        $this->mail->Body    = 'From Amazone Server';
    }
 
    public function sendmail($receive_mail,$mail_host,$mail_port,$mail_username,$mail_password,$filepath,$filename){
        try{
            $this->mail->AddAddress($receive_mail, '收件人');
 
            /*客户端设置*/
            $this->mail->Host       = $mail_host;      // sets GMAIL as the SMTP server
            $this->mail->Port       = $mail_port;  
            $this->mail->Username   = $mail_username;  // GMAIL username
            $this->mail->Password   = $mail_password;            // GMAIL password
            $this->mail->AddReplyTo($mail_username, 'From Amazone Server');
            $this->mail->SetFrom($mail_username, 'From Amazone Server');
            $this->mail->AddAttachment(dirname(BASEPATH).'/'.$filepath,$filename);
 
 
 
            $this->mail->Send();
                echo "邮件已发送!</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 */
复制代码


压缩包放在\application\libraries\PHPMailerLib文件夹下,没有就新建



文件结构如下



控制控制器访问//自己传递参数即可,这个里面包含的变量我都打备注了
调用的是库里面的第一个function
你可以自己改function的

如果想要增加标题和内容项的话
就把这两个赋值写到function里就可以啊

PHP复制代码
        $this->mail->Subject = 'From Amazone Server';
        $this->mail->Body    = 'From Amazone Server';
复制代码




控制控制器访问代码
PHP复制代码
        public function send(){
        $this->load->library('mailer');//调用
 
        $receive_mail = '';//接受邮件地址
        $mail_host = '';//发送邮件SMTP服务器
        $mail_port = '465';//SSL端口号
        $mail_username = '';//SMTP账号
        $mail_password = '';//SMTP密码
        $filepath = './upfile/7f270639248494f2838ea5d963fe9e7f.html';//附件地址
        $filename = '404.html';//附件名称
        $this->mailer->sendmail($receive_mail,$mail_host,$mail_port,$mail_username,$mail_password,$filepath,$filename);
        }
复制代码


有啥问题可以在下面提出来呀

能帮的一定帮的

评分

参与人数 1威望 +5 收起 理由
Hex + 5 赞一个!

查看全部评分

本版积分规则