2018-11-28 08:59:57 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-12-01 13:18:36 +01:00
|
|
|
"html/template"
|
|
|
|
|
|
|
|
"github.com/knadh/stuffbin"
|
2018-11-28 08:59:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-12-01 13:18:36 +01:00
|
|
|
notifTplImport = "import-status"
|
|
|
|
notifTplCampaign = "campaign-status"
|
|
|
|
notifSubscriberOptin = "subscriber-optin"
|
|
|
|
notifSubscriberData = "subscriber-data"
|
2018-11-28 08:59:57 +01:00
|
|
|
)
|
|
|
|
|
2019-12-01 13:18:36 +01:00
|
|
|
// notifData represents params commonly used across different notification
|
|
|
|
// templates.
|
|
|
|
type notifData struct {
|
|
|
|
RootURL string
|
|
|
|
LogoURL string
|
|
|
|
}
|
2018-11-28 08:59:57 +01:00
|
|
|
|
2019-12-01 13:18:36 +01:00
|
|
|
// sendNotification sends out an e-mail notification to admins.
|
|
|
|
func sendNotification(toEmails []string, subject, tplName string, data interface{}, app *App) error {
|
2018-11-28 08:59:57 +01:00
|
|
|
var b bytes.Buffer
|
2019-12-01 13:18:36 +01:00
|
|
|
if err := app.NotifTpls.ExecuteTemplate(&b, tplName, data); err != nil {
|
|
|
|
app.Logger.Printf("error compiling notification template '%s': %v", tplName, err)
|
2018-11-28 08:59:57 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:18:36 +01:00
|
|
|
err := app.Messenger.Push(app.Constants.FromEmail,
|
|
|
|
toEmails,
|
2018-11-28 08:59:57 +01:00
|
|
|
subject,
|
2019-07-18 09:10:48 +02:00
|
|
|
b.Bytes(),
|
|
|
|
nil)
|
2018-11-28 08:59:57 +01:00
|
|
|
if err != nil {
|
|
|
|
app.Logger.Printf("error sending admin notification (%s): %v", subject, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-21 15:48:41 +02:00
|
|
|
|
2019-12-01 13:18:36 +01:00
|
|
|
// compileNotifTpls compiles and returns e-mail notification templates that are
|
|
|
|
// used for sending ad-hoc notifications to admins and subscribers.
|
|
|
|
func compileNotifTpls(path string, fs stuffbin.FileSystem, app *App) (*template.Template, error) {
|
|
|
|
// Register utility functions that the e-mail templates can use.
|
|
|
|
funcs := template.FuncMap{
|
|
|
|
"RootURL": func() string {
|
|
|
|
return app.Constants.RootURL
|
|
|
|
},
|
|
|
|
"LogoURL": func() string {
|
|
|
|
return app.Constants.LogoURL
|
|
|
|
}}
|
|
|
|
|
|
|
|
tpl, err := stuffbin.ParseTemplatesGlob(funcs, fs, "/email-templates/*.html")
|
2019-07-21 15:48:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:18:36 +01:00
|
|
|
return tpl, err
|
2019-07-21 15:48:41 +02:00
|
|
|
}
|