From 468de54746bd99d1e0f08e3670f461827e2bccf9 Mon Sep 17 00:00:00 2001 From: Dominik George Date: Thu, 28 Apr 2022 20:59:07 +0200 Subject: [PATCH] Allow unauthenticated SMTP --- sendmail.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/sendmail.go b/sendmail.go index 4d61c77..51b3622 100644 --- a/sendmail.go +++ b/sendmail.go @@ -75,10 +75,6 @@ func (m *SendMailRequest) Execute() error { // Connect to the SMTP Server smtpServerAddr := viper.GetString("SMTP_SERVER_ADDR") smtpServerHost, _, _ := net.SplitHostPort(smtpServerAddr) - smtpClientAuth := smtp.PlainAuth("", - viper.GetString("SMTP_CLIENT_USERNAME"), - viper.GetString("SMTP_CLIENT_PASSWORD"), - smtpServerHost) // TLS config tlsconfig := &tls.Config{ @@ -100,8 +96,15 @@ func (m *SendMailRequest) Execute() error { } defer smtpClient.Quit() - if err = smtpClient.Auth(smtpClientAuth); err != nil { - return fmt.Errorf("failed authenticating to smtp server (%s)", err) + // Authenticate if configured + if viper.GetString("SMTP_CLIENT_USERNAME") != "" { + smtpClientAuth := smtp.PlainAuth("", + viper.GetString("SMTP_CLIENT_USERNAME"), + viper.GetString("SMTP_CLIENT_PASSWORD"), + smtpServerHost) + if err = smtpClient.Auth(smtpClientAuth); err != nil { + return fmt.Errorf("failed authenticating to smtp server (%s)", err) + } } // Initialize a mail transaction