利用phpmailer类发送邮件

17-05-19 11:52 字数 2223 阅读 3355 已编辑

发送邮件是经常用到的一个业务,比如在用户注册的时候,或者是利用邮件推广产品。

phpmailer是很好用的一个php发送邮件的工具类,利用它可以很方便的发送邮件。支持各种邮箱协议。

首先下载 phpmailer.zip

如何使用呢,下面以tp为例,简单介绍下使用教程。

首先解压 phpmailer/ThinkPHP/Library/Vendor/phpmailer目录。

然后在config.php中添加如下配置。

return [
    'mail_config' =>[
        'host' => 'smtp.mxhichina.com',// SMTP 服务器
        'port' => 25,// SMTP服务器的端口号
        'from' => 'postmaster@xxxx.com',// 你的邮件地址
        'fromName' => 'i am yeqiu',// 发送人名称
        'user' => 'postmaster@xxxx.com',// SMTP服务器用户名
        'pass' => 'xxxxx',// SMTP服务器密码
    ],
];

然后我新建了一个邮件发送类,方便使用时直接调用,你也可以写在Commonfunction.php中,更方便。

我封装的邮件发送类位于Logic下。

namespace CommonLogic;

class EmailLogic{
    public $mailer='';
    public $error='';
    function __construct(){
        vendor('phpmailer.class#phpmailer');
        vendor('phpmailer.class#smtp');
        $this->mailer = new PHPMailer();

        $this->mailer->CharSet   = 'UTF-8';
        $this->mailer->isSMTP();  // 设定使用SMTP服务
        $this->mailer->SMTPDebug  = 0;                     // 关闭SMTP调试功能
        /*
        * 1 = errors and messages
        * 2 = messages only
        */
        $this->mailer->SMTPAuth   = true;                  // 启用 SMTP 验证功能
        //$this->mailer->SMTPSecure = 'ssl';                 // 使用安全协议,开启这个后,会导致发送失败,不知道什么原因

    }
    function send($title,$contents,$sendto){

        $config = C('mail_config');

        if(!empty($title) && !empty($contents) && !empty($sendto)){

            $this->mailer->Subject = $title;

            $this->mailer->Host       = $config['host'];  // SMTP 服务器
            $this->mailer->Port       = $config['port'];  // SMTP服务器的端口号
            $this->mailer->Username   = $config['user'];  // SMTP服务器用户名
            $this->mailer->Password   = $config['pass'];  // SMTP服务器密码

            $this->mailer->setFrom($config['from'], $config['fromName']);

            $replyEmail = $config['FROM_EMAIL'];
            $replyName = $config['FROM_NAME'];
            $this->mailer->addReplyTo($replyEmail, $replyName);
            $this->mailer->msgHTML($contents);

            if(is_array($sendto)){
                foreach($sendto as $to){
                    if(filter_var($to,FILTER_VALIDATE_EMAIL)){
                        $this->mailer->addAddress($to);
                    }else{
                        $this->error='不正确的邮箱格式!';
                        return false;
                    }

                }
            }else{
                if(filter_var($sendto,FILTER_VALIDATE_EMAIL)){
                    $this->mailer->addAddress($sendto);
                }else{
                    $this->error='不正确的邮箱格式!';
                    return false;
                }

            }
            if($this->mailer->send()){
                return true;
            }else{
                $this->error = $this->mailer->ErrorInfo;
                return false;
            }
        }else{
            $this->error='参数不正确!';
            return false;
        }

    }

    /*if(is_array($attachment)){ // 添加附件,我并未测试过。
            foreach ($attachment as $file){
            is_file($file) && $mail->AddAttachment($file);
            }
    }*/
}

使用方法

$emailLogic = new EmailLogic();
try{
    $emailLogic->send('这是一封测试邮件', 'hello,此邮件由phpmailer发送,<a href="https://www.shiqidu.com/p/82">查看教程</a>' ,'123456@qq.com');
}catch (Exception $e){
    $this->writeLogs('邮件发送失败日志' ,$e->getMessage());
}
0人点赞>
关注 收藏 改进 举报
1 条评论
排序方式 时间 投票
dajiang

感谢小友分享,省的自己再向thinkphp中集成了,好用[em_71]

请登录后发表评论