diff --git a/notify/impl.go b/notify/impl.go index e7739866..09c9a5b3 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -221,6 +221,13 @@ func NewEmail(c *config.EmailConfig, t *template.Template, l log.Logger) *Email // auth resolves a string of authentication mechanisms. func (n *Email) auth(mechs string) (smtp.Auth, error) { username := n.conf.AuthUsername + + // If no username is set, keep going without authentication. + if n.conf.AuthUsername == "" { + level.Debug(n.logger).Log("msg", "smtp_auth_username is not configured. Attempting to send email without authenticating") + return nil, nil + } + err := &types.MultiError{} for _, mech := range strings.Split(mechs, " ") { switch mech { diff --git a/notify/impl_test.go b/notify/impl_test.go index db46f143..bac38d81 100644 --- a/notify/impl_test.go +++ b/notify/impl_test.go @@ -295,7 +295,7 @@ func TestOpsGenie(t *testing.T) { func TestEmailConfigNoAuthMechs(t *testing.T) { email := &Email{ - conf: &config.EmailConfig{}, tmpl: &template.Template{}, logger: log.NewNopLogger(), + conf: &config.EmailConfig{AuthUsername: "test"}, tmpl: &template.Template{}, logger: log.NewNopLogger(), } _, err := email.auth("") require.Error(t, err) @@ -304,8 +304,9 @@ func TestEmailConfigNoAuthMechs(t *testing.T) { func TestEmailConfigMissingAuthParam(t *testing.T) { + conf := &config.EmailConfig{AuthUsername: "test"} email := &Email{ - conf: &config.EmailConfig{}, tmpl: &template.Template{}, logger: log.NewNopLogger(), + conf: conf, tmpl: &template.Template{}, logger: log.NewNopLogger(), } _, err := email.auth("CRAM-MD5") require.Error(t, err) @@ -324,6 +325,15 @@ func TestEmailConfigMissingAuthParam(t *testing.T) { require.Equal(t, err.Error(), "missing password for PLAIN auth mechanism; missing password for LOGIN auth mechanism") } +func TestEmailNoUsernameStillOk(t *testing.T) { + email := &Email{ + conf: &config.EmailConfig{}, tmpl: &template.Template{}, logger: log.NewNopLogger(), + } + a, err := email.auth("CRAM-MD5") + require.NoError(t, err) + require.Nil(t, a) +} + func TestVictorOpsCustomFields(t *testing.T) { logger := log.NewNopLogger() tmpl := createTmpl(t)