| 60 | } |
| 61 | |
| 62 | func (s *SMTPHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTmpl string, helpers template.FuncMap) (DeliveryFunc, error) { |
| 63 | // First render the subject & body into their own discrete strings. |
| 64 | subject, err := markdown.PlaintextFromMarkdown(titleTmpl) |
| 65 | if err != nil { |
| 66 | return nil, xerrors.Errorf("render subject: %w", err) |
| 67 | } |
| 68 | |
| 69 | htmlBody := markdown.HTMLFromMarkdown(bodyTmpl) |
| 70 | plainBody, err := markdown.PlaintextFromMarkdown(bodyTmpl) |
| 71 | if err != nil { |
| 72 | return nil, xerrors.Errorf("render plaintext body: %w", err) |
| 73 | } |
| 74 | |
| 75 | // Then, reuse these strings in the HTML & plain body templates. |
| 76 | payload.Labels["_subject"] = subject |
| 77 | payload.Labels["_body"] = htmlBody |
| 78 | htmlBody, err = render.GoTemplate(htmlTemplate, payload, helpers) |
| 79 | if err != nil { |
| 80 | return nil, xerrors.Errorf("render full html template: %w", err) |
| 81 | } |
| 82 | payload.Labels["_body"] = plainBody |
| 83 | plainBody, err = render.GoTemplate(plainTemplate, payload, helpers) |
| 84 | if err != nil { |
| 85 | return nil, xerrors.Errorf("render full plaintext template: %w", err) |
| 86 | } |
| 87 | |
| 88 | return s.dispatch(subject, htmlBody, plainBody, payload.UserEmail), nil |
| 89 | } |
| 90 | |
| 91 | // dispatch returns a DeliveryFunc capable of delivering a notification via SMTP. |
| 92 | // |