73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Author: ywxapp<admin@ywxapp.cn>
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace ywxapp\event;
|
||
|
||
use PHPMailer\PHPMailer\PHPMailer;
|
||
use PHPMailer\PHPMailer\Exception;
|
||
use ywxapp\library\Result;
|
||
/**
|
||
* Email 类
|
||
*
|
||
* @author ywxapp <admin@ywxapp.cn>
|
||
*/
|
||
class Email
|
||
{
|
||
protected $mail;
|
||
|
||
|
||
public function __construct()
|
||
{
|
||
$this->mail = new PHPMailer(true);
|
||
|
||
// 配置 SMTP 参数
|
||
$this->mail->isSMTP(); // 使用 SMTP
|
||
$this->mail->Host = 'mail.xixingwl.cn'; // SMTP 服务器地址
|
||
$this->mail->SMTPAuth = true; // 开启 SMTP 认证
|
||
$this->mail->Username = 'test@xixingwl.cn'; // SMTP 用户名
|
||
$this->mail->Password = 'Y20mylove.'; // SMTP 密码
|
||
$this->mail->SMTPSecure = 'tls'; // 加密协议(tls 或 ssl)
|
||
$this->mail->Port = 587; // SMTP 端口
|
||
$this->mail->SMTPOptions = [
|
||
'ssl' => [
|
||
'verify_peer' => false,
|
||
'verify_peer_name' => false,
|
||
'allow_self_signed' => true
|
||
]
|
||
];
|
||
}
|
||
|
||
|
||
public function sendEmail($to, $subject, $body)
|
||
{
|
||
try {
|
||
// 设置收件人
|
||
$this->mail->setFrom('test@xixingwl.cn', 'From Name');
|
||
$this->mail->addAddress($to); // 添加收件人
|
||
// 设置邮件内容
|
||
$this->mail->isHTML(true); // 设置邮件格式为 HTML
|
||
$this->mail->Subject = $subject;
|
||
$this->mail->Body = $body;
|
||
// 发送邮件
|
||
$this->mail->send();
|
||
return true;
|
||
} catch (Exception $e) {
|
||
Result::instance()->error(message: "Message could not be sent. Mailer Error: {$this->mail->ErrorInfo}");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public function handle($mail)
|
||
{
|
||
echo "1244";
|
||
$subject = '欢迎注册';
|
||
$body = "亲爱的 {$mail['email']},感谢您注册我们的网站!";
|
||
return $this->sendEmail($mail['email'], $subject, $body);
|
||
}
|
||
} |