Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Add support for plaintext system e-mail templates.

Kailash Nadhb290d271c017
diff --git a/cmd/campaigns.go b/cmd/campaigns.go
index e8d1eb81..f2a024e1 100644
--- a/cmd/campaigns.go
+++ b/cmd/campaigns.go
@@ -773,7 +773,7 @@ func makeOptinCampaignMessage(o campaignReq, app *App) (campaignReq, error) {
// Prepare sample opt-in message for the campaign.
var b bytes.Buffer
- if err := app.notifTpls.ExecuteTemplate(&b, "optin-campaign", struct {
+ if err := app.notifTpls.tpls.ExecuteTemplate(&b, "optin-campaign", struct {
Lists []models.List
OptinURLAttr template.HTMLAttr
}{lists, optinURLAttr}); err != nil {
diff --git a/cmd/init.go b/cmd/init.go
index 226da913..c49e0bc2 100644
--- a/cmd/init.go
+++ b/cmd/init.go
@@ -1,6 +1,7 @@
package main
import (
+ "bytes"
"encoding/json"
"fmt"
"html/template"
@@ -32,6 +33,7 @@ import (
"github.com/knadh/listmonk/internal/messenger/email"
"github.com/knadh/listmonk/internal/messenger/postback"
"github.com/knadh/listmonk/internal/subimporter"
+ "github.com/knadh/listmonk/models"
"github.com/knadh/stuffbin"
"github.com/labstack/echo"
flag "github.com/spf13/pflag"
@@ -78,6 +80,11 @@ type constants struct {
BounceSendgridEnabled bool
}
+type notifTpls struct {
+ tpls *template.Template
+ contentType string
+}
+
func initFlags() {
f := flag.NewFlagSet("config", flag.ContinueOnError)
f.Usage = func() {
@@ -491,7 +498,7 @@ func initMediaStore() media.Store {
// initNotifTemplates compiles and returns e-mail notification templates that are
// used for sending ad-hoc notifications to admins and subscribers.
-func initNotifTemplates(path string, fs stuffbin.FileSystem, i *i18n.I18n, cs *constants) *template.Template {
+func initNotifTemplates(path string, fs stuffbin.FileSystem, i *i18n.I18n, cs *constants) *notifTpls {
// Register utility functions that the e-mail templates can use.
funcs := template.FuncMap{
"RootURL": func() string {
@@ -505,11 +512,36 @@ func initNotifTemplates(path string, fs stuffbin.FileSystem, i *i18n.I18n, cs *c
},
}
- tpl, err := stuffbin.ParseTemplatesGlob(funcs, fs, "/static/email-templates/*.html")
+ tpls, err := stuffbin.ParseTemplatesGlob(funcs, fs, "/static/email-templates/*.html")
if err != nil {
lo.Fatalf("error parsing e-mail notif templates: %v", err)
}
- return tpl
+
+ html, err := fs.Read("/static/email-templates/base.html")
+ if err != nil {
+ lo.Fatalf("error reading static/email-templates/base.html: %v", err)
+ }
+
+ out := &notifTpls{
+ tpls: tpls,
+ contentType: models.CampaignContentTypeHTML,
+ }
+
+ // Determine whether the notification templates are HTML or plaintext.
+ // Copy the first few (arbitrary) bytes of the template and check if has the <!doctype html> tag.
+ ln := 256
+ if len(html) < ln {
+ ln = len(html)
+ }
+ h := make([]byte, ln)
+ copy(h, html[0:ln])
+
+ if !bytes.Contains(bytes.ToLower(h), []byte("<!doctype html>")) {
+ out.contentType = models.CampaignContentTypePlain
+ lo.Println("system e-mail templates are plaintext")
+ }
+
+ return out
}
// initBounceManager initializes the bounce manager that scans mailboxes and listens to webhooks
diff --git a/cmd/main.go b/cmd/main.go
index 8c727405..739fa191 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
- "html/template"
"io"
"log"
"os"
@@ -43,7 +42,7 @@ type App struct {
media media.Store
i18n *i18n.I18n
bounce *bounce.Manager
- notifTpls *template.Template
+ notifTpls *notifTpls
log *log.Logger
bufLog *buflog.BufLog
diff --git a/cmd/notifications.go b/cmd/notifications.go
index 896bc97e..b60a137c 100644
--- a/cmd/notifications.go
+++ b/cmd/notifications.go
@@ -27,12 +27,13 @@ func (app *App) sendNotification(toEmails []string, subject, tplName string, dat
}
var b bytes.Buffer
- if err := app.notifTpls.ExecuteTemplate(&b, tplName, data); err != nil {
+ if err := app.notifTpls.tpls.ExecuteTemplate(&b, tplName, data); err != nil {
app.log.Printf("error compiling notification template '%s': %v", tplName, err)
return err
}
m := manager.Message{}
+ m.ContentType = app.notifTpls.contentType
m.From = app.constants.FromEmail
m.To = toEmails
m.Subject = subject
diff --git a/cmd/public.go b/cmd/public.go
index 687a29e4..09a20929 100644
--- a/cmd/public.go
+++ b/cmd/public.go
@@ -437,7 +437,7 @@ func handleSelfExportSubscriberData(c echo.Context) error {
// Prepare the attachment e-mail.
var msg bytes.Buffer
- if err := app.notifTpls.ExecuteTemplate(&msg, notifSubscriberData, data); err != nil {
+ if err := app.notifTpls.tpls.ExecuteTemplate(&msg, notifSubscriberData, data); err != nil {
app.log.Printf("error compiling notification template '%s': %v", notifSubscriberData, err)
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "",
@@ -447,10 +447,11 @@ func handleSelfExportSubscriberData(c echo.Context) error {
// Send the data as a JSON attachment to the subscriber.
const fname = "data.json"
if err := app.messengers[emailMsgr].Push(messenger.Message{
- From: app.constants.FromEmail,
- To: []string{data.Email},
- Subject: "Your data",
- Body: msg.Bytes(),
+ ContentType: app.notifTpls.contentType,
+ From: app.constants.FromEmail,
+ To: []string{data.Email},
+ Subject: "Your data",
+ Body: msg.Bytes(),
Attachments: []messenger.Attachment{
{
Name: fname,