Skip to content

Commit da550f4

Browse files
committed
feat: support optional SMTP auth
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 4eac2ac commit da550f4

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

coderd/notifications/dispatch/smtp.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ type SMTPHandler struct {
5353
cfg codersdk.NotificationsEmailConfig
5454
log slog.Logger
5555

56-
loginWarnOnce sync.Once
56+
noAuthWarnOnce sync.Once
57+
loginWarnOnce sync.Once
5758

5859
helpers template.FuncMap
5960
}
@@ -431,6 +432,15 @@ func (s *SMTPHandler) loadCertificate() (*tls.Certificate, error) {
431432
func (s *SMTPHandler) auth(ctx context.Context, mechs string) (sasl.Client, error) {
432433
username := s.cfg.Auth.Username.String()
433434

435+
// All auth mechanisms require username, so if one is not defined then don't return an auth client.
436+
if username == "" {
437+
s.noAuthWarnOnce.Do(func() {
438+
s.log.Warn(ctx, "skipping auth; no username configured", slog.F("support_mechanisms", mechs))
439+
})
440+
// nolint:nilnil // This is a valid response.
441+
return nil, nil
442+
}
443+
434444
var errs error
435445
list := strings.Split(mechs, " ")
436446
for _, mech := range list {

coderd/notifications/dispatch/smtp_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ func TestSMTP(t *testing.T) {
203203
retryable: false,
204204
},
205205
{
206-
// No auth, no problem!
207206
name: "No auth mechanisms supported, none configured",
208207
authMechs: []string{},
209208
cfg: codersdk.NotificationsEmailConfig{
@@ -213,6 +212,16 @@ func TestSMTP(t *testing.T) {
213212
toAddrs: []string{to},
214213
expectedAuthMeth: "",
215214
},
215+
{
216+
name: "Auth mechanisms supported optionally, none configured",
217+
authMechs: []string{sasl.Login, sasl.Plain},
218+
cfg: codersdk.NotificationsEmailConfig{
219+
Hello: hello,
220+
From: from,
221+
},
222+
toAddrs: []string{to},
223+
expectedAuthMeth: "",
224+
},
216225
/**
217226
* TLS connections
218227
*/

0 commit comments

Comments
 (0)