This commit is contained in:
筱锋xiao_lfeng 2023-05-04 22:30:08 +08:00
parent e639b2d6ef
commit a2d1af4549
2 changed files with 30 additions and 24 deletions

View File

@ -5,8 +5,11 @@
* https://www.x-lf.com/ * https://www.x-lf.com/
*/ */
namespace Mailer; namespace Mailer;
require_once dirname(__FILE__, 3) . "/class/Mailer/SendMail.php";
class MailTemplate class MailTemplate
{ {
private static string $GCode; private static string $GCode;
@ -14,10 +17,10 @@ class MailTemplate
/** /**
* 检查使用邮件发送模板 * 检查使用邮件发送模板
* @param string $G_code 获取后端发送来的验证码用于发送 * @param string|null $G_code 获取后端发送来的验证码用于发送
* @return string|null 需要返回值返回HTML信息给邮箱模板用于发送 * @return string|null 需要返回值返回HTML信息给邮箱模板用于发送
*/ */
public static function Templates(string $G_code): ?string public static function Templates(string $G_code = null): ?string
{ {
// 赋值给全局 // 赋值给全局
self::$GCode = $G_code; self::$GCode = $G_code;

View File

@ -5,26 +5,28 @@
* https://www.x-lf.com/ * https://www.x-lf.com/
*/ */
namespace Mailer; namespace Mailer;
use Mailer\PHPMailer\Exception; require_once dirname(__FILE__, 3) . "/class/Mailer/PHPMailer/PHPMailer.php";
use Mailer\PHPMailer\PHPMailer; 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";
class SendMail class SendMail
{ {
public static int $EmailType; public static int $EmailType;
public static string $EmailReceiver; public static string $EmailReceiver;
private array $ConfigData; private static array $ConfigData;
public static int $ExpTime; public static int $ExpTime;
public static string $WebTitle; public static string $WebTitle;
protected PHPMailer $Mail;
private PHPMailer $Mail;
/** /**
* @return void 导入文件,无具体返回值 * @return void 导入文件,无具体返回值
*/ */
protected function __consort() public function __construct()
{ {
// 文件导入 // 文件导入
$Array_ConfigData = null; $Array_ConfigData = null;
@ -32,13 +34,13 @@ protected function __consort()
while (!feof($FileData)) while (!feof($FileData))
$Array_ConfigData .= fgetc($FileData); $Array_ConfigData .= fgetc($FileData);
$Array_ConfigData = json_decode($Array_ConfigData, JSON_UNESCAPED_UNICODE); $Array_ConfigData = json_decode($Array_ConfigData, JSON_UNESCAPED_UNICODE);
$this->ConfigData = json_decode($Array_ConfigData, JSON_UNESCAPED_UNICODE)["Smtp"]; self::$ConfigData = $Array_ConfigData["Smtp"];
fclose($FileData); fclose($FileData);
// 参数赋予 // 参数赋予
self::$ExpTime = $Array_ConfigData["Mail"]['ExpDate']; self::$ExpTime = $Array_ConfigData["Mail"]['ExpDate'];
self::$WebTitle = $Array_ConfigData["Web"]['Title']; self::$WebTitle = $Array_ConfigData["Web"]['Title'];
// 导入 // 导入
$this->Mail = new PHPMailer(true); $this->Mail = new PHPMailer(true);
} }
@ -76,42 +78,43 @@ public function PostMail(string $EmailReceiver, int $EmailType, string $OtherPus
{ {
self::$EmailType = $EmailType; self::$EmailType = $EmailType;
self::$EmailReceiver = $EmailReceiver; self::$EmailReceiver = $EmailReceiver;
// 尝试邮件发送 // 尝试邮件发送
try { try {
// 服务器配置 // 服务器配置
$this->Mail->CharSet = "UTF-8"; $this->Mail->CharSet = "UTF-8";
$this->Mail->SMTPDebug = 0; $this->Mail->SMTPDebug = 0;
$this->Mail->isSMTP(); $this->Mail->isSMTP();
$this->Mail->Host = $this->ConfigData['Host']; $this->Mail->Host = self::$ConfigData['Host'];
$this->Mail->SMTPAuth = $this->ConfigData['SmtpAuth']; $this->Mail->SMTPAuth = self::$ConfigData['SmtpAuth'];
$this->Mail->Username = $this->ConfigData['Username']; $this->Mail->Username = self::$ConfigData['Username'];
$this->Mail->Password = $this->ConfigData['Password']; $this->Mail->Password = self::$ConfigData['Password'];
$this->Mail->SMTPSecure = $this->SSLCheck('Secure'); $this->Mail->SMTPSecure = $this->SSLCheck('Secure');
$this->Mail->Port = $this->SSLCheck('Port'); $this->Mail->Port = $this->SSLCheck('Port');
$this->Mail->setFrom($this->ConfigData['User'], $this->ConfigData['Name']); $this->Mail->setFrom(self::$ConfigData['User'], self::$ConfigData['Name']);
$this->Mail->addAddress($EmailReceiver); $this->Mail->addAddress($EmailReceiver);
// 发件编写 // 发件编写
if ($EmailType == 1) $this->EmailRegister($OtherPush); if ($EmailType == 1) $this->EmailRegister();
else if ($EmailType == 2) $this->EmailLogin($OtherPush); else if ($EmailType == 2) $this->EmailLogin();
$this->Mail->send(); $this->Mail->send();
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {
//echo '邮件发送失败:', $this->Mail->ErrorInfo; //echo '邮件发送失败:', $this->$this->Mail->ErrorInfo;
return false; return false;
} }
} }
private function EmailRegister(string $Input_Code): void protected function EmailRegister(): void
{ {
$this->Mail->Subject = $this->ConfigData['Name'] . ' - 站点注册'; // 邮箱标题 $this->Mail->Subject = self::$ConfigData['Name'] . ' - 站点注册'; // 邮箱标题
$this->Mail->Body = MailTemplate::Templates($Input_Code); $this->Mail->Body = MailTemplate::Templates();
} }
private function EmailLogin(string $OtherPush) protected function EmailLogin(): void
{ {
$this->Mail->Subject = $this->ConfigData['Name'] . ' - 邮箱登录验证码'; // 邮箱标题 $this->Mail->Subject = self::$ConfigData['Name'] . ' - 邮箱登录验证码'; // 邮箱标题
$this->Mail->Body = MailTemplate::Templates($Input_Code); $this->Mail->Body = MailTemplate::Templates();
} }
} }