2023-05-02 17:16:50 +08:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Copyright © 2016 - 2023 筱锋xiao_lfeng. All Rights Reserved.
|
|
|
|
* 开发开源遵循 MIT 许可,若需商用请联系开发者
|
|
|
|
* https://www.x-lf.com/
|
|
|
|
*/
|
|
|
|
|
2023-05-04 22:30:08 +08:00
|
|
|
|
2023-05-02 17:16:50 +08:00
|
|
|
namespace Mailer;
|
|
|
|
|
2023-05-04 22:30:08 +08:00
|
|
|
require_once dirname(__FILE__, 3) . "/class/Mailer/PHPMailer/PHPMailer.php";
|
|
|
|
require_once dirname(__FILE__, 3) . "/class/Mailer/PHPMailer/Exception.php";
|
|
|
|
require_once dirname(__FILE__, 3) . "/class/Mailer/PHPMailer/SMTP.php";
|
|
|
|
require_once dirname(__FILE__, 3) . "/class/Mailer/MailTemplate.php";
|
2023-05-02 17:16:50 +08:00
|
|
|
|
|
|
|
class SendMail
|
|
|
|
{
|
|
|
|
public static int $EmailType;
|
|
|
|
public static string $EmailReceiver;
|
2023-05-02 17:53:25 +08:00
|
|
|
|
2023-05-04 22:30:08 +08:00
|
|
|
private static array $ConfigData;
|
2023-05-02 17:53:25 +08:00
|
|
|
public static int $ExpTime;
|
|
|
|
public static string $WebTitle;
|
2023-05-04 22:30:08 +08:00
|
|
|
protected PHPMailer $Mail;
|
2023-05-02 17:16:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void 导入文件,无具体返回值
|
|
|
|
*/
|
2023-05-04 22:30:08 +08:00
|
|
|
public function __construct()
|
2023-05-02 17:16:50 +08:00
|
|
|
{
|
|
|
|
// 文件导入
|
|
|
|
$Array_ConfigData = null;
|
|
|
|
$FileData = fopen(dirname(__FILE__, 3) . "/setting.inc.json", 'r');
|
|
|
|
while (!feof($FileData))
|
|
|
|
$Array_ConfigData .= fgetc($FileData);
|
|
|
|
$Array_ConfigData = json_decode($Array_ConfigData, JSON_UNESCAPED_UNICODE);
|
2023-05-04 22:30:08 +08:00
|
|
|
self::$ConfigData = $Array_ConfigData["Smtp"];
|
2023-05-02 17:16:50 +08:00
|
|
|
fclose($FileData);
|
|
|
|
// 参数赋予
|
2023-05-02 17:53:25 +08:00
|
|
|
self::$ExpTime = $Array_ConfigData["Mail"]['ExpDate'];
|
|
|
|
self::$WebTitle = $Array_ConfigData["Web"]['Title'];
|
2023-05-02 17:16:50 +08:00
|
|
|
|
2023-05-04 22:30:08 +08:00
|
|
|
// 导入类
|
2023-05-02 17:16:50 +08:00
|
|
|
$this->Mail = new PHPMailer(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 检查通信协议是 HTTP 还是 HTTPS
|
|
|
|
* @param string $Smtp_Type [Port]获取端口值,[Secure]连接模式
|
|
|
|
* @return mixed|string|null
|
|
|
|
*/
|
|
|
|
private function SSLCheck(string $Smtp_Type)
|
|
|
|
{
|
|
|
|
if ($Smtp_Type == 'Port')
|
|
|
|
return $_SERVER['SERVER_PORT'] != '443' ? $this->ConfigData['Port'] : $this->ConfigData['SecurePort'];
|
|
|
|
elseif ($Smtp_Type == 'Secure')
|
|
|
|
if ($_SERVER['SERVER_PORT'] != '443')
|
|
|
|
return 'TLS';
|
|
|
|
else
|
|
|
|
return 'ssl';
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发件基础内容(调用)
|
|
|
|
* 说明:
|
|
|
|
*
|
|
|
|
* 1. [EmailType(int)] 邮件发送类型
|
|
|
|
* - [1] 站点注册邮件
|
|
|
|
* - [2] 站点邮件登录
|
|
|
|
* @param string $EmailReceiver 邮件接收方(邮箱地址)
|
|
|
|
* @param int $EmailType 发送邮件类型
|
2023-05-02 17:53:25 +08:00
|
|
|
* @param string $OtherPush 其他备注内容,例如激活码
|
2023-05-02 17:16:50 +08:00
|
|
|
* @return bool 邮件发送成功返回 true 否则返回 false
|
|
|
|
*/
|
2023-05-02 17:53:25 +08:00
|
|
|
public function PostMail(string $EmailReceiver, int $EmailType, string $OtherPush = null): bool
|
2023-05-02 17:16:50 +08:00
|
|
|
{
|
|
|
|
self::$EmailType = $EmailType;
|
|
|
|
self::$EmailReceiver = $EmailReceiver;
|
2023-05-04 22:30:08 +08:00
|
|
|
|
2023-05-02 17:16:50 +08:00
|
|
|
// 尝试邮件发送
|
|
|
|
try {
|
|
|
|
// 服务器配置
|
|
|
|
$this->Mail->CharSet = "UTF-8";
|
|
|
|
$this->Mail->SMTPDebug = 0;
|
|
|
|
$this->Mail->isSMTP();
|
2023-05-04 22:30:08 +08:00
|
|
|
$this->Mail->Host = self::$ConfigData['Host'];
|
|
|
|
$this->Mail->SMTPAuth = self::$ConfigData['SmtpAuth'];
|
|
|
|
$this->Mail->Username = self::$ConfigData['Username'];
|
|
|
|
$this->Mail->Password = self::$ConfigData['Password'];
|
2023-05-02 17:16:50 +08:00
|
|
|
$this->Mail->SMTPSecure = $this->SSLCheck('Secure');
|
|
|
|
$this->Mail->Port = $this->SSLCheck('Port');
|
2023-05-04 22:30:08 +08:00
|
|
|
$this->Mail->setFrom(self::$ConfigData['User'], self::$ConfigData['Name']);
|
2023-05-02 17:16:50 +08:00
|
|
|
$this->Mail->addAddress($EmailReceiver);
|
|
|
|
|
|
|
|
// 发件编写
|
2023-05-04 22:30:08 +08:00
|
|
|
if ($EmailType == 1) $this->EmailRegister();
|
|
|
|
else if ($EmailType == 2) $this->EmailLogin();
|
2023-05-02 17:16:50 +08:00
|
|
|
|
|
|
|
$this->Mail->send();
|
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
2023-05-04 22:30:08 +08:00
|
|
|
//echo '邮件发送失败:', $this->$this->Mail->ErrorInfo;
|
2023-05-02 17:16:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 22:30:08 +08:00
|
|
|
protected function EmailRegister(): void
|
2023-05-02 17:16:50 +08:00
|
|
|
{
|
2023-05-04 22:30:08 +08:00
|
|
|
$this->Mail->Subject = self::$ConfigData['Name'] . ' - 站点注册'; // 邮箱标题
|
|
|
|
$this->Mail->Body = MailTemplate::Templates();
|
2023-05-02 17:16:50 +08:00
|
|
|
}
|
|
|
|
|
2023-05-04 22:30:08 +08:00
|
|
|
protected function EmailLogin(): void
|
2023-05-02 17:16:50 +08:00
|
|
|
{
|
2023-05-04 22:30:08 +08:00
|
|
|
$this->Mail->Subject = self::$ConfigData['Name'] . ' - 邮箱登录验证码'; // 邮箱标题
|
|
|
|
$this->Mail->Body = MailTemplate::Templates();
|
2023-05-02 17:16:50 +08:00
|
|
|
}
|
|
|
|
}
|