Skip to content

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

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.go
index 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.Body
diff --git a/cmd/tx.go b/cmd/tx.go
index 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.ContentType
msg.Messenger = m.Messenger
msg.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.md
index 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 modes
diff --git a/models/messages.go b/models/messages.go
index 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 Subscriber
Tx *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)
}