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 测试邮件', '
如果您收到此邮件,说明 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 .= "您好,以下授权即将在 {$days} 天内过期,请及时续费:
| 授权ID | 项目 | 机器人QQ | 剩余时间 | 过期时间 |
|---|
此邮件由 CardAuth 授权系统自动发送,无需回复。