fetchAll( "SELECT `key`, `value` FROM {$db->table('configs')} WHERE `key` LIKE 'smtp_%'" ); $config = []; foreach ($rows as $row) { $config[$row['key']] = $row['value']; } $this->success($config); } /** * 保存 SMTP 配置 * POST /api/system/smtp-config */ public function saveSmtpConfig(): void { $input = $this->getJsonInput(); $db = Database::getInstance(); $keys = ['smtp_host', 'smtp_port', 'smtp_user', 'smtp_pass', 'smtp_encryption', 'smtp_from_email', 'smtp_from_name', 'smtp_enabled', 'smtp_expire_days']; foreach ($keys as $key) { if (isset($input[$key])) { $value = is_array($input[$key]) ? json_encode($input[$key]) : (string) $input[$key]; $exists = $db->fetchColumn("SELECT COUNT(*) FROM {$db->table('configs')} WHERE `key` = ?", [$key]); if ($exists) { $db->execute("UPDATE {$db->table('configs')} SET `value` = ? WHERE `key` = ?", [$value, $key]); } else { $db->execute("INSERT INTO {$db->table('configs')} (`key`, `value`, `description`) VALUES (?, ?, 'SMTP邮件配置')", [$key, $value]); } } } $this->success(null, 'SMTP配置保存成功'); } /** * 发送测试邮件 * POST /api/system/test-smtp * Body: { test_email } */ public function testSmtp(): void { $input = $this->getJsonInput(); $testEmail = trim($input['test_email'] ?? ''); if (empty($testEmail) || !filter_var($testEmail, FILTER_VALIDATE_EMAIL)) { $this->error('请输入有效的测试邮箱地址'); } $config = $this->getSmtpSettings(); if (!$config['smtp_enabled']) { $this->error('SMTP 未启用'); } $result = $this->sendMail( $testEmail, 'CardAuth 测试邮件', '

CardAuth 邮件配置测试

如果您收到此邮件,说明 SMTP 配置正确。

发送时间:' . date('Y-m-d H:i:s') . '

' ); if ($result === true) { $this->success(null, '测试邮件发送成功,请检查收件箱'); } else { $this->error('发送失败:' . $result); } } /** * 检查即将过期的授权并发送邮件通知 * POST /api/system/notify-expiring * 也可通过 GET 由 cron 调用 */ public function notifyExpiring(): void { $config = $this->getSmtpSettings(); if (!$config['smtp_enabled']) { $this->error('SMTP 未启用,无法发送通知'); } $db = Database::getInstance(); $days = (int) ($config['smtp_expire_days'] ?? 7); if ($days < 1) $days = 7; // 查找即将过期的活跃授权 $expiring = $db->fetchAll( "SELECT a.id, a.bot_qq, a.contact_qq, a.contact_name, a.project_name, a.duration_days, a.expire_time, a.authorized_at, DATEDIFF(a.expire_time, NOW()) AS days_left FROM {$db->table('authorizations')} a WHERE a.status = 'active' AND a.expire_time IS NOT NULL AND a.expire_time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL ? DAY) AND a.contact_qq IS NOT NULL AND a.contact_qq != '' ORDER BY a.expire_time ASC", [$days] ); if (empty($expiring)) { $this->success(['sent' => 0, 'message' => '没有即将过期的授权']); return; } $sent = 0; $failed = 0; $sentUsers = []; // 去重:同一 contact_qq 只发一封汇总邮件 foreach ($expiring as $auth) { $email = $auth['contact_qq'] . '@qq.com'; if (in_array($email, $sentUsers)) continue; $sentUsers[] = $email; // 汇总该用户的即将过期授权 $userAuths = array_filter($expiring, fn($a) => $a['contact_qq'] === $auth['contact_qq']); $rows = ''; foreach ($userAuths as $a) { $rows .= " #{$a['id']} {$a['project_name']} {$a['bot_qq']} 还剩 {$a['days_left']} 天 {$a['expire_time']} "; } $html = "

授权即将过期提醒

您好,以下授权即将在 {$days} 天内过期,请及时续费:

{$rows}
授权ID 项目 机器人QQ 剩余时间 过期时间

此邮件由 CardAuth 授权系统自动发送,无需回复。

"; $result = $this->sendMail($email, '【CardAuth】授权即将过期提醒', $html); if ($result === true) { $sent++; } else { $failed++; } } $this->logAction('notify_expiring', 'authorization', 0, [ 'sent' => $sent, 'failed' => $failed, 'total' => count($expiring), ]); $this->success([ 'sent' => $sent, 'failed' => $failed, 'total' => count($expiring), ], "发送完成:成功 {$sent} 封,失败 {$failed} 封"); } /** * 获取 SMTP 配置数组 */ private function getSmtpSettings(): array { $db = Database::getInstance(); $rows = $db->fetchAll("SELECT `key`, `value` FROM {$db->table('configs')} WHERE `key` LIKE 'smtp_%'"); $config = [ 'smtp_host' => '', 'smtp_port' => '465', 'smtp_user' => '', 'smtp_pass' => '', 'smtp_encryption' => 'ssl', 'smtp_from_email' => '', 'smtp_from_name' => 'CardAuth', 'smtp_enabled' => '0', 'smtp_expire_days' => '7', ]; foreach ($rows as $row) { $config[$row['key']] = $row['value']; } return $config; } /** * PHP mail 方式发送邮件(兼容大多数 SMTP 配置) */ private function sendMail(string $to, string $subject, string $html): bool|string { $config = $this->getSmtpSettings(); $host = $config['smtp_host']; $port = (int) $config['smtp_port']; $user = $config['smtp_user']; $pass = $config['smtp_pass']; $encryption = $config['smtp_encryption']; // ssl | tls $fromEmail = $config['smtp_from_email'] ?: $user; $fromName = $config['smtp_from_name'] ?: 'CardAuth'; if (empty($host) || empty($user) || empty($pass)) { return 'SMTP 配置不完整'; } $boundary = md5(uniqid(time())); $headers = "MIME-Version: 1.0\r\n"; $headers .= "From: =?UTF-8?B?" . base64_encode($fromName) . "?= <{$fromEmail}>\r\n"; $headers .= "Reply-To: {$fromEmail}\r\n"; $headers .= "Content-Type: text/html; charset=utf-8\r\n"; $headers .= "X-Mailer: CardAuth\r\n"; $message = $html; // 使用 socket 直连 SMTP 发送(不依赖 PHPMailer) $errno = 0; $errstr = ''; $url = ($encryption === 'ssl') ? "ssl://{$host}" : $host; $socket = @fsockopen($url, $port, $errno, $errstr, 30); if (!$socket) { return "连接 SMTP 服务器失败: {$errstr} ({$errno})"; } $this->smtpRead($socket); $this->smtpCommand($socket, "EHLO CardAuth"); $this->smtpCommand($socket, "AUTH LOGIN"); $this->smtpCommand($socket, base64_encode($user)); $this->smtpCommand($socket, base64_encode($pass)); $this->smtpCommand($socket, "MAIL FROM:<{$fromEmail}>"); $this->smtpCommand($socket, "RCPT TO:<{$to}>"); $this->smtpCommand($socket, "DATA"); $subjectHeader = "=?UTF-8?B?" . base64_encode($subject) . "?="; $message = preg_replace("/^\./m", "..", $message); $data = $headers . "To: {$to}\r\n" . "Subject: {$subjectHeader}\r\n" . "Date: " . date('r') . "\r\n" . "\r\n" . $message . "\r\n."; $this->smtpCommand($socket, $data); $this->smtpCommand($socket, "QUIT"); fclose($socket); return true; } private function smtpCommand($socket, string $command): string { fwrite($socket, $command . "\r\n"); return $this->smtpRead($socket); } private function smtpRead($socket): string { $response = ''; while ($line = fgets($socket, 512)) { $response .= $line; if (isset($line[3]) && $line[3] === ' ') break; } return $response; } }