phpmailer封装发送邮件

首先这是一个封装的phpmailer控制器方法,可以直接用

也是其次,这是一个实现了基本发送邮件的代码,

功能包括,发送方,收件方,抄送方,包括邮件附件,暂不支持回复功能,其实只要按照格式加上回复那一部分代码就可以了~

直接上代码:

MailController.php

<?php

namespace mason;

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;

class MailController
{
    /**
     * * phpmailer邮件对象
     * @var PHPMailer
     */
    public $mail;

    /**
     * 基础配置
     * @var array|mixed
     */
    public $config;

    /**
     * 要发送的数据集合
     * @var array|mixed
     */
    public $data;

    /**
     * @var string
     * 默认字符类型
     */
    public $CharSet = 'utf8';

    /**
     * @var int
     * 默认是否调试
     */
    public $SMTPDebug = 0;

    /**
     * @var string
     * 发送方的SMTP服务器地址
     */
    public $host = "smtp.exmail.qq.com";

    /**
     * @var string
     * 使用协议方式
     */
    public $SMTPSecure = "ssl";

    /**
     * @var int
     * 协议端口
     */
    public $port = 465;

    /**
     * @param array $config
     * @param array $data
     * @param PHPMailer $PHPMailer
     * 构造方法
     */
    public function __construct($config = array(), $data = array())
    {
        $this->mail = new PHPMailer(true);

        $this->config = $config;

        $this->data = $data;

        $this->baseConfig();

    }

    /**
     * @return $this
     * 基础配置
     */
    public function baseConfig()
    {
        $this->mail->SMTPDebug = isset($this->config['debug'])? $this->config['debug']: $this->SMTPDebug;

        if(isset($this->config['debug_output']) && $this->config['debug_output'] === true){
            $this->mail->Debugoutput = function ($str, $level){
                echo "$str";
            };
        }

        $this->mail->isSMTP();
        $this->mail->isHTML(false);
        $this->mail->CharSet = isset($this->config['CharSet'])? $this->config['CharSet']: $this->CharSet;

        $this->mail->Host = isset($this->config['host'])? $this->config['host']: $this->host;// 发送方的SMTP服务器地址
        $this->mail->SMTPSecure = isset($this->config['SMTPSecure'])? $this->config['SMTPSecure']: $this->SMTPSecure;// 使用ssl协议方式
        $this->mail->Port = isset($this->config['port'])? $this->config['port']: $this->port;// 163邮箱的ssl协议方式端口号是465/994

        (isset($this->config['SMTPAuth']) && $this->config['SMTPAuth'] === true) && $this->mail->SMTPAuth = true;

        return $this;
    }

    public function senderInfo()
    {
        $this->mail->Username = $this->data['username']; // 发送方的163邮箱用户名,就是你申请163的SMTP服务使用的163邮箱
        $this->mail->Password = $this->data['password']; // 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!

        return $this;
    }

    public function sendData()
    {
        $this->mail->setFrom($this->data['setFrom']); // 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示
        $this->mail->Subject = $this->data['Subject']; // 邮件标题
        $this->mail->Body = $this->data['Body']; //邮件正文

        return $this;
    }

    /**
     * @param $sendTo
     * 接收邮件方
     */
    public function receive($sendTo = '')
    {
        if(!empty($sendTo)){
            if(is_array($sendTo)){
                foreach ($sendTo as $v){
                    $this->mail->addAddress($v);
                }
            }else{
                $this->mail->addAddress($sendTo);
            }
        }
        return $this;
    }

    /**
     * @param $copyFor
     * 抄送邮件方
     */
    public function copyFor($copyFor = '')
    {
        if(!empty($copyFor)){
            if(is_array($copyFor)){
                foreach ($copyFor as $v){
                    $this->mail->addCC($v);
                }
            }else{
                $this->mail->addCC($copyFor);
            }
        }
        return $this;
    }

    /**
     * 添加附件
     * @param string $file
     * @throws \PHPMailer\PHPMailer\Exception
     */
    public function attachment($file = '')
    {
        if(!empty($file)){
            if(is_array($file)){
                foreach ($file as $v){
                    $this->mail->addAttachment($v);
                }
            }else{
                $this->mail->addAttachment($file);
            }
        }
        return $this;
    }

    /**
     * 邮件发送
     */
    public function send()
    {
        try {

            $this->senderInfo();

            $this->sendData();

            $this->receive($this->data['sendTo']);

            $this->copyFor($this->data['copyTo']);

            $this->attachment($this->data['file']);

            // 发送邮件
            if (!$this->mail->send()) {
                echo json_encode([
                    'code' => 400,
                    'msg' => '发送失败:'.$this->mail->ErrorInfo,
                    'data' => [],
                    'other'=>[]
                ]);
                exit;
            }
//            else {
//                echo json_encode([
//                    'code' => 200,
//                    'msg' => '发送成功',
//                    'data' => [],
//                    'other'=>[]
//                ]);
//            }
        }
        catch (Exception $e){
            echo json_encode(['code' => 409, 'msg' => '发送异常:'.$this->mail->ErrorInfo, 'data' => [], 'other'=>[]]);
            exit;
        }
    }
}

调用方式:

$config = [
    //'debug_output' => true, //调试专用
    'SMTPAuth' => true,
];
$data = [
    'username' => $param['sendEmail'], //用户邮件地址
    'password' => $param['emailPassword'], //用户配置的密码
    //'password' => 546345643, //测试异常接收功能,实际运行可删除此行代码
    'sendTo'=>$sendTo, //发送方
    'copyTo'=>$copyTo, //抄送方
    'file'=>array_merge($uploadArr, [public_path('static').$file_path]), //附件,都是地址,最好是本地地址
    'setFrom' => $param['sendEmail'], //谁发送的,用来显示在邮件列表的名称
    'Subject' => '邮件标题', //邮件标题
    'Body' => $user['nick_name'] . "的".date('Y-m-d')."日报已提交, 本邮件由系统自动发送" //邮件内容,这里只是一段话,具体的格式支持请查看文档自行修改
];
$mail = new MailController($config, $data);
$mail->send();

以上~

 

本文为 今天也想见到你 博客文章,转载无需和我联系,但请注明来自 今天也想见到你 博客 0925.wang
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇