2018-11-28 07:59:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-03-08 06:57:41 +00:00
|
|
|
|
|
|
|
"github.com/knadh/listmonk/internal/manager"
|
2018-11-28 07:59:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-12-01 12:18:36 +00:00
|
|
|
notifTplImport = "import-status"
|
|
|
|
notifTplCampaign = "campaign-status"
|
|
|
|
notifSubscriberOptin = "subscriber-optin"
|
|
|
|
notifSubscriberData = "subscriber-data"
|
2018-11-28 07:59:57 +00:00
|
|
|
)
|
|
|
|
|
2019-12-01 12:18:36 +00:00
|
|
|
// notifData represents params commonly used across different notification
|
|
|
|
// templates.
|
|
|
|
type notifData struct {
|
|
|
|
RootURL string
|
|
|
|
LogoURL string
|
|
|
|
}
|
2018-11-28 07:59:57 +00:00
|
|
|
|
2019-12-01 12:18:36 +00:00
|
|
|
// sendNotification sends out an e-mail notification to admins.
|
2020-03-08 06:57:41 +00:00
|
|
|
func (app *App) sendNotification(toEmails []string, subject, tplName string, data interface{}) error {
|
2018-11-28 07:59:57 +00:00
|
|
|
var b bytes.Buffer
|
2020-03-07 18:33:22 +00: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 07:59:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-08 06:57:41 +00:00
|
|
|
err := app.manager.PushMessage(manager.Message{
|
|
|
|
From: app.constants.FromEmail,
|
|
|
|
To: toEmails,
|
|
|
|
Subject: subject,
|
|
|
|
Body: b.Bytes(),
|
|
|
|
Messenger: "email",
|
|
|
|
})
|
2018-11-28 07:59:57 +00:00
|
|
|
if err != nil {
|
2020-03-07 18:33:22 +00:00
|
|
|
app.log.Printf("error sending admin notification (%s): %v", subject, err)
|
2018-11-28 07:59:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|