Add {{ templating }} support to e-mail subjects

This commit is contained in:
Kailash Nadh 2020-07-05 18:39:24 +05:30
parent 24192a327f
commit 64d44707c2
3 changed files with 36 additions and 2 deletions

View File

@ -558,7 +558,10 @@ func sendTestMessage(sub models.Subscriber, camp *models.Campaign, app *App) err
fmt.Sprintf("Error rendering message: %v", err)) fmt.Sprintf("Error rendering message: %v", err))
} }
if err := app.messenger.Push(camp.FromEmail, []string{sub.Email}, camp.Subject, m.Body(), nil); err != nil { if err := app.messenger.Push(camp.FromEmail,
[]string{sub.Email},
m.Subject(),
m.Body(), nil); err != nil {
return err return err
} }

View File

@ -68,6 +68,7 @@ type CampaignMessage struct {
from string from string
to string to string
subject string
body []byte body []byte
unsubURL string unsubURL string
} }
@ -126,6 +127,7 @@ func (m *Manager) NewCampaignMessage(c *models.Campaign, s models.Subscriber) Ca
Campaign: c, Campaign: c,
Subscriber: s, Subscriber: s,
subject: c.Subject,
from: c.FromEmail, from: c.FromEmail,
to: s.Email, to: s.Email,
unsubURL: fmt.Sprintf(m.cfg.UnsubURL, c.UUID, s.UUID), unsubURL: fmt.Sprintf(m.cfg.UnsubURL, c.UUID, s.UUID),
@ -271,7 +273,7 @@ func (m *Manager) SpawnWorkers() {
numMsg++ numMsg++
err := m.messengers[msg.Campaign.MessengerID].Push( err := m.messengers[msg.Campaign.MessengerID].Push(
msg.from, []string{msg.to}, msg.Campaign.Subject, msg.body, nil) msg.from, []string{msg.to}, msg.subject, msg.body, nil)
if err != nil { if err != nil {
m.logger.Printf("error sending message in campaign %s: %v", msg.Campaign.Name, err) m.logger.Printf("error sending message in campaign %s: %v", msg.Campaign.Name, err)
@ -480,6 +482,16 @@ func (m *Manager) sendNotif(c *models.Campaign, status, reason string) error {
// and applies the resultant bytes to Message.body to be used in messages. // and applies the resultant bytes to Message.body to be used in messages.
func (m *CampaignMessage) Render() error { func (m *CampaignMessage) Render() error {
out := bytes.Buffer{} out := bytes.Buffer{}
// Render the subject if it's a template.
if m.Campaign.SubjectTpl != nil {
if err := m.Campaign.SubjectTpl.ExecuteTemplate(&out, models.ContentTpl, m); err != nil {
return err
}
m.subject = out.String()
out.Reset()
}
if err := m.Campaign.Tpl.ExecuteTemplate(&out, models.BaseTpl, m); err != nil { if err := m.Campaign.Tpl.ExecuteTemplate(&out, models.BaseTpl, m); err != nil {
return err return err
} }
@ -487,6 +499,11 @@ func (m *CampaignMessage) Render() error {
return nil return nil
} }
// Subject returns a copy of the message subject
func (m *CampaignMessage) Subject() string {
return m.subject
}
// Body returns a copy of the message body. // Body returns a copy of the message body.
func (m *CampaignMessage) Body() []byte { func (m *CampaignMessage) Body() []byte {
out := make([]byte, len(m.body)) out := make([]byte, len(m.body))

View File

@ -169,6 +169,7 @@ type Campaign struct {
// TemplateBody is joined in from templates by the next-campaigns query. // TemplateBody is joined in from templates by the next-campaigns query.
TemplateBody string `db:"template_body" json:"-"` TemplateBody string `db:"template_body" json:"-"`
Tpl *template.Template `json:"-"` Tpl *template.Template `json:"-"`
SubjectTpl *template.Template `json:"-"`
// Pseudofield for getting the total number of subscribers // Pseudofield for getting the total number of subscribers
// in searches and queries. // in searches and queries.
@ -310,6 +311,19 @@ func (c *Campaign) CompileTemplate(f template.FuncMap) error {
return fmt.Errorf("error inserting child template: %v", err) return fmt.Errorf("error inserting child template: %v", err)
} }
// If the subject line has a template string, compile it.
if strings.Contains(c.Subject, "{{") {
subj := c.Subject
for _, r := range regTplFuncs {
subj = r.regExp.ReplaceAllString(subj, r.replace)
}
subjTpl, err := template.New(ContentTpl).Funcs(f).Parse(subj)
if err != nil {
return fmt.Errorf("error compiling subject: %v", err)
}
c.SubjectTpl = subjTpl
}
c.Tpl = out c.Tpl = out
return nil return nil
} }