Skip to content

Commit

Permalink
Allow environmental variable lookup in our templates.
Browse files Browse the repository at this point in the history
This pull-request closes #97, by allowing environmental variables
to be used in the email template:

        To: {{env "RECIPIENT"}}
        From: {{env "SENDER"}}
        ...
  • Loading branch information
skx committed May 4, 2022
1 parent 915df4f commit 571db4e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions processor/emailer/emailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os/exec"
"path/filepath"
"strconv"
"strings"
"text/template"

"github.com/mmcdole/gofeed"
Expand Down Expand Up @@ -51,6 +52,16 @@ func New(feed *gofeed.Feed, item withstate.FeedItem, opts []configfile.Option) *
return &Emailer{feed: feed, item: item, opts: opts}
}

// env returns the contents of an environmental variable.
func env(s string) string {
return (os.Getenv(s))
}

// split converts a string to an array.
func split(in string, delim string) []string {
return strings.Split(in, delim)
}

// loadTemplate loads the template used for sending the email notification.
func (e *Emailer) loadTemplate() (*template.Template, error) {

Expand Down Expand Up @@ -83,7 +94,9 @@ func (e *Emailer) loadTemplate() (*template.Template, error) {
// Function map allows exporting functions to the template
//
funcMap := template.FuncMap{
"env": env,
"quoteprintable": e.toQuotedPrintable,
"split": split,
}

tmpl := template.Must(template.New("email.tmpl").Funcs(funcMap).Parse(string(content)))
Expand Down

0 comments on commit 571db4e

Please sign in to comment.