1
0
Fork 0
mirror of https://github.com/TwiN/gatus.git synced 2024-12-14 11:58:04 +00:00

feat(alerting): Allow specifying a different username for email provider (#231)

* Update email alerting provider to supply a username, maintaining backwards compatibility with from

* Update README.md

Co-authored-by: Tom Moitié <tomm@gendius.co.uk>
Co-authored-by: TwiN <twin@twinnation.org>
This commit is contained in:
Tom Moitié 2022-01-12 01:07:25 +00:00 committed by GitHub
parent c466542990
commit ce6f58f403
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View file

@ -354,20 +354,22 @@ endpoints:
#### Configuring Email alerts #### Configuring Email alerts
| Parameter | Description | Default | | Parameter | Description | Default |
|:-------------------------------|:-------------------------------------------------------------------------------------------|:--------------| |:-------------------------------|:-------------------------------------------------------------------------------------------|:----------------------|
| `alerting.email` | Configuration for alerts of type `email` | `{}` | | `alerting.email` | Configuration for alerts of type `email` | `{}` |
| `alerting.email.from` | Email used to send the alert | Required `""` | | `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.username` | Username of the SMTP server used to send the alert. If empty, uses `alerting.email.from`. | `""` |
| `alerting.email.host` | Host of the mail server (e.g. `smtp.gmail.com`) | Required `""` | | `alerting.email.password` | Password of the SMTP server used to send the alert | Required `""` |
| `alerting.email.port` | Port the mail server is listening to (e.g. `587`) | Required `0` | | `alerting.email.host` | Host of the mail server (e.g. `smtp.gmail.com`) | Required `""` |
| `alerting.email.to` | Email(s) to send the alerts to | Required `""` | | `alerting.email.port` | Port the mail server is listening to (e.g. `587`) | Required `0` |
| `alerting.email.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A | | `alerting.email.to` | Email(s) to send the alerts to | Required `""` |
| `alerting.email.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A |
```yaml ```yaml
alerting: alerting:
email: email:
from: "from@example.com" from: "from@example.com"
username: "from@example.com"
password: "hunter2" password: "hunter2"
host: "mail.example.com" host: "mail.example.com"
port: 587 port: 587

View file

@ -13,6 +13,7 @@ import (
// AlertProvider is the configuration necessary for sending an alert using SMTP // AlertProvider is the configuration necessary for sending an alert using SMTP
type AlertProvider struct { type AlertProvider struct {
From string `yaml:"from"` From string `yaml:"from"`
Username string `yaml:"username"`
Password string `yaml:"password"` Password string `yaml:"password"`
Host string `yaml:"host"` Host string `yaml:"host"`
Port int `yaml:"port"` Port int `yaml:"port"`
@ -29,13 +30,19 @@ func (provider *AlertProvider) IsValid() bool {
// Send an alert using the provider // Send an alert using the provider
func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) error { 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) subject, body := provider.buildMessageSubjectAndBody(endpoint, alert, result, resolved)
m := gomail.NewMessage() m := gomail.NewMessage()
m.SetHeader("From", provider.From) m.SetHeader("From", provider.From)
m.SetHeader("To", strings.Split(provider.To, ",")...) m.SetHeader("To", strings.Split(provider.To, ",")...)
m.SetHeader("Subject", subject) m.SetHeader("Subject", subject)
m.SetBody("text/plain", body) 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) return d.DialAndSend(m)
} }