Skip to content

Commit

Permalink
new mail template method to allow to use them with other notification…
Browse files Browse the repository at this point in the history
…s services such as sms
  • Loading branch information
lucasmenendez committed Nov 22, 2024
1 parent 3a7ebdb commit a1f3e2b
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions notifications/mailtemplates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ func (mt MailTemplate) ExecTemplate(data any) (*notifications.Notification, erro
if !ok {
return nil, fmt.Errorf("template not found")
}
// create a new notification with the subject and plain body of the
// template placeholder
n := &notifications.Notification{
Subject: mt.Placeholder.Subject,
PlainBody: mt.Placeholder.PlainBody,
// create a notification with the plain body placeholder inflated
n, err := mt.ExecPlain(data)
if err != nil {
return nil, err
}
// execute the template with the data provided parse the template
// set the mail subject
n.Subject = mt.Placeholder.Subject
// parse the html template file
tmpl, err := htmltemplate.ParseFiles(path)
if err != nil {
return nil, err
Expand All @@ -91,17 +92,31 @@ func (mt MailTemplate) ExecTemplate(data any) (*notifications.Notification, erro
}
// set the body of the notification
n.Body = buf.String()
// if the plain body is not empty, execute the template with the data
// provided
if n.PlainBody != "" {
tmpl, err := texttemplate.New("plain").Parse(n.PlainBody)
return n, nil
}

// ExecPlain method executes the plain body placeholder template with the data
// provided. If the placeholder plain body is not empty, it executes the plain
// text template with the data provided. If it is empty, just returns an empty
// notification. It resulting notification and an error if the defined template
// could not be executed.
//
// This method also allows to notifications services that do not support HTML
// emails to use a mail template.
func (mt MailTemplate) ExecPlain(data any) (*notifications.Notification, error) {
n := &notifications.Notification{}
if mt.Placeholder.PlainBody != "" {
// parse the placeholder plain body template
tmpl, err := texttemplate.New("plain").Parse(mt.Placeholder.PlainBody)
if err != nil {
return nil, err
}
// inflate the template with the data
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, data); err != nil {
return nil, err
}
// return the notification with the plain body filled with the data
n.PlainBody = buf.String()
}
return n, nil
Expand Down

0 comments on commit a1f3e2b

Please sign in to comment.