From 21f5d0839a630a2f2b75e64e41aa3f3d7b37198e Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 19 Aug 2021 21:51:52 +0200 Subject: [PATCH] Allow multi recipients for email notifier (#463) Co-authored-by: CrazyMax --- docs/config/index.md | 4 +++- docs/notif/mail.md | 8 +++++--- internal/config/config_test.go | 13 ++++++++---- internal/config/fixtures/config.test.yml | 4 +++- internal/model/notif_mail.go | 26 ++++++++++++------------ internal/notif/mail/client.go | 2 +- 6 files changed, 34 insertions(+), 23 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index d63b8b57..4ba2252f 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -52,7 +52,9 @@ You can override this using the [`--config` flag or `CONFIG` env var with `serve ssl: false insecureSkipVerify: false from: diun@example.com - to: webmaster@example.com + to: + - webmaster@example.com + - me@example.com rocketchat: endpoint: http://rocket.foo.com:3000 channel: "#general" diff --git a/docs/notif/mail.md b/docs/notif/mail.md index dd4733b8..9c85126b 100644 --- a/docs/notif/mail.md +++ b/docs/notif/mail.md @@ -13,7 +13,9 @@ Notifications can be sent through SMTP. ssl: false insecureSkipVerify: false from: diun@example.com - to: webmaster@example.com + to: + - webmaster@example.com + - me@example.com templateTitle: "{{ .Entry.Image }} released" templateBody: | Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released. @@ -31,7 +33,7 @@ Notifications can be sent through SMTP. | `password` | | SMTP password | | `passwordFile` | | Use content of secret file as SMTP password if `password` not defined | | `from`[^1] | | Sender email address | -| `to`[^1] | | Recipient email address | +| `to`[^1] | | List of recipients email addresses | | `templateTitle`[^1] | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title | | `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body | @@ -46,7 +48,7 @@ Notifications can be sent through SMTP. * `DIUN_NOTIF_MAIL_PASSWORD` * `DIUN_NOTIF_MAIL_PASSWORDFILE` * `DIUN_NOTIF_MAIL_FROM` - * `DIUN_NOTIF_MAIL_TO` + * `DIUN_NOTIF_MAIL_TO` (comma separated) * `DIUN_NOTIF_MAIL_TEMPLATETITLE` * `DIUN_NOTIF_MAIL_TEMPLATEBODY` diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 66a9b371..5dec6d84 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -95,8 +95,11 @@ func TestLoadFile(t *testing.T) { InsecureSkipVerify: utl.NewFalse(), LocalName: "localhost", From: "diun@example.com", - To: "webmaster@example.com", - TemplateTitle: model.NotifDefaultTemplateTitle, + To: []string{ + "webmaster@example.com", + "me@example.com", + }, + TemplateTitle: model.NotifDefaultTemplateTitle, TemplateBody: `Docker tag {{ if .Entry.Image.HubLink }}[**{{ .Entry.Image }}**]({{ .Entry.Image.HubLink }}){{ else }}**{{ .Entry.Image }}**{{ end }} which you subscribed to through {{ .Entry.Provider }} provider has been {{ if (eq .Entry.Status "new") }}newly added{{ else }}updated{{ end }}. @@ -418,8 +421,10 @@ func TestLoadMixed(t *testing.T) { InsecureSkipVerify: utl.NewTrue(), LocalName: "foo.com", From: "diun@foo.com", - To: "webmaster@foo.com", - TemplateTitle: model.NotifDefaultTemplateTitle, + To: []string{ + "webmaster@foo.com", + }, + TemplateTitle: model.NotifDefaultTemplateTitle, TemplateBody: `Docker tag {{ if .Entry.Image.HubLink }}[**{{ .Entry.Image }}**]({{ .Entry.Image.HubLink }}){{ else }}**{{ .Entry.Image }}**{{ end }} which you subscribed to through {{ .Entry.Provider }} provider has been {{ if (eq .Entry.Status "new") }}newly added{{ else }}updated{{ end }}. diff --git a/internal/config/fixtures/config.test.yml b/internal/config/fixtures/config.test.yml index 092a6848..e37de56c 100644 --- a/internal/config/fixtures/config.test.yml +++ b/internal/config/fixtures/config.test.yml @@ -37,7 +37,9 @@ notif: ssl: false insecureSkipVerify: false from: diun@example.com - to: webmaster@example.com + to: + - webmaster@example.com + - me@example.com templateBody: | Docker tag {{ if .Entry.Image.HubLink }}[**{{ .Entry.Image }}**]({{ .Entry.Image.HubLink }}){{ else }}**{{ .Entry.Image }}**{{ end }} which you subscribed to through {{ .Entry.Provider }} provider has been {{ if (eq .Entry.Status "new") }}newly added{{ else }}updated{{ end }}. diff --git a/internal/model/notif_mail.go b/internal/model/notif_mail.go index 4447afaf..8bc0c609 100644 --- a/internal/model/notif_mail.go +++ b/internal/model/notif_mail.go @@ -17,19 +17,19 @@ Need help, or have questions? Go to {{ .Meta.URL }} and leave an issue.` // NotifMail holds mail notification configuration details type NotifMail struct { - Host string `yaml:"host,omitempty" json:"host,omitempty" validate:"required"` - Port int `yaml:"port,omitempty" json:"port,omitempty" validate:"required,min=1"` - SSL *bool `yaml:"ssl,omitempty" json:"ssl,omitempty" validate:"required"` - InsecureSkipVerify *bool `yaml:"insecureSkipVerify,omitempty" json:"insecureSkipVerify,omitempty" validate:"required"` - LocalName string `yaml:"localName,omitempty" json:"localName,omitempty" validate:"omitempty"` - Username string `yaml:"username,omitempty" json:"username,omitempty" validate:"omitempty"` - UsernameFile string `yaml:"usernameFile,omitempty" json:"usernameFile,omitempty" validate:"omitempty,file"` - Password string `yaml:"password,omitempty" json:"password,omitempty" validate:"omitempty"` - PasswordFile string `yaml:"passwordFile,omitempty" json:"passwordFile,omitempty" validate:"omitempty,file"` - From string `yaml:"from,omitempty" json:"from,omitempty" validate:"required,email"` - To string `yaml:"to,omitempty" json:"to,omitempty" validate:"required,email"` - TemplateTitle string `yaml:"templateTitle,omitempty" json:"templateTitle,omitempty" validate:"required"` - TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"` + Host string `yaml:"host,omitempty" json:"host,omitempty" validate:"required"` + Port int `yaml:"port,omitempty" json:"port,omitempty" validate:"required,min=1"` + SSL *bool `yaml:"ssl,omitempty" json:"ssl,omitempty" validate:"required"` + InsecureSkipVerify *bool `yaml:"insecureSkipVerify,omitempty" json:"insecureSkipVerify,omitempty" validate:"required"` + LocalName string `yaml:"localName,omitempty" json:"localName,omitempty" validate:"omitempty"` + Username string `yaml:"username,omitempty" json:"username,omitempty" validate:"omitempty"` + UsernameFile string `yaml:"usernameFile,omitempty" json:"usernameFile,omitempty" validate:"omitempty,file"` + Password string `yaml:"password,omitempty" json:"password,omitempty" validate:"omitempty"` + PasswordFile string `yaml:"passwordFile,omitempty" json:"passwordFile,omitempty" validate:"omitempty,file"` + From string `yaml:"from,omitempty" json:"from,omitempty" validate:"required,email"` + To []string `yaml:"to,omitempty" json:"to,omitempty" validate:"required"` + TemplateTitle string `yaml:"templateTitle,omitempty" json:"templateTitle,omitempty" validate:"required"` + TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"` } // GetDefaults gets the default values diff --git a/internal/notif/mail/client.go b/internal/notif/mail/client.go index 332acd38..c6eee229 100644 --- a/internal/notif/mail/client.go +++ b/internal/notif/mail/client.go @@ -89,7 +89,7 @@ func (c *Client) Send(entry model.NotifEntry) error { mailMessage := gomail.NewMessage() mailMessage.SetHeader("From", fmt.Sprintf("%s <%s>", c.meta.Name, c.cfg.From)) - mailMessage.SetHeader("To", c.cfg.To) + mailMessage.SetHeader("To", c.cfg.To...) mailMessage.SetHeader("Subject", string(title)) mailMessage.SetBody("text/plain", textpart) mailMessage.AddAlternative("text/html", htmlpart)