Add options to load recipients from file

Signed-off-by: Lukas Bachschwell <lukas@lbsfilm.at>
This commit is contained in:
Lukas Bachschwell 2020-12-01 13:51:43 +01:00
parent 28bc16668a
commit c139365a67
No known key found for this signature in database
GPG key ID: CCC6AA87CC8DF425
3 changed files with 25 additions and 3 deletions

View file

@ -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

View file

@ -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"),

View file

@ -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 {