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
Add new 'altbody' param to /api/tx to send multipart plaintext bodies in transactional mails. Closes #2486
Kailash Nadhbe7b60ae9f3b
diff --git a/cmd/templates.go b/cmd/templates.goindex c0b9be4d..7d06fa15 100644--- a/cmd/templates.go+++ b/cmd/templates.go@@ -265,7 +265,7 @@ func (a *App) previewTemplate(tpl models.Template) ([]byte, error) {}// Render the message.- if err := m.Render(dummySubscriber, &tpl); err != nil {+ if err := m.Render(dummySubscriber, &tpl, a.manager.GenericTemplateFuncs()); err != nil {return nil, echo.NewHTTPError(http.StatusBadRequest, err.Error())}out = m.Bodydiff --git a/cmd/tx.go b/cmd/tx.goindex 3b631414..baa760fa 100644--- a/cmd/tx.go+++ b/cmd/tx.go@@ -129,7 +129,7 @@ func (a *App) SendTxMessage(c echo.Context) error {}// Render the message.- if err := m.Render(sub, tpl); err != nil {+ if err := m.Render(sub, tpl, a.manager.GenericTemplateFuncs()); err != nil {return echo.NewHTTPError(http.StatusBadRequest,a.i18n.Ts("globals.messages.errorFetching", "name"))}@@ -143,6 +143,7 @@ func (a *App) SendTxMessage(c echo.Context) error {msg.ContentType = m.ContentTypemsg.Messenger = m.Messengermsg.Body = m.Body+ msg.AltBody = []byte(m.AltBody)for _, a := range m.Attachments {msg.Attachments = append(msg.Attachments, models.Attachment{Name: a.Name,diff --git a/docs/docs/content/apis/transactional.md b/docs/docs/content/apis/transactional.mdindex b73bb0b5..ae7afb00 100644--- a/docs/docs/content/apis/transactional.md+++ b/docs/docs/content/apis/transactional.md@@ -26,6 +26,7 @@ Allows sending transactional messages to one or more subscribers via a preconfig| headers | JSON\[\] | | Optional array of email headers. || messenger | string | | Messenger to send the message. Default is `email`. || content_type | string | | Email format options include `html`, `markdown`, and `plain`. |+| altbody | string | | Optional alternate plaintext body for multipart HTML emails. |##### Subscriber modesdiff --git a/models/messages.go b/models/messages.goindex 6645b01e..fe80c688 100644--- a/models/messages.go+++ b/models/messages.go@@ -61,6 +61,7 @@ type TxMessage struct {ContentType string `json:"content_type"`Messenger string `json:"messenger"`Subject string `json:"subject"`+ AltBody string `json:"altbody"`// File attachments added from multi-part form data.Attachments []Attachment `json:"-"`@@ -70,7 +71,7 @@ type TxMessage struct {SubjectTpl *txttpl.Template `json:"-"`}-func (m *TxMessage) Render(sub Subscriber, tpl *Template) error {+func (m *TxMessage) Render(sub Subscriber, tpl *Template, funcs txttpl.FuncMap) error {data := struct {Subscriber SubscriberTx *TxMessage@@ -85,6 +86,19 @@ func (m *TxMessage) Render(sub Subscriber, tpl *Template) error {copy(m.Body, b.Bytes())b.Reset()+ // Render alt body if it has any templating strings.+ if m.AltBody != "" && strings.Contains(m.AltBody, "{{") {+ t, err := txttpl.New(BaseTpl).Funcs(funcs).Parse(m.AltBody)+ if err != nil {+ return fmt.Errorf("error compiling alt body: %v", err)+ }+ if err := t.ExecuteTemplate(&b, BaseTpl, data); err != nil {+ return err+ }+ m.AltBody = b.String()+ b.Reset()+ }+// Was a subject provided in the message?var (subjTpl *txttpl.Template@@ -93,7 +107,7 @@ func (m *TxMessage) Render(sub Subscriber, tpl *Template) error {if subject != "" {if strings.Contains(m.Subject, "{{") {// If the subject has a template string, render that.- s, err := txttpl.New(BaseTpl).Funcs(txttpl.FuncMap(nil)).Parse(m.Subject)+ s, err := txttpl.New(BaseTpl).Funcs(funcs).Parse(m.Subject)if err != nil {return fmt.Errorf("error compiling subject: %v", err)}