notifications.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "bytes"
  4. "github.com/knadh/listmonk/internal/manager"
  5. )
  6. const (
  7. notifTplImport = "import-status"
  8. notifTplCampaign = "campaign-status"
  9. notifSubscriberOptin = "subscriber-optin"
  10. notifSubscriberData = "subscriber-data"
  11. )
  12. // notifData represents params commonly used across different notification
  13. // templates.
  14. type notifData struct {
  15. RootURL string
  16. LogoURL string
  17. }
  18. // sendNotification sends out an e-mail notification to admins.
  19. func (app *App) sendNotification(toEmails []string, subject, tplName string, data interface{}) error {
  20. if len(toEmails) == 0 {
  21. return nil
  22. }
  23. var b bytes.Buffer
  24. if err := app.notifTpls.ExecuteTemplate(&b, tplName, data); err != nil {
  25. app.log.Printf("error compiling notification template '%s': %v", tplName, err)
  26. return err
  27. }
  28. m := manager.Message{}
  29. m.From = app.constants.FromEmail
  30. m.To = toEmails
  31. m.Subject = subject
  32. m.Body = b.Bytes()
  33. m.Messenger = emailMsgr
  34. if err := app.manager.PushMessage(m); err != nil {
  35. app.log.Printf("error sending admin notification (%s): %v", subject, err)
  36. return err
  37. }
  38. return nil
  39. }