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 发送邮件类型 * @param string $OtherPush 其他备注内容,例如激活码 * @return bool 邮件发送成功返回 true 否则返回 false */ public function PostMail(string $EmailReceiver, int $EmailType, string $OtherPush = null): bool { self::$EmailType = $EmailType; self::$EmailReceiver = $EmailReceiver; // 尝试邮件发送 try { // 服务器配置 $this->Mail->CharSet = "UTF-8"; $this->Mail->SMTPDebug = 0; $this->Mail->isSMTP(); $this->Mail->Host = self::$ConfigData['Host']; $this->Mail->SMTPAuth = self::$ConfigData['SmtpAuth']; $this->Mail->Username = self::$ConfigData['Username']; $this->Mail->Password = self::$ConfigData['Password']; $this->Mail->SMTPSecure = $this->SSLCheck('Secure'); $this->Mail->Port = $this->SSLCheck('Port'); $this->Mail->setFrom(self::$ConfigData['User'], self::$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->$this->Mail->ErrorInfo; return false; } } protected function EmailRegister(): void { $this->Mail->Subject = self::$ConfigData['Name'] . ' - 站点注册'; // 邮箱标题 $this->Mail->Body = MailTemplate::Templates(); } protected function EmailLogin(): void { $this->Mail->Subject = self::$ConfigData['Name'] . ' - 邮箱登录验证码'; // 邮箱标题 $this->Mail->Body = MailTemplate::Templates(); } }