CRM/webman/app/service/MailService.php

58 lines
1.8 KiB
PHP
Raw Normal View History

2025-03-14 17:10:14 +08:00
<?php
namespace app\service;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use support\Log;
class MailService
{
protected $mailer;
public function __construct()
{
$this->mailer = new PHPMailer(true);
$this->configure();
}
protected function configure()
{
// SMTP 配置
$this->mailer->isSMTP();
$this->mailer->Host = 'smtp.qq.com';
$this->mailer->SMTPAuth = true;
$this->mailer->Username = '1696136552@qq.com'; // 替换为你的 Gmail
$this->mailer->Password = 'jhqkgjwzqqzgdcfd'; // 替换为应用专用密码
// $this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // 使用 TLS
$this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL 加密
// $this->mailer->Port = 587;
$this->mailer->Port = 465;
$this->mailer->CharSet = 'UTF-8';
}
/**
* 发送验证码邮件
* @param string $toEmail 收件邮箱
* @param string $code 验证码
* @return bool
*/
public function sendVerificationCode(string $toEmail, string $code): bool
{
try {
$this->mailer->setFrom('1696136552@qq.com', 'XinYouHe Code'); // 发件人
$this->mailer->addAddress($toEmail);
// 邮件内容
$this->mailer->isHTML(true);
$this->mailer->Subject = '您的验证码';
$this->mailer->Body = "您的CRM验证码是<strong>{$code}</strong>5分钟内有效。";
$this->mailer->send();
Log::info("邮件发送成功: {$toEmail}");
return true;
} catch (Exception $e) {
Log::error("邮件发送失败: " . $this->mailer->ErrorInfo);
return false;
}
}
}