You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
2.0 KiB
56 lines
2.0 KiB
<?php
|
|
|
|
namespace app\utility;
|
|
|
|
use PHPMailer\PHPMailer\Exception;
|
|
use PHPMailer\PHPMailer\SMTP;
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use think\facade\Log;
|
|
|
|
class SendEmail
|
|
{
|
|
|
|
private $phpEmail;
|
|
public function __construct()
|
|
{
|
|
$this->phpEmail = new PHPMailer();
|
|
$this->phpEmail->SMTPDebug = SMTP::DEBUG_OFF; // 关闭调试功能
|
|
$this->phpEmail->CharSet = 'UTF-8';
|
|
$this->phpEmail->isSMTP(); // 使用SMTP 协议
|
|
$this->phpEmail->SMTPAuth = true; // 启用SMTP身份验证
|
|
$this->phpEmail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // 推荐使用tls加密协议,tls协议端口是587; ssl加密协议已经过时,存在严重安全漏洞(如 POODLE 攻击), ssl协议端口是465
|
|
$this->phpEmail->Host = env('EMAIL.HOST'); // SMTP 服务器地址
|
|
$this->phpEmail->Port = env('EMAIL.PORT'); // SMTP 端口
|
|
$this->phpEmail->Username = env('EMAIL.USERNAME'); // SMTP 用户名
|
|
$this->phpEmail->Password = env('EMAIL.PASSWORD'); // SMTP 密码
|
|
$this->phpEmail->From = env('EMAIL.FROM'); // 发件人信息
|
|
// $this->phpEmail->FromName = env('EMAIL.FROM'); // 发件人名称
|
|
}
|
|
|
|
/**
|
|
* @desc 发送邮箱服务
|
|
* @param $toAddress // 收件地址
|
|
* @param $title // 标题
|
|
* @param $content // 内容
|
|
* @return bool
|
|
*/
|
|
public function sendEmail($toAddress, $title, $content): bool
|
|
{
|
|
try {
|
|
// $this->phpEmail->isHTML(true);
|
|
$this->phpEmail->addAddress($toAddress);
|
|
$this->phpEmail->Subject = $title;
|
|
$this->phpEmail->Body = $content;
|
|
$status = $this->phpEmail->send();
|
|
if ($status) {
|
|
return true;
|
|
}
|
|
Log::info("---发送邮件失败--- status = ". $status . ' | error = '. $this->phpEmail->ErrorInfo);
|
|
return false;
|
|
}catch (\Exception $exception) {
|
|
Log::info("---发送邮件异常--- msg = ".$exception->getMessage());
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|