2018-11-28 15:59:57 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-03-08 14:57:41 +08:00
|
|
|
|
|
|
|
"github.com/knadh/listmonk/internal/manager"
|
2018-11-28 15:59:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-12-01 20:18:36 +08:00
|
|
|
notifTplImport = "import-status"
|
|
|
|
notifTplCampaign = "campaign-status"
|
|
|
|
notifSubscriberOptin = "subscriber-optin"
|
|
|
|
notifSubscriberData = "subscriber-data"
|
2018-11-28 15:59:57 +08:00
|
|
|
)
|
|
|
|
|
2019-12-01 20:18:36 +08:00
|
|
|
// notifData represents params commonly used across different notification
|
|
|
|
// templates.
|
|
|
|
type notifData struct {
|
|
|
|
RootURL string
|
|
|
|
LogoURL string
|
|
|
|
}
|
2018-11-28 15:59:57 +08:00
|
|
|
|
2019-12-01 20:18:36 +08:00
|
|
|
// sendNotification sends out an e-mail notification to admins.
|
2020-03-08 14:57:41 +08:00
|
|
|
func (app *App) sendNotification(toEmails []string, subject, tplName string, data interface{}) error {
|
2021-09-24 21:58:32 +08:00
|
|
|
if len(toEmails) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-28 15:59:57 +08:00
|
|
|
var b bytes.Buffer
|
2021-10-28 22:39:06 +08:00
|
|
|
if err := app.notifTpls.tpls.ExecuteTemplate(&b, tplName, data); err != nil {
|
2020-03-08 02:33:22 +08:00
|
|
|
app.log.Printf("error compiling notification template '%s': %v", tplName, err)
|
2018-11-28 15:59:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-20 19:01:24 +08:00
|
|
|
m := manager.Message{}
|
2021-10-28 22:39:06 +08:00
|
|
|
m.ContentType = app.notifTpls.contentType
|
2020-09-20 19:01:24 +08:00
|
|
|
m.From = app.constants.FromEmail
|
|
|
|
m.To = toEmails
|
|
|
|
m.Subject = subject
|
|
|
|
m.Body = b.Bytes()
|
|
|
|
m.Messenger = emailMsgr
|
|
|
|
if err := app.manager.PushMessage(m); err != nil {
|
2020-03-08 02:33:22 +08:00
|
|
|
app.log.Printf("error sending admin notification (%s): %v", subject, err)
|
2018-11-28 15:59:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|