Skip to content

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

Add optional `subject` param to tx API. Closes #2333.

Kailash Nadh301c13a60da3
diff --git a/docs/docs/content/apis/transactional.md b/docs/docs/content/apis/transactional.md
index 80207ec7..48f04095 100644
--- a/docs/docs/content/apis/transactional.md
+++ b/docs/docs/content/apis/transactional.md
@@ -20,6 +20,7 @@ Allows sending transactional messages to one or more subscribers via a preconfig
| subscriber_ids | number\[\] | | Multiple subscriber IDs as an alternative to `subscriber_id`. |
| template_id | number | Yes | ID of the transactional template to be used for the message. |
| from_email | string | | Optional sender email. |
+| subject | string | | Optional subject. If empty, the subject defined on the template is used |
| data | JSON | | Optional nested JSON map. Available in the template as `{{ .Tx.Data.* }}`. |
| headers | JSON\[\] | | Optional array of email headers. |
| messenger | string | | Messenger to send the message. Default is `email`. |
diff --git a/models/models.go b/models/models.go
index d4a8e7b5..acd8c0f9 100644
--- a/models/models.go
+++ b/models/models.go
@@ -387,11 +387,11 @@ type TxMessage struct {
Headers Headers `json:"headers"`
ContentType string `json:"content_type"`
Messenger string `json:"messenger"`
+ Subject string `json:"subject"`
// File attachments added from multi-part form data.
Attachments []Attachment `json:"-"`
- Subject string `json:"-"`
Body []byte `json:"-"`
Tpl *template.Template `json:"-"`
SubjectTpl *txttpl.Template `json:"-"`
@@ -654,15 +654,35 @@ func (m *TxMessage) Render(sub Subscriber, tpl *Template) error {
copy(m.Body, b.Bytes())
b.Reset()
+ // Was a subject provided in the message?
+ var (
+ subjTpl *txttpl.Template
+ subject = m.Subject
+ )
+ 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)
+ if err != nil {
+ return fmt.Errorf("error compiling subject: %v", err)
+ }
+ subjTpl = s
+ }
+ } else {
+ // Use the subject from the template.
+ subject = tpl.Subject
+ subjTpl = tpl.SubjectTpl
+ }
+
// If the subject is also a template, render that.
- if tpl.SubjectTpl != nil {
- if err := tpl.SubjectTpl.ExecuteTemplate(&b, BaseTpl, data); err != nil {
+ if subjTpl != nil {
+ if err := subjTpl.ExecuteTemplate(&b, BaseTpl, data); err != nil {
return err
}
m.Subject = b.String()
b.Reset()
} else {
- m.Subject = tpl.Subject
+ m.Subject = subject
}
return nil