Files
uptimesebi/internal/notify/email.go
Sebastian UnterschützandClaude Opus 5.5 52819e64bb
ci / image (pull_request) Successful in 4m24s
ci / build (pull_request) Successful in 5m6s
ci / lint (pull_request) Successful in 12m3s
ci / test (pull_request) Successful in 15m27s
feat(notify): the remaining notification services of Uptime Kuma
Adds 93 services, bringing the total to 109 plus webhook and email:
chat and messengers (DingTalk, Feishu, LINE, Nextcloud Talk, Threema,
WhatsApp gateways, Nostr, Home Assistant, …), push (Bark, Gorush,
ServerChan, …), SMS and calls (Twilio, Plivo, 46elks, Alibaba Cloud,
Teltonika routers, …), email APIs (SendGrid, Brevo, Resend, Microsoft
Graph, …) and incident management (Grafana OnCall, Jira Service
Management, Splunk On-Call, Squadcast, SIGNL4, …). Customers bring
their own credentials; secrets are sealed like the others.

Services pair incidents by their ID where they can resolve alerts.
Signed requests (DingTalk, Nextcloud Talk, Alibaba Cloud) and replies
that report failures with status 200 are handled. Nostr messages are
sealed and gift-wrapped (NIP-17/59) with go-nostr, but sent over our own
WebSocket connections so relays go through the network guard. Messages
now carry the monitor's name, ID and target.

The channel form groups services, the i18n test derives provider keys
from the registry, and channel types may contain digits.

Refs #92

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 17:40:19 +01:00

132 lines
5.3 KiB
Go

package notify
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"
)
// Email delivery services reached over their APIs instead of SMTP.
func emailFields(extra ...Field) []Field {
return append(extra,
text("from_email", true), text("from_name", false), text("to_email", true),
text("cc_email", false), text("bcc_email", false), text("subject", false),
)
}
func subject(cfg map[string]string, m Message) string { return or(cfg["subject"], m.Title) }
func addresses(s string, as func(string) any) []any {
var out []any
for _, a := range list(s) {
out = append(out, as(a))
}
return out
}
func emailObj(a string) any { return map[string]string{"email": a} }
var emailProviders = []Provider{
{Type: "brevo", Group: "email", Fields: emailFields(secret("api_key", true)),
send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
body := map[string]any{
"sender": map[string]any{"email": cfg["from_email"], "name": or(cfg["from_name"], "UptimeSebi")},
"to": addresses(cfg["to_email"], emailObj),
"subject": subject(cfg, m),
"textContent": m.text(),
}
if cc := addresses(cfg["cc_email"], emailObj); cc != nil {
body["cc"] = cc
}
if bcc := addresses(cfg["bcc_email"], emailObj); bcc != nil {
body["bcc"] = bcc
}
return c.json(ctx, http.MethodPost, "https://api.brevo.com/v3/smtp/email", http.Header{"api-key": {cfg["api_key"]}}, body)
}},
{Type: "resend", Group: "email", Fields: emailFields(secret("api_key", true)),
send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
body := map[string]any{
"from": or(cfg["from_name"], "UptimeSebi") + " <" + cfg["from_email"] + ">", "to": list(cfg["to_email"]),
"subject": subject(cfg, m), "text": m.text(),
}
if cc := list(cfg["cc_email"]); cc != nil {
body["cc"] = cc
}
if bcc := list(cfg["bcc_email"]); bcc != nil {
body["bcc"] = bcc
}
return c.json(ctx, http.MethodPost, "https://api.resend.com/emails", bearer(cfg["api_key"]), body)
}},
{Type: "sendgrid", Group: "email", Fields: emailFields(secret("api_key", true)),
send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
p := map[string]any{"to": addresses(cfg["to_email"], emailObj)}
if cc := addresses(cfg["cc_email"], emailObj); cc != nil {
p["cc"] = cc
}
if bcc := addresses(cfg["bcc_email"], emailObj); bcc != nil {
p["bcc"] = bcc
}
return c.json(ctx, http.MethodPost, "https://api.sendgrid.com/v3/mail/send", bearer(cfg["api_key"]), map[string]any{
"personalizations": []any{p},
"from": map[string]any{"email": cfg["from_email"], "name": or(cfg["from_name"], "UptimeSebi")},
"subject": subject(cfg, m),
"content": []map[string]any{{"type": "text/plain", "value": m.text()}},
})
}},
{Type: "turbosmtp", Group: "email", Fields: emailFields(
text("consumer_key", true), secret("consumer_secret", true),
Field{Name: "region", Kind: "select", Options: []string{"us", "eu"}, Default: "us"},
), send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
host := "api.turbo-smtp.com"
if cfg["region"] == "eu" {
host = "api.eu.turbo-smtp.com"
}
body := map[string]any{
"from": cfg["from_email"], "to": strings.Join(list(cfg["to_email"]), ","), "subject": subject(cfg, m), "content": m.text(),
}
if cc := list(cfg["cc_email"]); cc != nil {
body["cc"] = strings.Join(cc, ",")
}
if bcc := list(cfg["bcc_email"]); bcc != nil {
body["bcc"] = strings.Join(bcc, ",")
}
h := http.Header{"consumerKey": {cfg["consumer_key"]}, "consumerSecret": {cfg["consumer_secret"]}}
return c.json(ctx, http.MethodPost, "https://"+host+"/api/v2/mail/send", h, body)
}},
{Type: "microsoftgraph", Group: "email", Fields: []Field{
text("tenant_id", true), text("client_id", true), secret("client_secret", true), text("mailbox", true),
text("to_email", true), text("cc_email", false), text("bcc_email", false), text("subject", false),
}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
// An app registration with the Mail.Send application permission
// sends as the mailbox.
reply, err := c.form(ctx, "https://login.microsoftonline.com/"+url.PathEscape(cfg["tenant_id"])+"/oauth2/v2.0/token", nil, url.Values{
"client_id": {cfg["client_id"]}, "client_secret": {cfg["client_secret"]},
"scope": {"https://graph.microsoft.com/.default"}, "grant_type": {"client_credentials"},
})
if err != nil {
return err
}
var tok struct {
AccessToken string `json:"access_token"`
}
if json.Unmarshal(reply, &tok) != nil || tok.AccessToken == "" {
return errors.New("Microsoft Entra issued no access token")
}
recipients := func(s string) []any {
return addresses(s, func(a string) any { return map[string]any{"emailAddress": map[string]string{"address": a}} })
}
return c.json(ctx, http.MethodPost, "https://graph.microsoft.com/v1.0/users/"+url.PathEscape(cfg["mailbox"])+"/sendMail", bearer(tok.AccessToken),
map[string]any{
"message": map[string]any{
"subject": subject(cfg, m), "body": map[string]any{"contentType": "Text", "content": m.text()},
"toRecipients": recipients(cfg["to_email"]), "ccRecipients": recipients(cfg["cc_email"]), "bccRecipients": recipients(cfg["bcc_email"]),
},
"saveToSentItems": false,
})
}},
}