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("no access token from Microsoft Entra") } 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, }) }}, }