Alert Summary
HTML; if ($truncatedAlerts > 0) { $alertSummary .= << HTML; } if ($externalURL) { $externalURLSafe = htmlspecialchars($externalURL, ENT_QUOTES, 'UTF-8'); $alertSummary .= << HTML; } $alertSummary .= << HTML; // Build common labels section $labelsContent = ''; if (!empty($groupLabels) || !empty($commonLabels)) { $labelsContent .= '
Labels
'; if (!empty($groupLabels)) { $labelsContent .= '
Group Labels
'; foreach ($groupLabels as $key => $value) { $keySafe = htmlspecialchars($key, ENT_QUOTES, 'UTF-8'); $valueSafe = esc($value); $labelsContent .= << HTML; } $labelsContent .= ''; } if (!empty($commonLabels)) { $labelsContent .= '
Common Labels
'; foreach ($commonLabels as $key => $value) { $keySafe = htmlspecialchars($key, ENT_QUOTES, 'UTF-8'); $valueSafe = esc($value); $labelsContent .= << HTML; } $labelsContent .= ''; } $labelsContent .= ''; } // Build common annotations section $annotationsContent = ''; if (!empty($commonAnnotations)) { $annotationsContent .= '
Common Annotations
'; foreach ($commonAnnotations as $key => $value) { $keySafe = htmlspecialchars($key, ENT_QUOTES, 'UTF-8'); $valueSafe = esc($value); $annotationsContent .= << HTML; } $annotationsContent .= '
'; } // Build individual alerts section $alertsContent = ''; if (!empty($alerts)) { $alertsContent .= '
Alerts
'; foreach ($alerts as $index => $alert) { $alertStatus = $alert['status'] ?? 'unknown'; $alertLabels = $alert['labels'] ?? []; $alertAnnotations = $alert['annotations'] ?? []; $startsAt = $alert['startsAt'] ?? null; $endsAt = $alert['endsAt'] ?? null; $generatorURL = $alert['generatorURL'] ?? null; $fingerprint = $alert['fingerprint'] ?? null; $alertStatusSafe = htmlspecialchars($alertStatus, ENT_QUOTES, 'UTF-8'); $alertNumber = $index + 1; // Choose alert icon $alertIcon = '🔔'; $alertColor = '#666666'; if ($alertStatus === 'firing') { $alertIcon = '🔥'; $alertColor = '#dc3545'; } elseif ($alertStatus === 'resolved') { $alertIcon = '✅'; $alertColor = '#28a745'; } $alertsContent .= <<
$alertIcon Alert $alertNumber - $alertStatusSafe
HTML; // Add alert labels if (!empty($alertLabels)) { $alertsContent .= '
Labels
'; foreach ($alertLabels as $key => $value) { $keySafe = htmlspecialchars($key, ENT_QUOTES, 'UTF-8'); $valueSafe = esc($value); $alertsContent .= << HTML; } $alertsContent .= ''; } // Add alert annotations if (!empty($alertAnnotations)) { $alertsContent .= '
Annotations
'; foreach ($alertAnnotations as $key => $value) { $keySafe = htmlspecialchars($key, ENT_QUOTES, 'UTF-8'); $valueSafe = esc($value); $alertsContent .= << HTML; } $alertsContent .= ''; } // Add timing and metadata if ($startsAt) { $startsAtSafe = htmlspecialchars($startsAt, ENT_QUOTES, 'UTF-8'); $alertsContent .= << HTML; } if ($endsAt) { $endsAtSafe = htmlspecialchars($endsAt, ENT_QUOTES, 'UTF-8'); $alertsContent .= << HTML; } if ($generatorURL) { $generatorURLSafe = htmlspecialchars($generatorURL, ENT_QUOTES, 'UTF-8'); $alertsContent .= << HTML; } if ($fingerprint) { $fingerprintSafe = htmlspecialchars($fingerprint, ENT_QUOTES, 'UTF-8'); $alertsContent .= << HTML; } $alertsContent .= '
'; } $alertsContent .= ''; } $emailTitle = "$statusIcon Grafana Alert: $statusSafe"; $emailStamp = "$dateSafe"; $body = <<
📋 Request Information
HTML; $html = emailShell($emailTitle, $emailStamp, $body, "This Grafana/Prometheus alert was automatically forwarded to your email address."); // Create subject line $alertCountText = $alertCount === 1 ? '1 alert' : "$alertCount alerts"; $subject = "$statusIcon Grafana Alert: $statusSafe - $alertCountText"; return [ 'html' => $html, 'subject' => $subject ]; }