package core import ( "bytes" "errors" "html" "html/template" "net/mail" "github.com/pocketbase/pocketbase/tools/mailer" ) const systemAlertHTML = `

{{.AppName}} system alert occurred:

{{.AlertDetails}}

For more information you could explore the logs in the dashboard of your application.

` // sendSystemAlertToAllSuperusers sends a system error alert to all superusers. // // note: unexported for now until there is clarity around the planned log level alerts. func sendSystemAlertToAllSuperusers(app App, subject string, details string) error { superusers, err := app.FindAllRecords(CollectionNameSuperusers) if err != nil { return err } var alertErrors []error for _, superuser := range superusers { err := sendSystemAlert(app, superuser, subject, details) if err != nil { alertErrors = append(alertErrors, err) } } return errors.Join(alertErrors...) } // sendSystemAlert sends a system error alert to a single superuser. // // note: unexported for now until there is clarity around the planned log level alerts. func sendSystemAlert(app App, superuser *Record, subject string, details string) error { if !superuser.IsSuperuser() { return errors.New("system alerts can be sent only to superusers") } if subject == "" || details == "" { return errors.New("system alerts subject and details are required") } data := struct { AppName string AlertDetails string }{ AppName: app.Settings().Meta.AppName, AlertDetails: details, } tpl := template.New("system_alert") var parseErr error tpl, parseErr = tpl.Parse(systemAlertHTML) if parseErr != nil { return parseErr } var buff bytes.Buffer executeErr := tpl.Execute(&buff, data) if executeErr != nil { return executeErr } message := &mailer.Message{ From: mail.Address{ Name: app.Settings().Meta.SenderName, Address: app.Settings().Meta.SenderAddress, }, To: []mail.Address{{Address: superuser.Email()}}, Subject: "[" + app.Settings().Meta.AppName + " system alert] " + html.EscapeString(subject), HTML: buff.String(), } return app.NewMailClient().Send(message) }