config = $config; } /** * 发送定时任务通知 * @return bool|string 成功返回 true,失败返回错误信息 */ public function notifySchedule($actionType, $account, $description = "") { if (($this->config['enable_schedule_email'] ?? '0') !== '1') return true; // 未开启也视为“处理完毕” $title = "定时任务: " . $actionType; $maskedKey = substr($account['access_key_id'], 0, 7) . '***'; $details = [ ['label' => '账号 ID', 'value' => $maskedKey], ['label' => '执行动作', 'value' => $actionType, 'highlight' => true], ['label' => '执行时间', 'value' => date('Y-m-d H:i:s')], ['label' => '详情说明', 'value' => $description ?: '根据预设时间表自动执行。'] ]; $html = $this->renderEmailTemplate($title, "您的实例已执行{$actionType}操作", $details, 'info'); return $this->sendMail($this->config['notify_email'] ?? '', '', "CDT通知 - {$actionType}", $html); } /** * 发送流量告警 * @return bool|string */ public function sendTrafficWarning($accessKeyId, $traffic, $percentage, $statusText, $threshold) { if (empty($this->config['notify_email'])) return false; $title = "流量告警 - " . $statusText; $details = [ ['label' => '账号 ID', 'value' => substr($accessKeyId, 0, 7) . '***'], ['label' => '当前流量', 'value' => $traffic . ' GB'], ['label' => '使用率', 'value' => $percentage . '%', 'highlight' => true], ['label' => '设定阈值', 'value' => $threshold . '%'], ['label' => '当前状态', 'value' => $statusText] ]; $html = $this->renderEmailTemplate($title, "检测到流量异常或达到阈值", $details, 'warning'); return $this->sendMail($this->config['notify_email'] ?? '', '', 'CDT流量熔断告警', $html); } public function sendTestEmail($to) { $details = [ ['label' => '测试结果', 'value' => '成功 (Success)'], ['label' => '发送时间', 'value' => date('Y-m-d H:i:s')], ['label' => '服务器', 'value' => $_SERVER['SERVER_NAME'] ?? 'localhost'] ]; $html = $this->renderEmailTemplate("测试邮件", "SMTP 配置验证成功", $details, 'success'); return $this->sendMail($to, 'Admin', 'CDT Monitor Test', $html); } private function renderEmailTemplate($title, $summary, $details, $type = 'info') { $color = '#007AFF'; if ($type === 'warning') $color = '#FF3B30'; if ($type === 'success') $color = '#34C759'; $rows = ''; foreach ($details as $item) { $valColor = isset($item['highlight']) && $item['highlight'] ? $color : '#1C1C1E'; $rows .= " {$item['label']} {$item['value']} "; } return "
CDT MONITOR

{$title}

{$summary}

{$rows}
© " . date('Y') . " CDT Monitor
"; } private function sendMail($to, $name, $subject, $body) { $mail = new PHPMailer(); $mail->CharSet = 'UTF-8'; $mail->IsSMTP(); $mail->SMTPDebug = 0; $mail->SMTPAuth = true; $secure = $this->config['notify_secure'] ?? 'ssl'; if (!empty($secure)) { $mail->SMTPSecure = $secure; } else { $mail->SMTPSecure = ''; $mail->SMTPAutoTLS = false; } $mail->Host = $this->config['notify_host'] ?? ''; $mail->Port = $this->config['notify_port'] ?? 465; $mail->Username = $this->config['notify_username'] ?? ''; $mail->Password = $this->config['notify_password'] ?? ''; $mail->SetFrom($mail->Username, '阿里云CDT监控'); $mail->Subject = $subject; $mail->MsgHTML($body); $mail->AddAddress($to, $name); // 修改:返回 true 或 错误信息字符串 if ($mail->Send()) { return true; } else { return $mail->ErrorInfo; } } }