|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Foundation\Utils; |
| 6 | + |
| 7 | +use App\Exception\Handler\BusinessException; |
| 8 | +use PHPMailer\PHPMailer\PHPMailer; |
| 9 | + |
| 10 | +/** |
| 11 | + * 邮件工具类 |
| 12 | + * Class Mail |
| 13 | + * @package App\Foundation\Utils |
| 14 | + * @Author YiYuan-Lin |
| 15 | + * @Date: 2021/8/17 |
| 16 | + */ |
| 17 | +class Mail |
| 18 | +{ |
| 19 | + /** |
| 20 | + * 邮件类实例化 |
| 21 | + * @var |
| 22 | + */ |
| 23 | + static private $mail; |
| 24 | + |
| 25 | + /** |
| 26 | + * Mail类对象 |
| 27 | + * @var |
| 28 | + */ |
| 29 | + static public $obj = null; |
| 30 | + |
| 31 | + /** |
| 32 | + * 初始化邮件工具类 |
| 33 | + * @param array $config |
| 34 | + * config['char_set'] string 邮件编码 |
| 35 | + * config['smtp_debug'] bool 是否调试模式输出 |
| 36 | + * config['is_html'] bool 是否HTML输出 |
| 37 | + * config['host'] string SMTP服务器 |
| 38 | + * config['port'] int 服务器端口 |
| 39 | + * config['smtp_auth'] bool 允许SMTP认证 |
| 40 | + * config['username'] string SMTP用户名 |
| 41 | + * config['password'] string SMTP密码 |
| 42 | + * config['smtp_secure'] tls/ssl 允许 TLS 或者ssl协议 |
| 43 | + * @return Mail|null |
| 44 | + */ |
| 45 | + public static function init(array $config = []) |
| 46 | + { |
| 47 | + self::$mail = new PHPMailer(true); |
| 48 | + self::__setConfig($config); |
| 49 | + if (!is_object(self::$obj)) self::$obj = new self; |
| 50 | + return self::$obj; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * 设置参数 |
| 55 | + * @param array $config |
| 56 | + */ |
| 57 | + private static function __setConfig(array $config) |
| 58 | + { |
| 59 | + //使用SMTP |
| 60 | + self::$mail->isSMTP(); |
| 61 | + //设定邮件编码 |
| 62 | + self::$mail->CharSet = $config['char_set'] ?? "UTF-8"; |
| 63 | + //调试模式输出 |
| 64 | + self::$mail->SMTPDebug = $config['smtp_debug'] ?? 0; |
| 65 | + //是否HTML输出 |
| 66 | + self::$mail->isHTML($config['is_html'] ?? true); |
| 67 | + //SMTP服务器 |
| 68 | + self::$mail->Host = $config['host'] ?? env('MAIL_SMTP_HOST', 'smtp.163.com'); |
| 69 | + //服务器端口 25 或者465 具体要看邮箱服务器支持 |
| 70 | + self::$mail->Port = $config['port'] ?? env('MAIL_SMTP_PORT', 465); |
| 71 | + //允许SMTP认证 |
| 72 | + self::$mail->SMTPAuth = $config['smtp_auth'] ?? true; |
| 73 | + //SMTP用户名 |
| 74 | + self::$mail->Username = $config['username'] ?? env('MAIL_SMTP_USERNAME', 'SMTP用户名'); |
| 75 | + //SMTP密码 部分邮箱是授权码(例如163邮箱) |
| 76 | + self::$mail->Password = $config['password'] ?? env('MAIL_SMTP_PASSWORD', '密码或者授权码'); |
| 77 | + //允许 TLS 或者ssl协议 |
| 78 | + self::$mail->SMTPSecure = $config['smtp_secure'] ?? env('MAIL_SMTP_ENCRYPTION', 'ssl'); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * 设置发件人 |
| 83 | + * @param string $email |
| 84 | + * @param string $name |
| 85 | + * @return mixed |
| 86 | + */ |
| 87 | + public function setFromAddress(string $email, string $name) : self |
| 88 | + { |
| 89 | + self::$mail->setFrom($email, $name); |
| 90 | + return $this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * 设置收件人信息 |
| 95 | + * @param string $email |
| 96 | + * @param string $name |
| 97 | + * @return self |
| 98 | + */ |
| 99 | + public function setAddress(string $email, string $name) : self |
| 100 | + { |
| 101 | + self::$mail->addAddress($email, $name); |
| 102 | + return $this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * 设置收件人信息(多收件人) |
| 107 | + * @param array $emailInfo |
| 108 | + * @return self |
| 109 | + */ |
| 110 | + public function setMoreAddress(array $emailInfo) : self |
| 111 | + { |
| 112 | + foreach ($emailInfo as $item) { |
| 113 | + self::$mail->addAddress($item['email'], $item['name']); |
| 114 | + } |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * 抄送 |
| 120 | + * @param string $email |
| 121 | + * @return self |
| 122 | + */ |
| 123 | + public function addCC(string $email) : self |
| 124 | + { |
| 125 | + self::$mail->addCC($email); |
| 126 | + return $this; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * 密送 |
| 131 | + * @param string $email |
| 132 | + * @return self |
| 133 | + */ |
| 134 | + public function addBCC(string $email) : self |
| 135 | + { |
| 136 | + self::$mail->addBCC($email); |
| 137 | + return $this; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * 添加附件 |
| 142 | + * @param string $url |
| 143 | + * @param string $fileName |
| 144 | + * @return self |
| 145 | + */ |
| 146 | + public function addAttachment(string $url, string $fileName = '') : self |
| 147 | + { |
| 148 | + self::$mail->addAttachment($url, $fileName); |
| 149 | + return $this; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * 设置邮件标题 |
| 154 | + * @param string $title |
| 155 | + * @return self |
| 156 | + */ |
| 157 | + public function setSubject(string $title) : self |
| 158 | + { |
| 159 | + self::$mail->Subject = $title; |
| 160 | + return $this; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * 设置邮件内容 |
| 165 | + * @param string $body |
| 166 | + * @return self |
| 167 | + */ |
| 168 | + public function setBody(string $body) : self |
| 169 | + { |
| 170 | + self::$mail->Body = $body; |
| 171 | + return $this; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * 发送邮件 |
| 176 | + * @return bool |
| 177 | + */ |
| 178 | + public function send() |
| 179 | + { |
| 180 | + try { |
| 181 | + if (self::$mail->send()) return true; |
| 182 | + return false; |
| 183 | + }catch (\Exception $e) { |
| 184 | + Throw new BusinessException(400, $e->getMessage()); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments