ausocean / cloud

GNU General Public License v3.0
1 stars 1 forks source link

notify: Emails formatted by templates #141

Open scruzin opened 1 week ago

scruzin commented 1 week ago

At present, email bodies are simple strings constructed by the sender, which limits their composition.

It is proposed to have a WithTemplates option, whereby templates are optionally supplied to the Notifier.

The notifier would include an optional property:

templates *template.Template

Templates would be keyed by the notification kind.

A new FmtSend, that wraps Send method, would be implemented roughly as follows:

func (n *Notifier) FmtSend(ctx context.Context, skey int64, kind string, data interface{}) error {
        if n.templates == nil {
                return errors.New("missing templates")
        }
        var b bytes.Buffer
        // Look up the template for this kind of notification.
        err = n.templates.ExecuteTemplate(&b, kind, data) {
        if err != nil {
                // Handle error
                return err
        }
        return n.Send(ctx, skey, kind, b.String())
}