Add more general PLAIN/CRAM-MD5 auth support.
This commit is contained in:
parent
f6ba424325
commit
6d140d7571
|
@ -188,27 +188,40 @@ func (n *notifier) sendEmailNotification(to string, a *Alert) error {
|
||||||
}
|
}
|
||||||
defer c.Quit()
|
defer c.Quit()
|
||||||
|
|
||||||
hasAuth, _ := c.Extension("AUTH")
|
// Authenticate if we and the server are both configured for it.
|
||||||
smtpAuth := os.Getenv("SMTP_AUTH")
|
hasAuth, mechs := c.Extension("AUTH")
|
||||||
if hasAuth && smtpAuth != "" {
|
username := os.Getenv("SMTP_AUTH_USERNAME")
|
||||||
idx := strings.IndexRune(smtpAuth, ':')
|
if hasAuth && username != "" {
|
||||||
if idx < 0 {
|
for _, mech := range strings.Split(mechs, " ") {
|
||||||
return fmt.Errorf("SMTP_AUTH must be in the format username:password")
|
switch mech {
|
||||||
}
|
case "CRAM-MD5":
|
||||||
username := smtpAuth[:idx]
|
secret := os.Getenv("SMTP_AUTH_SECRET")
|
||||||
password := smtpAuth[idx+1:]
|
if secret == "" {
|
||||||
host, _, _ := net.SplitHostPort(*smtpSmartHost)
|
continue
|
||||||
|
}
|
||||||
|
if err := c.Auth(smtp.CRAMMD5Auth(username, secret)); err != nil {
|
||||||
|
return fmt.Errorf("cram-md5 auth failed: %s", err)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
case "PLAIN":
|
||||||
|
password := os.Getenv("SMTP_AUTH_PASSWORD")
|
||||||
|
if password == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
identity := os.Getenv("SMTP_AUTH_IDENTITY")
|
||||||
|
|
||||||
if hasTLS, _ := c.Extension("TLS"); hasTLS {
|
// PLAIN auth requires TLS to be started first.
|
||||||
cfg := &tls.Config{ServerName: host}
|
host, _, _ := net.SplitHostPort(*smtpSmartHost)
|
||||||
if err := c.StartTLS(cfg); err != nil {
|
if err := c.StartTLS(&tls.Config{ServerName: host}); err != nil {
|
||||||
return err
|
return fmt.Errorf("starttls failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.Auth(smtp.PlainAuth(identity, username, password, host)); err != nil {
|
||||||
|
return fmt.Errorf("plain auth failed: %s", err)
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.Auth(smtp.PlainAuth("", username, password, host)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the sender and recipient.
|
// Set the sender and recipient.
|
||||||
|
|
Loading…
Reference in New Issue