2015-06-04 07:29:39 -05:00
|
|
|
package setting
|
|
|
|
|
2023-12-14 12:59:43 -06:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
2021-07-19 05:31:51 -05:00
|
|
|
|
2015-06-04 07:29:39 -05:00
|
|
|
type SmtpSettings struct {
|
2020-05-13 09:33:40 -05:00
|
|
|
Enabled bool
|
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
|
|
|
FromAddress string
|
|
|
|
FromName string
|
|
|
|
EhloIdentity string
|
|
|
|
StartTLSPolicy string
|
|
|
|
SkipVerify bool
|
2023-12-14 12:59:43 -06:00
|
|
|
StaticHeaders map[string]string
|
2024-01-22 09:50:05 -06:00
|
|
|
EnableTracing bool
|
2015-06-08 10:56:56 -05:00
|
|
|
|
|
|
|
SendWelcomeEmailOnSignUp bool
|
2021-07-19 05:31:51 -05:00
|
|
|
TemplatesPatterns []string
|
|
|
|
ContentTypes []string
|
2015-06-08 10:56:56 -05:00
|
|
|
}
|
|
|
|
|
2023-12-14 12:59:43 -06:00
|
|
|
// validates mail headers
|
|
|
|
var mailHeaderRegex = regexp.MustCompile(`^[A-Z][A-Za-z0-9]*(-[A-Z][A-Za-z0-9]*)*$`)
|
|
|
|
|
|
|
|
func (cfg *Cfg) readSmtpSettings() error {
|
2018-04-30 09:21:04 -05:00
|
|
|
sec := cfg.Raw.Section("smtp")
|
|
|
|
cfg.Smtp.Enabled = sec.Key("enabled").MustBool(false)
|
|
|
|
cfg.Smtp.Host = sec.Key("host").String()
|
|
|
|
cfg.Smtp.User = sec.Key("user").String()
|
|
|
|
cfg.Smtp.Password = sec.Key("password").String()
|
|
|
|
cfg.Smtp.CertFile = sec.Key("cert_file").String()
|
|
|
|
cfg.Smtp.KeyFile = sec.Key("key_file").String()
|
|
|
|
cfg.Smtp.FromAddress = sec.Key("from_address").String()
|
|
|
|
cfg.Smtp.FromName = sec.Key("from_name").String()
|
|
|
|
cfg.Smtp.EhloIdentity = sec.Key("ehlo_identity").String()
|
2020-05-13 09:33:40 -05:00
|
|
|
cfg.Smtp.StartTLSPolicy = sec.Key("startTLS_policy").String()
|
2018-04-30 09:21:04 -05:00
|
|
|
cfg.Smtp.SkipVerify = sec.Key("skip_verify").MustBool(false)
|
2015-06-08 10:56:56 -05:00
|
|
|
|
2018-04-30 09:21:04 -05:00
|
|
|
emails := cfg.Raw.Section("emails")
|
|
|
|
cfg.Smtp.SendWelcomeEmailOnSignUp = emails.Key("welcome_email_on_sign_up").MustBool(false)
|
2021-07-19 05:31:51 -05:00
|
|
|
cfg.Smtp.TemplatesPatterns = util.SplitString(emails.Key("templates_pattern").MustString("emails/*.html, emails/*.txt"))
|
|
|
|
cfg.Smtp.ContentTypes = util.SplitString(emails.Key("content_types").MustString("text/html"))
|
2023-12-14 12:59:43 -06:00
|
|
|
|
|
|
|
// populate static headers
|
|
|
|
if err := cfg.readGrafanaSmtpStaticHeaders(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-22 09:50:05 -06:00
|
|
|
cfg.Smtp.EnableTracing = sec.Key("enable_tracing").MustBool(false)
|
|
|
|
|
2023-12-14 12:59:43 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validHeader(header string) bool {
|
|
|
|
return mailHeaderRegex.MatchString(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Cfg) readGrafanaSmtpStaticHeaders() error {
|
|
|
|
staticHeadersSection := cfg.Raw.Section("smtp.static_headers")
|
|
|
|
keys := staticHeadersSection.Keys()
|
|
|
|
cfg.Smtp.StaticHeaders = make(map[string]string, len(keys))
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
if !validHeader(key.Name()) {
|
|
|
|
return fmt.Errorf("header %q in [smtp.static_headers] configuration: must follow canonical MIME form", key.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Smtp.StaticHeaders[key.Name()] = key.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|