使用 phpmailer 发送邮件,支持抄送、密送和发送附件
Sonder
2020-04-06
7651字
19分钟
浏览 (3.3k)
1、使用 composer 下载 phpmailer
composer require phpmailer/phpmailer
2、自定义 Mailer.php 文件,我使用的是TP5.1的框架测试,可自定义了参数弄的有点乱,大家将就下
<?php
namespace util;
use PHPMailer\PHPMailer\PHPMailer;
class Mailer
{
// 编码格式为utf8,不设置编码的话,中文会出现乱码
protected $charSet = 'utf-8';
// 发送方的SMTP服务器地址,QQ邮箱为:smtp.qq.com
protected $host = 'smtp.163.com';
// 163邮箱的ssl协议方式端口号是465/994 QQ邮箱的ssl协议方式端口号是465/587
protected $port = 465;
// 是否使用身份验证,默认false
protected $smtpAuth = true;
// 发送方的邮箱用户名,就是你申请163的SMTP服务使用的163邮箱,或者QQ邮箱等
protected $mailUsername = '';
// 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!QQ邮箱同理,如果没有授权码就是邮箱密码
protected $mailPassword = '';
// 使用ssl协议方式
protected $smtpSecure = 'ssl';
// 错误调试,默认关闭
protected $smtpDebug = 0;
// 发送邮件地址
protected $mailFrom = '';
// 发送名称
protected $mailFromname = '';
// 回复邮箱地址
protected $replyMail = '';
/**
* 初始化发送邮件参数
* @param array $conf
*/
public function __construct($conf = [])
{
if (!empty($conf)) {
$this->charSet = !empty($conf['charSet']) ? $conf['charSet'] : $this->charSet;
$this->host = !empty($conf['host']) ? $conf['host'] : $this->host;
$this->port = !empty($conf['port']) ? $conf['port'] : $this->port;
$this->smtpAuth = isset($conf['smtpAuth']) ? $conf['smtpAuth'] : $this->smtpAuth;
$this->mailUsername = !empty($conf['mailUsername']) ? $conf['mailUsername'] : $this->mailUsername;
$this->mailPassword = !empty($conf['mailPassword']) ? $conf['mailPassword'] : $this->mailPassword;
$this->smtpSecure = isset($conf['smtpSecure']) ? $conf['smtpSecure'] : $this->smtpSecure;
$this->smtpDebug = isset($conf['smtpDebug']) ? $conf['smtpDebug'] : $this->smtpDebug;
$this->mailFrom = !empty($conf['mailFrom']) ? $conf['mailFrom'] : $this->mailFrom;
$this->mailFromname = !empty($conf['mailFromname']) ? $conf['mailFromname'] : $this->mailFromname;
} else {
$mail_smtpAuth = config('email.mail_smtpAuth');
$mail_smtpSecure = config('email.mail_smtpSecure');
$mail_smtpDebug = config('email.mail_smtpDebug');
$this->host = !empty(config('email.mail_host')) ? config('email.mail_host') : $this->host;
$this->port = !empty(config('email.mail_port')) ? config('email.mail_port') : $this->port;
$this->smtpAuth = isset($mail_smtpAuth) ? config('email.mail_smtpAuth') : $this->smtpAuth;
$this->mailUsername = !empty(config('email.mail_mailUsername')) ? config('email.mail_mailUsername') : $this->mailUsername;
$this->mailPassword = !empty(config('email.mail_mailPassword')) ? config('email.mail_mailPassword') : $this->mailPassword;
$this->smtpSecure = isset($mail_smtpSecure) ? config('email.mail_smtpSecure') : $this->smtpSecure;
$this->smtpDebug = isset($mail_smtpDebug) ? config('email.mail_smtpDebug') : $this->smtpDebug;
$this->mailFrom = !empty(config('email.mail_mailFrom')) ? config('email.mail_mailFrom') : $this->mailFrom;
$this->mailFromname = !empty(config('email.mail_mailFromname')) ? config('email.mail_mailFromname') : $this->mailFromname;
}
}
/**
* 发送邮件
*
* @param string|array $to_mail 单人发送为邮箱地址,多人发送[['email'=>'***@163.com', 'name'=>'发送人名称']]
* @param string $mail_title 发送邮件标题
* @param string $mail_body 发送邮件正文
* @param string $to_name 单人发送时,个人名称,多人无效
* @param array $options 抄送、秘密抄送、附件设置,格式
* cc, bcc, attachment 使用到哪个传哪个就行了,不需要的可以不写
* [
* 'cc' => [['email'=>'抄送邮箱地址1', 'name'=>'收件人姓名,如果为空此参数可没有']],
* 'bcc' => [['email'=>'秘密抄送邮箱地址1', 'name'=>'收件人姓名,如果为空此参数可没有']],
* 'attachment' => [['file_path'=>'附件地址', 'file_name'=>'附件名称,如果为空此参数可没有']]
* ]
* @return array
*/
public function sendEmail($to_mail = '', $mail_title = '', $mail_body = '', $to_name = '', $options = [])
{
$mailer = new PHPMailer();
// 使用SMTP服务
$mailer->isSMTP();
$mailer->isHTML();
$mailer->SMTPDebug = $this->smtpDebug;
$mailer->CharSet = $this->charSet;
$mailer->Host = $this->host;
$mailer->Port = $this->port;
$mailer->SMTPAuth = $this->smtpAuth;
$mailer->Username = $this->mailUsername;
$mailer->Password = $this->mailPassword;
$mailer->SMTPSecure = $this->smtpSecure;
$mailer->From = $this->mailFrom;
$mailer->FromName = $this->mailFromname;
// 设置收件人信息,如邮件格式说明中的收件人,这里会显示为 zhangsan(yyyy@163.com)
if (is_array($to_mail)) {
foreach ($to_mail as $mail) {
$mailer->addAddress($mail['email'], $mail['name']);
}
} else {
$mailer->addAddress($to_mail, $to_name);
}
if (!empty($this->replyMail)) {
// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址,第二个参数代表回复的邮箱收到邮件后看到名称
$mailer->addReplyTo($this->replyMail, $to_name);
if (is_array($to_mail)) {
foreach ($to_mail as $mail) {
$mailer->addReplyTo($mail['email'], $mail['name']);
}
} else {
$mailer->addReplyTo($to_mail, $to_name);
}
}
// 设置邮件抄送人,邮件地址和姓名
if (isset($options['cc'])) {
$cc_mail = $options['cc'];
foreach ($cc_mail as $mail) {
$mail_name = $mail['name'] ?? $mail['name'];
$mailer->addCC($mail['email'], $mail_name);
}
}
// 设置秘密抄送人,邮件地址和姓名
if (isset($options['bcc'])) {
$bcc_mail = $options['bcc'];
foreach ($bcc_mail as $mail) {
$mail_name = $mail['name'] ?? $mail['name'];
$mailer->addBCC($mail['email'], $mail_name);
}
}
// 设置发送附件,** 文件地址不支持URL格式 **
if (!empty($options['attachment'])) {
$attachment = $options['attachment'];
foreach ($attachment as $val) {
$file_name = isset($val['file_name']) ? $val['file_name'] : '';
$mailer->addAttachment($val['file_path'], $file_name);
}
}
$mailer->Subject = $mail_title; // 邮件标题
$mailer->Body = $mail_body; // 邮件正文
$mailer->AltBody = "请切换到支持 HTML 的邮件客户端,或者使用浏览器打开"; // 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用
// 发送邮件
if (!$mailer->send()) {
// 输出错误信息
return ['code'=>1, 'msg'=>$mailer->ErrorInfo];
}
return ['code'=>0];
}
}
3、测试
# 需要引入文件
use util\Mailer;
# 使用
$Mailer = new Mailer();
# 发送
$email = '****@qq.com';
$options = [
'cc' => [
['email'=>'****@163.com', 'name'=>'163邮箱1']
],
'bcc' => [
['email'=>'****@qq.com', 'name'=>'QQ邮箱2']
],
];
$res = $Mailer->sendEmail($email, '测试标题', '测试内容', 'QQ邮箱1', $options);
print_r($res);
**注意:**发送附件不能使用URL文件地址