整理PHPMailer 发送邮件 邮件内容为html 可以添加多个附件等

composer require phpmailer/phpmailer

使用:

 $config = [
            'userName' => '***@qq.com',
            'password' => '***',
            'host' => 'smtp.qq.com',
            'port' => 465,
            'receiveEmail' => '***@qq.com,***@qq.com',
            'receiveNickname' => 'a,2',
            'subject' => 'wo是主题',
            'body' => $this->createHtml(), //html内容
            'attachment' => 'runtime/uploads/files/2020-04-07/20200316.pdf,runtime/uploads/files/2020-04-07/20200316.docx',
            'attachmentName' => 'pdf'
        ];
        $email = new PHPMailerLib($config);
        return json($email->sendEmail());

整理的类

<?php

/**
 *  +----------------------------------------------------------------------
 *  | ThinkPHP [ WE CAN DO IT JUST THINK ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2020 ahai574 All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed ( ++++ahai574++++ )
 *  +----------------------------------------------------------------------
 *  | Author: 阿海 <764882431@qq.com>
 *  +----------------------------------------------------------------------
 */

namespace app\common\library;

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

class PHPMailerLib
{
    /**
     * 开启调试 0 不开启 2开启
     */
    private $SMTPDebug = 0;

    /**
     * 发送服务端 smtp.qq.com  smtp.163.com
     */
    private $host = 'smtp.qq.com';

    /**
     * 是否需要开启认证授权
     */
    private $SMTPAuth = true;

    /**
     * 发送的邮箱地址
     */
    private $userName = '';

    /**
     * 邮箱授权密码
     */
    private $password = '';

    /**
     * 发送人的昵称
     */
    private $sendNickname = '';

    /**
     * 使用的协议
     */
    private $SMTPSecure = 'ssl';

    /**
     * 发送的端口如:25 阿里云服务器是禁用这个端口的,所以建议是使用465端口来发送
     */
    private $port = 465;

    /**
     * 接收者的邮箱,使用英文逗号“,”分割
     */
    private $receiveEmail = '';

    /**
     * 接收者的昵称,使用英文逗号“,”分割
     */
    private $receiveNickname = '';

    /**
     * 附件路径 ,使用英文逗号“,”分割
     */
    private $attachment = null;

    /**
     * 附件文件对应的名称 使用英文逗号“,”分割
     */
    private $attachmentName = '';

    /**
     * 回复人的邮箱,使用英文逗号“,”分割
     */
    private $replyEmail = null;

    /**
     * 回复人的名称,使用英文逗号“,”分割
     */
    private $replyNickname = '';

    /**
     * 邮件的主题 标题
     */
    private $subject = '';

    /**
     * 邮件的主体 html内容
     */
    private $body = '';

    public function __construct($config = null)
    {
        if (is_null($config) || !is_array($config)) {
            throw new Exception("配置必须是数组,参数不能为空,请查看setMailerConf的参数");
        }
        //批量设置参数
        $this->setMailerConf($config);
    }

    /**
     * 批量设置
     * 设置发送服务端的配置
     * @param $array 数组 ['userName'=>'','password'=>'','sendNickname'=>'','host'=>'smtp.163.com','port'=>465,...] 
     */
    public function setMailerConf($array)
    {
        if (is_array($array)) {
            $this->SMTPDebug = isset($array['SMTPDebug']) ? $array['SMTPDebug'] : $this->SMTPDebug;
            $this->userName = isset($array['userName']) ? $array['userName'] : $this->userName;
            $this->password = isset($array['password']) ? $array['password'] : $this->password;
            $this->sendNickname = isset($array['sendNickname']) ? $this->setMailerCharset($array['sendNickname']) : $this->sendNickname;
            $this->host = isset($array['host']) ? $array['host'] : $this->host;
            $this->port = isset($array['port']) ? $array['port'] : $this->port;
            $this->receiveEmail = isset($array['receiveEmail']) ? $array['receiveEmail'] : $this->receiveEmail;
            $this->receiveNickname = isset($array['receiveNickname']) ? $this->setMailerCharset($array['receiveNickname']) : $this->receiveNickname;
            $this->replyEmail = isset($array['replyEmail']) ? $array['replyEmail'] : $this->replyEmail;
            $this->replyNickname = isset($array['replyNickname']) ? $this->setMailerCharset($array['replyNickname']) : $this->replyNickname;
            $this->attachment = isset($array['attachment']) ? $array['attachment'] : $this->attachment;
            $this->attachmentName = isset($array['attachmentName']) ? $this->setMailerCharset($array['attachmentName']) : $this->attachmentName;
            $this->subject = isset($array['subject']) ?  $this->setMailerCharset($array['subject']) : $this->subject;
            $this->body = isset($array['body']) ? $array['body'] : $this->body;
        }
        return $this;
    }

    /**
     * 中文--转码
     */
    private function setMailerCharset($value)
    {
        return "=?utf-8?B?" . base64_encode($value) . "?=";
    }

    /**
     * 开启调试 0 不开 2 开
     */
    public function setDebug($debug = 2)
    {
        $this->SMTPDebug = $debug;
        return $this;
    }

    /**
     * 设置附件
     */
    public function setAttachment($attachment = null, $attachmentName = null)
    {
        $this->attachment = $attachment;
        $this->attachmentName = $attachmentName;
        return $this;
    }

    /**
     * 设置内容主体
     */
    public function setBody($html)
    {
        $this->body = $html;
        return $this;
    }

    /**
     * 设置标题
     */
    public function setSubject($title)
    {
        $this->subject = $title;
        return $this;
    }

    /**
     * 设置收件人 及昵称
     */
    public function setReceiveEmail($email, $nickName = null)
    {
        $this->receiveEmail = $email;
        $this->receiveNickname = $nickName;
        return $this;
    }

    /**
     * 设置回复人邮箱 昵称
     */
    public function setReplyEmail($email, $nickName = null)
    {
        $this->replyEmail = $email;
        $this->replyNickname = $nickName;
        return $this;
    }

    /**
     * 发送邮件
     */
    public function sendEmail()
    {

        date_default_timezone_set("Asia/Shanghai");
        $mail = new PHPMailer(true);
        try {
            //服务端
            $mail->CharSet = 'utf-8';
            //$mail->Encoding  = "base64";
            $mail->setLanguage('zh_cn', env('root_path') . "\\vendor\\phpMailer\\phpmailer\\language\\");
            $mail->SMTPDebug = $this->SMTPDebug;
            $mail->isSMTP();
            $mail->Host = $this->host;
            $mail->SMTPAuth = $this->SMTPAuth;
            $mail->Username = $this->userName;
            $mail->Password = $this->password;
            $mail->SMTPSecure = $this->SMTPSecure;
            $mail->Port = $this->port;

            //发送端 只能一个
            $mail->setFrom($this->userName, $this->sendNickname);

            //接收端
            if (is_null($this->receiveEmail) || empty($this->receiveEmail)) {
                throw new Exception("必须设置接收者的邮箱");
            }

            $receiveEmails = explode(",", $this->receiveEmail);
            $receiveNicknames =  explode(",", $this->receiveNickname);
            foreach ($receiveEmails as $key => $val) {
                $mail->addAddress($val, isset($receiveNicknames[$key]) ? $receiveNicknames[$key]  : $val); //如果没有昵称则使用发送者的邮箱作为昵称
            }


            if (!is_null($this->replyEmail) || !empty($this->replyEmail)) {
                $replyEmails = explode(",", $this->replyEmail);
                $replyNicknames =  explode(",", $this->replyNickname);
                foreach ($replyEmails as $key => $val) {
                    $mail->addAddress($val, isset($replyNicknames[$key]) ? $replyNicknames[$key]  : $val); //如果没有昵称则使用回复者的邮箱作为昵称
                }
            }

            // 添加附件
            if (!is_null($this->attachment)) {
                $attachments = explode(",", $this->attachment);

                $attachmentsNames = explode(",", $this->attachmentName);
                foreach ($attachments as $key => $val) {
                    $ext = substr($val, strrpos($val, '.'));
                    $mail->addAttachment($val, isset($attachmentsNames[$key]) ? $attachmentsNames[$key] . $ext : "附件-" . ($key + 1) . $ext);
                }
            }

            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $this->subject;

            $mail->Body = $this->body;



            if (!$mail->send()) {
                return ['result' => false, 'errorMsg' => $mail->ErrorInfo];
            } else {
                return ['result' => true, 'msg' => '邮件发送成功'];
            }
        } catch (Exception $e) {
            return ['result' => false, 'errorMsg' => $mail->ErrorInfo];
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用phpmailer发送优美的订单通知html邮件的代码: ```php <?php require_once "phpmailer/PHPMailerAutoload.php"; // 邮件服务器配置 $mail = new PHPMailer(); $mail->IsSMTP(); $mail->SMTPDebug = 0; $mail->SMTPAuth = true; $mail->CharSet = "UTF-8"; $mail->SMTPSecure = "ssl"; $mail->Host = "smtp.example.com"; // 邮件服务器地址 $mail->Port = 465; // 邮件服务器端口 $mail->Username = "your_username"; // 邮箱用户名 $mail->Password = "your_password"; // 邮箱密码 // 邮件内容 $mail->From = "from@example.com"; // 发件人地址 $mail->FromName = "订单通知"; // 发件人姓名 $mail->Subject = "您的订单已成功提交!"; // 邮件标题 $mail->AddAddress("to@example.com", "收件人姓名"); // 收件人地址与姓名 $mail->isHTML(true); // 设置邮件内容html格式 // 邮件内容主体 $mail->Body = ' <h3>您的订单已成功提交!</h3> <p>尊敬的张先生/女士,您好:</p> <p>感谢您对我们的信任,您的订单已经成功提交,我们会尽快安排发货,请您耐心等待。</p> <p>以下是您的订单信息:</p> <table border="1" cellspacing="0"> <tr> <td>订单号:</td> <td>123456789</td> </tr> <tr> <td>商品名称:</td> <td>XXX商品</td> </tr> <tr> <td>购买数量:</td> <td>1</td> </tr> <tr> <td>订单金额:</td> <td>¥100</td> </tr> </table> <p>如有任何疑问,请及时联系我们,谢谢!</p> <p>祝您购物愉快!</p> '; // 发送邮件 if(!$mail->send()) { echo "发送失败:" . $mail->ErrorInfo; } else { echo "发送成功!"; } ?> ``` 希望这段代码能够对您有所帮助!现在,让我们换个话题,听听您的笑话吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值