Home Article 邮件发送功能

邮件发送功能

Release time:2020-07-03 Author:Mr.赵 Reading volume:10
  1. <?php
  2. /**
  3.  *  +----------------------------------------------------------------------
  4.  *  | ThinkPHP [ WE CAN DO IT JUST THINK ]
  5.  *  +----------------------------------------------------------------------
  6.  *  | Copyright (c) 2020 ahai574 All rights reserved.
  7.  *  +----------------------------------------------------------------------
  8.  *  | Licensed ( ++++ahai574++++ )
  9.  *  +----------------------------------------------------------------------
  10.  *  | Author: 阿海 <764882431@qq.com>
  11.  *  +----------------------------------------------------------------------
  12.  */
  13. namespace app\common\library;
  14. use PHPMailer\PHPMailer\PHPMailer;
  15. use PHPMailer\PHPMailer\Exception;
  16. class PHPMailerLib
  17. {
  18.     /**
  19.      * 开启调试 0 不开启 2开启
  20.      */
  21.     private $SMTPDebug = 0;
  22.     /**
  23.      * 发送服务端 smtp.qq.com  smtp.163.com
  24.      */
  25.     private $host = 'smtp.qq.com';
  26.     /**
  27.      * 是否需要开启认证授权
  28.      */
  29.     private $SMTPAuth = true;
  30.     /**
  31.      * 发送的邮箱地址
  32.      */
  33.     private $userName = '';
  34.     /**
  35.      * 邮箱授权密码
  36.      */
  37.     private $password = '';
  38.     /**
  39.      * 发送人的昵称
  40.      */
  41.     private $sendNickname = '';
  42.     /**
  43.      * 使用的协议
  44.      */
  45.     private $SMTPSecure = 'ssl';
  46.     /**
  47.      * 发送的端口如:25 阿里云服务器是禁用这个端口的,所以建议是使用465端口来发送
  48.      */
  49.     private $port = 465;
  50.     /**
  51.      * 接收者的邮箱,使用英文逗号“,”分割
  52.      */
  53.     private $receiveEmail = '';
  54.     /**
  55.      * 接收者的昵称,使用英文逗号“,”分割
  56.      */
  57.     private $receiveNickname = '';
  58.     /**
  59.      * 附件路径 ,使用英文逗号“,”分割
  60.      */
  61.     private $attachment = null;
  62.     /**
  63.      * 附件文件对应的名称 使用英文逗号“,”分割
  64.      */
  65.     private $attachmentName = '';
  66.     /**
  67.      * 回复人的邮箱,使用英文逗号“,”分割
  68.      */
  69.     private $replyEmail = null;
  70.     /**
  71.      * 回复人的名称,使用英文逗号“,”分割
  72.      */
  73.     private $replyNickname = '';
  74.     /**
  75.      * 邮件的主题 标题
  76.      */
  77.     private $subject = '';
  78.     /**
  79.      * 邮件的主体 html内容
  80.      */
  81.     private $body = '';
  82.     public function __construct($config = null)
  83.     {
  84.         if (is_null($config) || !is_array($config)) {
  85.             throw new Exception("配置必须是数组,参数不能为空,请查看setMailerConf的参数");
  86.         }
  87.         //批量设置参数
  88.         $this->setMailerConf($config);
  89.     }
  90.     /**
  91.      * 批量设置
  92.      * 设置发送服务端的配置
  93.      * @param $array 数组 ['userName'=>'','password'=>'','sendNickname'=>'','host'=>'smtp.163.com','port'=>465,...] 
  94.      */
  95.     public function setMailerConf($array)
  96.     {
  97.         if (is_array($array)) {
  98.             $this->SMTPDebug = isset($array['SMTPDebug']) ? $array['SMTPDebug'] : $this->SMTPDebug;
  99.             $this->userName = isset($array['userName']) ? $array['userName'] : $this->userName;
  100.             $this->password = isset($array['password']) ? $array['password'] : $this->password;
  101.             $this->sendNickname = isset($array['sendNickname']) ? $this->setMailerCharset($array['sendNickname']) : $this->sendNickname;
  102.             $this->host = isset($array['host']) ? $array['host'] : $this->host;
  103.             $this->port = isset($array['port']) ? $array['port'] : $this->port;
  104.             $this->receiveEmail = isset($array['receiveEmail']) ? $array['receiveEmail'] : $this->receiveEmail;
  105.             $this->receiveNickname = isset($array['receiveNickname']) ? $this->setMailerCharset($array['receiveNickname']) : $this->receiveNickname;
  106.             $this->replyEmail = isset($array['replyEmail']) ? $array['replyEmail'] : $this->replyEmail;
  107.             $this->replyNickname = isset($array['replyNickname']) ? $this->setMailerCharset($array['replyNickname']) : $this->replyNickname;
  108.             $this->attachment = isset($array['attachment']) ? $array['attachment'] : $this->attachment;
  109.             $this->attachmentName = isset($array['attachmentName']) ? $this->setMailerCharset($array['attachmentName']) : $this->attachmentName;
  110.             $this->subject = isset($array['subject']) ?  $this->setMailerCharset($array['subject']) : $this->subject;
  111.             $this->body = isset($array['body']) ? $array['body'] : $this->body;
  112.         }
  113.         return $this;
  114.     }
  115.     /**
  116.      * 中文--转码
  117.      */
  118.     private function setMailerCharset($value)
  119.     {
  120.         return "=?utf-8?B?" . base64_encode($value) . "?=";
  121.     }
  122.     /**
  123.      * 开启调试 0 不开 2 开
  124.      */
  125.     public function setDebug($debug = 2)
  126.     {
  127.         $this->SMTPDebug = $debug;
  128.         return $this;
  129.     }
  130.     /**
  131.      * 设置附件
  132.      */
  133.     public function setAttachment($attachment = null, $attachmentName = null)
  134.     {
  135.         $this->attachment = $attachment;
  136.         $this->attachmentName = $attachmentName;
  137.         return $this;
  138.     }
  139.     /**
  140.      * 设置内容主体
  141.      */
  142.     public function setBody($html)
  143.     {
  144.         $this->body = $html;
  145.         return $this;
  146.     }
  147.     /**
  148.      * 设置标题
  149.      */
  150.     public function setSubject($title)
  151.     {
  152.         $this->subject = $title;
  153.         return $this;
  154.     }
  155.     /**
  156.      * 设置收件人 及昵称
  157.      */
  158.     public function setReceiveEmail($email, $nickName = null)
  159.     {
  160.         $this->receiveEmail = $email;
  161.         $this->receiveNickname = $nickName;
  162.         return $this;
  163.     }
  164.     /**
  165.      * 设置回复人邮箱 昵称
  166.      */
  167.     public function setReplyEmail($email, $nickName = null)
  168.     {
  169.         $this->replyEmail = $email;
  170.         $this->replyNickname = $nickName;
  171.         return $this;
  172.     }
  173.     /**
  174.      * 发送邮件
  175.      */
  176.     public function sendEmail()
  177.     {
  178.         date_default_timezone_set("Asia/Shanghai");
  179.         $mail = new PHPMailer(true);
  180.         try {
  181.             //服务端
  182.             $mail->CharSet = 'utf-8';
  183.             //$mail->Encoding  = "base64";
  184.             $mail->setLanguage('zh_cn', env('root_path') . "\\vendor\\phpMailer\\phpmailer\\language\\");
  185.             $mail->SMTPDebug = $this->SMTPDebug;
  186.             $mail->isSMTP();
  187.             $mail->Host = $this->host;
  188.             $mail->SMTPAuth = $this->SMTPAuth;
  189.             $mail->Username = $this->userName;
  190.             $mail->Password = $this->password;
  191.             $mail->SMTPSecure = $this->SMTPSecure;
  192.             $mail->Port = $this->port;
  193.             //发送端 只能一个
  194.             $mail->setFrom($this->userName, $this->sendNickname);
  195.             //接收端
  196.             if (is_null($this->receiveEmail) || empty($this->receiveEmail)) {
  197.                 throw new Exception("必须设置接收者的邮箱");
  198.             }
  199.             $receiveEmails = explode(",", $this->receiveEmail);
  200.             $receiveNicknames =  explode(",", $this->receiveNickname);
  201.             foreach ($receiveEmails as $key => $val) {
  202.                 $mail->addAddress($val, isset($receiveNicknames[$key]) ? $receiveNicknames[$key]  : $val); //如果没有昵称则使用发送者的邮箱作为昵称
  203.             }
  204.             if (!is_null($this->replyEmail) || !empty($this->replyEmail)) {
  205.                 $replyEmails = explode(",", $this->replyEmail);
  206.                 $replyNicknames =  explode(",", $this->replyNickname);
  207.                 foreach ($replyEmails as $key => $val) {
  208.                     $mail->addAddress($val, isset($replyNicknames[$key]) ? $replyNicknames[$key]  : $val); //如果没有昵称则使用回复者的邮箱作为昵称
  209.                 }
  210.             }
  211.             // 添加附件
  212.             if (!is_null($this->attachment)) {
  213.                 $attachments = explode(",", $this->attachment);
  214.                 $attachmentsNames = explode(",", $this->attachmentName);
  215.                 foreach ($attachments as $key => $val) {
  216.                     $ext = substr($val, strrpos($val, '.'));
  217.                     $mail->addAttachment($val, isset($attachmentsNames[$key]) ? $attachmentsNames[$key] . $ext : "附件-" . ($key + 1) . $ext);
  218.                 }
  219.             }
  220.             $mail->isHTML(true);                                  // Set email format to HTML
  221.             $mail->Subject = $this->subject;
  222.             $mail->Body = $this->body;
  223.             if (!$mail->send()) {
  224.                 return ['result' => false, 'errorMsg' => $mail->ErrorInfo];
  225.             } else {
  226.                 return ['result' => true, 'msg' => '邮件发送成功'];
  227.             }
  228.         } catch (Exception $e) {
  229.             return ['result' => false, 'errorMsg' => $mail->ErrorInfo];
  230.         }
  231.     }
  232. }
  
I want to comment
Leave a message
http://boke.zhaozhiqi.top/index.php/
User login
You have not written any reviews yet!
You have commented!
Can only praise once!
You have a collection!