5 changed files with 214 additions and 21 deletions
@ -0,0 +1,10 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\model; |
||||
|
|
||||
|
use think\Model; |
||||
|
|
||||
|
class EmailSendLog extends Model |
||||
|
{ |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,142 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\service\user; |
||||
|
|
||||
|
use app\model\AdminConfig; |
||||
|
use app\model\EmailSendLog; |
||||
|
use PHPMailer\PHPMailer\PHPMailer; |
||||
|
|
||||
|
/** |
||||
|
* 发送邮箱 |
||||
|
* 需用用到扩展 composer require phpmailer/phpmailer |
||||
|
*/ |
||||
|
class EmailService |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
* [ |
||||
|
* 'Host'=>"smtp.163.com", |
||||
|
* 'Port'=>"465", |
||||
|
* 'Username'=>"xxx@163.com", |
||||
|
* 'Password'=>"YGFDAVZVRGIQWBXC", |
||||
|
* ], |
||||
|
* [ |
||||
|
* 'Host'=>"smtp.qq.com", |
||||
|
* 'Port'=>"465", |
||||
|
* 'Username'=>"xxx@qq.com", |
||||
|
* 'Password'=>"oxdkempurasfbjjd", |
||||
|
* ], |
||||
|
* @var array |
||||
|
*/ |
||||
|
public $mailList = []; |
||||
|
|
||||
|
|
||||
|
public function __construct() |
||||
|
{ |
||||
|
// 取数据库配置 |
||||
|
|
||||
|
$AdminConfig = new AdminConfig(); |
||||
|
|
||||
|
$emailContentJson = $AdminConfig->where('title', 'email')->where('delete_time', 0)->value('content'); |
||||
|
if ($emailContentJson) { |
||||
|
$emailContentArr = json_decode($emailContentJson, true); |
||||
|
foreach ($emailContentArr as $emailContent) { |
||||
|
if ($emailContent['title'] == 'QQ邮箱' && !empty($emailContent['email']) && !empty($emailContent['token'])) { |
||||
|
$this->mailList[] = [ |
||||
|
'Host' => "smtp.qq.com", |
||||
|
'Port' => "465", |
||||
|
'Username' => $emailContent['email'], |
||||
|
'Password' => $emailContent['token'], |
||||
|
]; |
||||
|
} |
||||
|
if ($emailContent['title'] == '网易邮箱' && !empty($emailContent['email']) && !empty($emailContent['token'])) { |
||||
|
$this->mailList[] = [ |
||||
|
'Host' => "smtp.163.com", |
||||
|
'Port' => "465", |
||||
|
'Username' => $emailContent['email'], |
||||
|
'Password' => $emailContent['token'], |
||||
|
]; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
* @param $toEmail |
||||
|
* @param $bookName |
||||
|
* @param $path |
||||
|
* @return bool|string|void |
||||
|
*/ |
||||
|
public function sendInvoice($toEmail, $bookName, $path) |
||||
|
{ |
||||
|
try { |
||||
|
if (empty($this->mailList)) { |
||||
|
throw new \Exception('邮箱未配置'); |
||||
|
} |
||||
|
$EmailSendLog = new EmailSendLog(); |
||||
|
$list = []; |
||||
|
foreach ($this->mailList as $i => $m) { |
||||
|
$time = strtotime(date("Y-m-d"), time()); |
||||
|
$errorCount = $EmailSendLog->where(['username' => $m['Username'], 'status' => 0])->whereTime('create_time', $time)->count(); |
||||
|
if ($errorCount < 10) {//错误超过十条记录选择下一个邮箱 |
||||
|
$logsCount = $EmailSendLog->where(['username' => $m['Username']])->whereTime('create_time', $time)->count(); |
||||
|
if ($logsCount < 590) {//每日限量六百超过六百选择下一个邮箱 |
||||
|
$list = $m; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if ($list) { |
||||
|
$status = $this->sendEmail($toEmail, $bookName, $path, $list['Username'], $list['Password'], $list['Host'], $list['Port']); |
||||
|
$EmailSendLog->insert(['toemail' => $toEmail, 'bookname' => $bookName, 'path' => $path, 'username' => $list['Username'], 'status' => $status, 'create_time' => time()]); |
||||
|
if ($status == 1) { |
||||
|
return true; |
||||
|
} elseif ($status == 0) { |
||||
|
throw new \Exception('发送失败,请稍后再试'); |
||||
|
} |
||||
|
} else { |
||||
|
throw new \Exception('邮件全部额度使用完毕'); |
||||
|
} |
||||
|
} catch (\Exception $e) { |
||||
|
return $e->getMessage(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 发送邮箱 |
||||
|
* @param $toEmail |
||||
|
* @param $bookName |
||||
|
* @param $path |
||||
|
* @param $Username |
||||
|
* @param $Password |
||||
|
* @param $Host |
||||
|
* @param $Port |
||||
|
* @return bool|string |
||||
|
*/ |
||||
|
public function sendEmail($toEmail, $bookName, $path, $Username, $Password, $Host, $Port) |
||||
|
{ |
||||
|
try { |
||||
|
$mail = new PHPMailer(true); |
||||
|
$mail->isSMTP(); // 使用SMTP服务 |
||||
|
$mail->CharSet = "utf8"; // 编码格式为utf8,不设置编码的话,中文会出现乱码 |
||||
|
$mail->Host = $Host; // 发送方的SMTP服务器地址 |
||||
|
$mail->SMTPAuth = true; // 是否使用身份验证 |
||||
|
$mail->Username = $Username; // 发送方的163邮箱用户名,就是你申请163的SMTP服务使用的163邮箱</span><span style="color:#333333;"> |
||||
|
$mail->Password = $Password; // 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!</span><span style="color:#333333;"> |
||||
|
$mail->SMTPSecure = "ssl"; // 使用ssl协议方式</span><span style="color:#333333;"> |
||||
|
$mail->Port = $Port; // 163邮箱的ssl协议方式端口号是465/994 |
||||
|
$mail->setFrom($Username, $bookName); // 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示 |
||||
|
$mail->addAddress($toEmail); // 设置收件人信息,如邮件格式说明中的收件人,这里会显示为Liang(yyyy@163.com) |
||||
|
$mail->Subject = "需要的发票下载地址"; // 邮件标题 |
||||
|
$mail->IsHTML(true); |
||||
|
$mail->Body = "您好请查看:发票的下载链接 <a href='" . $path . "'>点击下载</a>";// 邮件正文 |
||||
|
$mail->send(); |
||||
|
return true; |
||||
|
} catch (\Exception $e) { |
||||
|
return $e->getMessage(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue