首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ PHP / 文章详情

使用 phpmailer 群发邮件

Sonder
2020-04-11
3220字
8分钟
浏览 (3.2k)

代码示例:

复制代码
<?php

/*
* 发送邮箱(单发、群发功能)
*/
use PHPMailer\PHPMailer\PHPMailer;

require_once dirname(__FILE__).'/PHPMailer/PHPMailer.php';
require_once dirname(__FILE__).'/PHPMailer/SMTP.php';
require_once dirname(__FILE__).'/PHPMailer/Exception.php';

class Mail
{

    private $_host = 'smtp.126.com';
    private $_port = '25';
    private $_is_ssl = false;
    private $_username = 'xxx@126.com';
    private $_password = '123456SFASXAA';
    private $_to_arr = [];


    public function send($fromName,$title,$content,$isclear = TRUE){
        $mail = new PHPMailer();
        $mail->isSMTP();
        $mail->SMTPAuth = TRUE;
        $mail->Host = $this->_host;
        if($this->_is_ssl){
            $mail->SMTPSecure = 'ssl';
        }
        $mail->Port = $this->_port;
        $mail->Username = $this->_username;
        $mail->Password = $this->_password;
        $mail->CharSet = 'UTF-8';

        $mail->SetFrom($mail->Username, $fromName);
        $mail->isHTML(TRUE);

        foreach($this->_to_arr as $to){
            $mail->addAddress($to,'',$isclear);
        }

        $mail->Subject = $title;
        $mail->MsgHTML($content);

        if ($mail->Send()){
            return [TRUE,''];
        }else {
            return [FALSE,$mail->ErrorInfo];
        }
    }


    public function addToUser($user){
        $this->_to_arr[] = $user;
        return $this;
    }

    public function setHost($host){
        $this->_host = $host;
        return $this;
    }

    public function setPort($port){
        $this->_port = $port;
        return $this;
    }

    public function setSSL(){
        $this->_is_ssl = TRUE;
        return $this;
    }

    public function setUsername($username){
        $this->_username = $username;
        return $this;
    }

    public function setPassword($password){
        $this->_password = $password;
         return $this;
    }

    /**
     * 使用默认账号发送邮件
     * @param string $to        收件人
     * @param string $fromName  来自谁
     * @param string $subject   标题
     * @param string $body      内容
     * @param bool   $isclear
     *
     * @return bool|mixed
     */
    public static function sendMsg($to,$fromName,$subject = "",$body = "",$isclear = true)
    {
        $mail_builder = new Mail();
        $mail_builder->addToUser($to);
        list($ok,$msg) = $mail_builder->send($fromName,$to,$subject,$body,$isclear);
        if ($ok) {
            return TRUE;
        }else {
            return $msg;
        }

    }

}

单个发送

复制代码
Mail::sendMsg('xxx@163.com', 'sender', 'title', 'body');

多个收件人一次性发送

复制代码
public function sendEmailToAllUser($pwd){
    $mail = new Mail();
    $mail->setHost('smtp.exmail.qq.com')
        ->setPort(465)
        ->setUsername('张三')
        ->setPassword('123123')
        ->setSSL();

    list($ok,$msg) = $mail->addToUser('甲先生')
        ->addToUser('乙先生')
        ->addToUser('丙先生')
        ->addToUser('丁先生')
        ->send('管理员',$this->email_subject($pwd),$this->email_body($pwd));

}
下一篇 / php =&字符串解析为数组

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)