High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
feat: add support for an alternate plain text message in notification emails (#3241)
Mike3f9177a51d19
diff --git a/cmd/init.go b/cmd/init.goindex 044ac0f9..ddaa1dfd 100644--- a/cmd/init.go+++ b/cmd/init.go@@ -20,6 +20,7 @@ import ("strconv""strings""syscall"+ txttpl "text/template""time""github.com/Masterminds/sprig/v3"@@ -792,6 +793,11 @@ func initNotifs(fs stuffbin.FileSystem, i *i18n.I18n, em *email.Emailer, u *UrlClo.Fatalf("error parsing e-mail notif templates: %v", err)}+ txtTpls, err := parseTextTemplatesGlob(initTplFuncs(i, u), fs, "/static/email-templates/*.txt")+ if err != nil {+ lo.Fatalf("error parsing e-mail notif text templates: %v", err)+ }+// Read the notification templates.html, err := fs.Read("/static/email-templates/base.html")if err != nil {@@ -814,7 +820,7 @@ func initNotifs(fs stuffbin.FileSystem, i *i18n.I18n, em *email.Emailer, u *UrlCFromEmail: ko.String("app.from_email"),SystemEmails: ko.Strings("app.notify_emails"),ContentType: contentType,- }, tpls, em, lo)+ }, tpls, txtTpls, em, lo)}// initBounceManager initializes the bounce manager that scans mailboxes and listens to webhooks@@ -1196,3 +1202,35 @@ func joinFSPaths(root string, paths []string) []string {return out}++// parseTextTemplatesGlob takes a file system, a file path pattern, and parses matching files+// into a template.Template with an optional template.FuncMap that will be applied to the+// compiled templates.+func parseTextTemplatesGlob(f txttpl.FuncMap, fs stuffbin.FileSystem, pattern string) (*txttpl.Template, error) {+ paths, err := fs.Glob(pattern)+ if err != nil {+ return nil, err+ }+ if len(paths) == 0 {+ return nil, nil+ }++ tpl := txttpl.New(filepath.Base(paths[0]))+ if f != nil {+ tpl = tpl.Funcs(f)+ }++ for _, p := range paths {+ f, err := fs.Read(p)+ if err != nil {+ return nil, fmt.Errorf("%s: %v", p, err)+ }++ _, err = tpl.Parse(string(f))+ if err != nil {+ return nil, err+ }+ }++ return tpl, nil+}diff --git a/internal/notifs/notifs.go b/internal/notifs/notifs.goindex c323e32b..7ff7b1f0 100644--- a/internal/notifs/notifs.go+++ b/internal/notifs/notifs.go@@ -11,6 +11,7 @@ import ("net/textproto""regexp""strings"+ txttpl "text/template""github.com/knadh/listmonk/internal/messenger/email""github.com/knadh/listmonk/models"@@ -44,17 +45,19 @@ type Notifs struct {var (reTitle = regexp.MustCompile(`(?s)<title\s*data-i18n\s*>(.+?)</title>`)- Tpls *template.Template- no *Notifs+ Tpls *template.Template+ TxtTpls *txttpl.Template+ no *Notifs)// Initialize returns a new Notifs instance.-func Initialize(opt Opt, tpls *template.Template, em *email.Emailer, lo *log.Logger) {+func Initialize(opt Opt, tpls *template.Template, txtTpls *txttpl.Template, em *email.Emailer, lo *log.Logger) {if no != nil {lo.Fatal("notifs already initialized")}Tpls = tpls+ TxtTpls = txtTplsno = &Notifs{opt: opt,em: em,@@ -80,6 +83,16 @@ func Notify(toEmails []string, subject, tplName string, data any, hdr textproto.}body := buf.Bytes()+ var bufAlt bytes.Buffer+ tplNameText := tplName + "-text"+ if TxtTpls != nil && TxtTpls.Lookup(tplNameText) != nil {+ if err := TxtTpls.ExecuteTemplate(&bufAlt, tplNameText, data); err != nil {+ no.lo.Printf("error compiling notification template '%s': %v", tplNameText, err)+ return err+ }+ }+ altBody := bufAlt.Bytes()+subject, body = GetTplSubject(subject, body)m := models.Message{@@ -90,6 +103,7 @@ func Notify(toEmails []string, subject, tplName string, data any, hdr textproto.Subject: subject,Body: body,Headers: hdr,+ AltBody: altBody,}// Send the message.