Add ability to attach a file to the sent mail(s)

This commit is contained in:
Michael de Wit 2017-08-31 11:14:08 +02:00
parent f8088a6429
commit 982dc51fc6
3 changed files with 12 additions and 0 deletions

View file

@ -13,6 +13,7 @@ You can configure the plugin using the following parameters:
* **recipients_only** - Do not send mails to the commit author, but only to **recipients**, defaults to `false` * **recipients_only** - Do not send mails to the commit author, but only to **recipients**, defaults to `false`
* **subject** - The subject line template * **subject** - The subject line template
* **body** - The email body template * **body** - The email body template
* **attachment** - An optional file to attach to the sent mail(s), can be an absolute path or relative to the working directory.
## Example ## Example

View file

@ -74,6 +74,11 @@ func main() {
Usage: "body template", Usage: "body template",
EnvVar: "PLUGIN_BODY", EnvVar: "PLUGIN_BODY",
}, },
cli.StringFlag{
Name: "attachment",
Usage: "attachment filename",
EnvVar: "PLUGIN_ATTACHMENT",
},
// Drone environment // Drone environment
// Repo // Repo
@ -369,6 +374,7 @@ func run(c *cli.Context) error {
RecipientsOnly: c.Bool("recipients.only"), RecipientsOnly: c.Bool("recipients.only"),
Subject: c.String("template.subject"), Subject: c.String("template.subject"),
Body: c.String("template.body"), Body: c.String("template.body"),
Attachment: c.String("attachment"),
}, },
} }

View file

@ -88,6 +88,7 @@ type (
RecipientsOnly bool RecipientsOnly bool
Subject string Subject string
Body string Body string
Attachment string
} }
Plugin struct { Plugin struct {
@ -195,6 +196,10 @@ func (p Plugin) Exec() error {
message.AddAlternative("text/plain", plainBody) message.AddAlternative("text/plain", plainBody)
message.AddAlternative("text/html", html) message.AddAlternative("text/html", html)
if p.Config.Attachment != "" {
message.Attach(p.Config.Attachment)
}
if err := gomail.Send(closer, message); err != nil { if err := gomail.Send(closer, message); err != nil {
log.Errorf("Could not send email to %q: %v", recipient, err) log.Errorf("Could not send email to %q: %v", recipient, err)
return err return err