From ce6f58f403acad16ee5b7aab2f80124ddfebffb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Moiti=C3=A9?= Date: Wed, 12 Jan 2022 01:07:25 +0000 Subject: [PATCH] feat(alerting): Allow specifying a different username for email provider (#231) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update email alerting provider to supply a username, maintaining backwards compatibility with from * Update README.md Co-authored-by: Tom Moitié Co-authored-by: TwiN --- README.md | 20 +++++++++++--------- alerting/provider/email/email.go | 9 ++++++++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e35c7c4f..5931c224 100644 --- a/README.md +++ b/README.md @@ -354,20 +354,22 @@ endpoints: #### Configuring Email alerts -| Parameter | Description | Default | -|:-------------------------------|:-------------------------------------------------------------------------------------------|:--------------| -| `alerting.email` | Configuration for alerts of type `email` | `{}` | -| `alerting.email.from` | Email used to send the alert | Required `""` | -| `alerting.email.password` | Password of the email used to send the alert | Required `""` | -| `alerting.email.host` | Host of the mail server (e.g. `smtp.gmail.com`) | Required `""` | -| `alerting.email.port` | Port the mail server is listening to (e.g. `587`) | Required `0` | -| `alerting.email.to` | Email(s) to send the alerts to | Required `""` | -| `alerting.email.default-alert` | Default alert configuration.
See [Setting a default alert](#setting-a-default-alert) | N/A | +| Parameter | Description | Default | +|:-------------------------------|:-------------------------------------------------------------------------------------------|:----------------------| +| `alerting.email` | Configuration for alerts of type `email` | `{}` | +| `alerting.email.from` | Email used to send the alert | Required `""` | +| `alerting.email.username` | Username of the SMTP server used to send the alert. If empty, uses `alerting.email.from`. | `""` | +| `alerting.email.password` | Password of the SMTP server used to send the alert | Required `""` | +| `alerting.email.host` | Host of the mail server (e.g. `smtp.gmail.com`) | Required `""` | +| `alerting.email.port` | Port the mail server is listening to (e.g. `587`) | Required `0` | +| `alerting.email.to` | Email(s) to send the alerts to | Required `""` | +| `alerting.email.default-alert` | Default alert configuration.
See [Setting a default alert](#setting-a-default-alert) | N/A | ```yaml alerting: email: from: "from@example.com" + username: "from@example.com" password: "hunter2" host: "mail.example.com" port: 587 diff --git a/alerting/provider/email/email.go b/alerting/provider/email/email.go index 03277aa1..b95f37e3 100644 --- a/alerting/provider/email/email.go +++ b/alerting/provider/email/email.go @@ -13,6 +13,7 @@ import ( // AlertProvider is the configuration necessary for sending an alert using SMTP type AlertProvider struct { From string `yaml:"from"` + Username string `yaml:"username"` Password string `yaml:"password"` Host string `yaml:"host"` Port int `yaml:"port"` @@ -29,13 +30,19 @@ func (provider *AlertProvider) IsValid() bool { // Send an alert using the provider func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) error { + var username string + if len(provider.Username) > 0 { + username = provider.Username + } else { + username = provider.From + } subject, body := provider.buildMessageSubjectAndBody(endpoint, alert, result, resolved) m := gomail.NewMessage() m.SetHeader("From", provider.From) m.SetHeader("To", strings.Split(provider.To, ",")...) m.SetHeader("Subject", subject) m.SetBody("text/plain", body) - d := gomail.NewDialer(provider.Host, provider.Port, provider.From, provider.Password) + d := gomail.NewDialer(provider.Host, provider.Port, username, provider.Password) return d.DialAndSend(m) }