p2 project
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

5 months ago
<?php
namespace app\utility;
use PHPMailer\PHPMailer\Exception;
4 months ago
use PHPMailer\PHPMailer\SMTP;
5 months ago
use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Log;
5 months ago
class SendEmail
{
private $phpEmail;
public function __construct()
{
$this->phpEmail = new PHPMailer();
4 months ago
$this->phpEmail->SMTPDebug = SMTP::DEBUG_OFF; // 关闭调试功能
5 months ago
$this->phpEmail->CharSet = 'UTF-8';
4 months ago
$this->phpEmail->isSMTP(); // 使用SMTP 协议
$this->phpEmail->SMTPAuth = true; // 启用SMTP身份验证
$this->phpEmail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // 推荐使用tls加密协议,tls协议端口是587; ssl加密协议已经过时,存在严重安全漏洞(如 POODLE 攻击), ssl协议端口是465
4 months ago
$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'); // 发件人名称
5 months ago
}
/**
* @desc 发送邮箱服务
* @param $toAddress // 收件地址
* @param $title // 标题
* @param $content // 内容
* @return bool
*/
public function sendEmail($toAddress, $title, $content): bool
{
try {
4 months ago
// $this->phpEmail->isHTML(true);
5 months ago
$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);
5 months ago
return false;
}catch (\Exception $exception) {
Log::info("---发送邮件异常--- msg = ".$exception->getMessage());
5 months ago
return false;
}
}
}