From c139365a67e67330ba2d1998fcbd4370598c383a Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Tue, 1 Dec 2020 13:51:43 +0100 Subject: [PATCH] Add options to load recipients from file Signed-off-by: Lukas Bachschwell --- DOCS.md | 1 + main.go | 6 ++++++ plugin.go | 21 ++++++++++++++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/DOCS.md b/DOCS.md index cd87c77..9547c92 100644 --- a/DOCS.md +++ b/DOCS.md @@ -10,6 +10,7 @@ You can configure the plugin using the following parameters: * **password** - SMTP password * **skip_verify** - Skip verification of SSL certificates, defaults to `false` * **recipients** - List of recipients to send this mail to (besides the commit author) +* **recipients_file** - Filename to load additional recipients from (textfile with one email per line) (besides the commit author) * **recipients_only** - Do not send mails to the commit author, but only to **recipients**, defaults to `false` * **subject** - The subject line template * **body** - The email body template diff --git a/main.go b/main.go index 5dee7ff..e96fd59 100644 --- a/main.go +++ b/main.go @@ -56,6 +56,11 @@ func main() { Usage: "skip tls verify", EnvVar: "PLUGIN_SKIP_VERIFY", }, + cli.StringFlag{ + Name: "recipients.file", + Usage: "file to read recipients from", + EnvVar: "EMAIL_RECIPIENTS_FILE,PLUGIN_RECIPIENTS_FILE", + }, cli.StringSliceFlag{ Name: "recipients", Usage: "recipient addresses", @@ -386,6 +391,7 @@ func run(c *cli.Context) error { Password: c.String("password"), SkipVerify: c.Bool("skip.verify"), Recipients: c.StringSlice("recipients"), + RecipientsFile: c.String("recipients.file"), RecipientsOnly: c.Bool("recipients.only"), Subject: c.String("template.subject"), Body: c.String("template.body"), diff --git a/plugin.go b/plugin.go index e7bb98c..5a4045c 100644 --- a/plugin.go +++ b/plugin.go @@ -1,13 +1,15 @@ package main import ( + "bufio" "crypto/tls" + "os" + log "github.com/Sirupsen/logrus" "github.com/aymerick/douceur/inliner" "github.com/drone/drone-go/template" "github.com/jaytaylor/html2text" "gopkg.in/gomail.v2" - "os" ) type ( @@ -86,12 +88,13 @@ type ( Password string SkipVerify bool Recipients []string + RecipientsFile string RecipientsOnly bool Subject string Body string Attachment string Attachments []string - ClientHostname string + ClientHostname string } Plugin struct { @@ -126,6 +129,18 @@ func (p Plugin) Exec() error { } } + if p.Config.RecipientsFile != "" { + f, err := os.Open(p.Config.RecipientsFile) + if err == nil { + scanner := bufio.NewScanner(f) + for scanner.Scan() { + p.Config.Recipients = append(p.Config.Recipients, scanner.Text()) + } + } else { + log.Errorf("Could not open RecipientsFile %s: %v", p.Config.RecipientsFile, err) + } + } + if p.Config.Username == "" && p.Config.Password == "" { dialer = &gomail.Dialer{Host: p.Config.Host, Port: p.Config.Port} } else { @@ -134,7 +149,7 @@ func (p Plugin) Exec() error { if p.Config.SkipVerify { dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true} } - dialer.LocalName = p.Config.ClientHostname + dialer.LocalName = p.Config.ClientHostname closer, err := dialer.Dial() if err != nil {