From 267604fc8622ffec56060ee9037c32192546e51b Mon Sep 17 00:00:00 2001 From: XiaoLFeng Date: Tue, 2 May 2023 17:16:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B1=BB=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Class/Key.php | 48 ++++++++++++++++ Class/Mailer/SendMail.php | 112 ++++++++++++++++++++++++++++++++++++++ Class/Normal.php | 33 +++++++---- Class/Sql.php | 26 +++++++++ 4 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 Class/Key.php create mode 100644 Class/Mailer/SendMail.php diff --git a/Class/Key.php b/Class/Key.php new file mode 100644 index 0000000..6baacdc --- /dev/null +++ b/Class/Key.php @@ -0,0 +1,48 @@ +ConfigData = json_decode($Array_ConfigData, JSON_UNESCAPED_UNICODE)["Smtp"]; + fclose($FileData); + // 参数赋予 + self::$ExpDate = $Array_ConfigData["Mail"]['ExpDate']; + + // 类导入 + $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 发送邮件类型 + * @return bool 邮件发送成功返回 true 否则返回 false + */ + public function PostMail(string $EmailReceiver, int $EmailType): bool + { + self::$EmailType = $EmailType; + self::$EmailReceiver = $EmailReceiver; + // 尝试邮件发送 + try { + // 服务器配置 + $this->Mail->CharSet = "UTF-8"; + $this->Mail->SMTPDebug = 0; + $this->Mail->isSMTP(); + $this->Mail->Host = $this->ConfigData['Host']; + $this->Mail->SMTPAuth = $this->ConfigData['SmtpAuth']; + $this->Mail->Username = $this->ConfigData['Username']; + $this->Mail->Password = $this->ConfigData['Password']; + $this->Mail->SMTPSecure = $this->SSLCheck('Secure'); + $this->Mail->Port = $this->SSLCheck('Port'); + $this->Mail->setFrom($this->ConfigData['User'], $this->ConfigData['Name']); + $this->Mail->addAddress($EmailReceiver); + + // 发件编写 + if ($EmailType == 1) $this->EmailRegister(); + else if ($EmailType == 2) $this->EmailLogin(); + + $this->Mail->send(); + return true; + } catch (Exception $e) { + //echo '邮件发送失败:', $this->Mail->ErrorInfo; + return false; + } + } + + private function EmailRegister(): void + { + $this->Mail->Subject = $this->ConfigData['Name'] . ' - 站点注册'; // 邮箱标题 + $this->Mail->Body = MailTemplate::Templates($Input_Code); + } + + private function EmailLogin() + { + $this->Mail->Subject = $this->ConfigData['Name'] . ' - 邮箱登录验证码'; // 邮箱标题 + $this->Mail->Body = MailTemplate::Templates($Input_Code); + } +} \ No newline at end of file diff --git a/Class/Normal.php b/Class/Normal.php index 8237c94..9f9059d 100644 --- a/Class/Normal.php +++ b/Class/Normal.php @@ -16,18 +16,31 @@ class Normal */ public static function Output(int $gType, array $OtherArray = null) { - $Json_Data = [ - 'output' => self::OutputMessage($gType, 0), - 'code' => self::OutputMessage($gType, 1), - 'data' => [ - 'message' => self::OutputMessage($gType, 2), - ], - ]; - if (!empty($OtherArray)) { - $Json_Data['data']['data'] = $OtherArray; + if (self::OutputMessage($gType, 1) != null) { + $Json_Data = [ + 'output' => self::OutputMessage($gType, 0), + 'code' => self::OutputMessage($gType, 1), + 'data' => [ + 'statusCode' => $gType, + 'message' => self::OutputMessage($gType, 2), + ], + ]; + if (!empty($OtherArray)) { + $Json_Data['data']['data'] = $OtherArray; + } + header(self::HttpStatusCode(self::OutputMessage($gType, 1))); + } else { + $Json_Data = [ + 'output' => 'DevelopError', + 'code' => 0, + 'data' => [ + 'statusCode' => 0, + 'message' => "开发错误,请查阅", + ], + ]; + header(self::HttpStatusCode(502)); } // Json 输出 - header(self::HttpStatusCode(self::OutputMessage($gType, 1))); echo json_encode($Json_Data, JSON_UNESCAPED_UNICODE); } diff --git a/Class/Sql.php b/Class/Sql.php index c5113f7..ae9df16 100644 --- a/Class/Sql.php +++ b/Class/Sql.php @@ -60,4 +60,30 @@ public static function INSERT(string $Mysql_Query): bool else return false; } + + /** + * MySQL更新库 + * @param string $Mysql_Query + * @return bool + */ + public static function UPDATE(string $Mysql_Query): bool + { + if (preg_match('/^UPDATE/', $Mysql_Query)) + return mysqli_query(self::MySqlConn(), $Mysql_Query); + else + return false; + } + + /** + * MySQL删除库 + * @param string $Mysql_Query + * @return bool + */ + public static function DELETE(string $Mysql_Query): bool + { + if (preg_match('/^DELETE/', $Mysql_Query)) + return mysqli_query(self::MySqlConn(), $Mysql_Query); + else + return false; + } } \ No newline at end of file