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 {
|
2018-11-28 15:59:57 +08:00
|
|
|
var b bytes.Buffer
|
2020-03-08 02:33:22 +08:00
|
|
|
if err := app.notifTpls.ExecuteTemplate(&b, tplName, data); err != nil {
|
|
|
|
app.log.Printf("error compiling notification template '%s': %v", tplName, err)
|
2018-11-28 15:59:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-08 14:57:41 +08:00
|
|
|
err := app.manager.PushMessage(manager.Message{
|
|
|
|
From: app.constants.FromEmail,
|
|
|
|
To: toEmails,
|
|
|
|
Subject: subject,
|
|
|
|
Body: b.Bytes(),
|
|
|
|
Messenger: "email",
|
|
|
|
})
|
2018-11-28 15:59:57 +08:00
|
|
|
if 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
|
|
|
|
}
|