From 468de54746bd99d1e0f08e3670f461827e2bccf9 Mon Sep 17 00:00:00 2001 From: Dominik George Date: Thu, 28 Apr 2022 20:59:07 +0200 Subject: [PATCH 1/2] 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 From 09fa59110bb4c006d0212fd654ba1bd46c0acd8c Mon Sep 17 00:00:00 2001 From: Rodrigue Chakode Date: Sat, 30 Apr 2022 11:19:46 +0200 Subject: [PATCH 2/2] add variable SMTP_AUTHENTICATION_ENABLED * boolean setting whether SMTP authentication is required or not (default: true) --- app.yaml.sample | 1 + docs/configuration-variables.md | 1 + main.go | 1 + sendmail.go | 2 +- 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app.yaml.sample b/app.yaml.sample index 0b71cbe..3c5533a 100644 --- a/app.yaml.sample +++ b/app.yaml.sample @@ -13,6 +13,7 @@ env_variables: TEMPLATE_CONTACT_REQUEST_REPLY: templates/template_reply_contact_request.html SMTP_SERVER_ADDR: "smtp.mailgun.org:587" SMTP_VERITY_CERT: true + SMTP_AUTHENTICATION_ENABLED: true SMTP_CLIENT_USERNAME: "postmaster@example.com" SMTP_CLIENT_PASSWORD: "postmasterSecretPassWord" CONTACT_REPLY_EMAIL: "noreply@example.com" diff --git a/docs/configuration-variables.md b/docs/configuration-variables.md index e242ef5..53acf33 100644 --- a/docs/configuration-variables.md +++ b/docs/configuration-variables.md @@ -4,6 +4,7 @@ Regardless of the deployment platform (Google App Engine, Kubernetes, Docker), t * `SMTP_SERVER_ADDR`: Set the address of the SMTP server in the form of `host:port`. It's required that the SMTP server being supporting TLS. * `SMTP_VERITY_CERT`: Tell whether the SMTP certificate should be validated against top level authorities. If you're using a self-signed certificate on the SMTP server, this value must be set to `false`. +* `SMTP_AUTHENTICATION_ENABLED`: Boolean (default: `true`) indicating whether SMTP authentication is required or not. If true, the variables `SMTP_CLIENT_USERNAME` and `SMTP_CLIENT_PASSWORD` are used the perform the authentication. * `SMTP_CLIENT_USERNAME`: Set the username to connect to the SMTP server. * `SMTP_CLIENT_PASSWORD`: Set the password to connect to the SMTP server. * `CONTACT_REPLY_EMAIL`: Set an email address for the reply email. It's not necessary a valid email address; for example if don't want the user to reply the email, you can set something like `noreply@example.com`. diff --git a/main.go b/main.go index 49bb8a5..f8a96e9 100644 --- a/main.go +++ b/main.go @@ -76,6 +76,7 @@ func main() { viper.SetDefault("CONTACT_REPLY_BCC_EMAIL", "contact@company.com") viper.SetDefault("EMAIL_SUBJECT", "Thanks to try our product") viper.SetDefault("DEMO_URL", "http://company.com/product-demo") + viper.SetDefault("SMTP_AUTHENTICATION_ENABLED", true) host := os.Getenv("HOST") port := os.Getenv("PORT") diff --git a/sendmail.go b/sendmail.go index 51b3622..e58c098 100644 --- a/sendmail.go +++ b/sendmail.go @@ -97,7 +97,7 @@ func (m *SendMailRequest) Execute() error { defer smtpClient.Quit() // Authenticate if configured - if viper.GetString("SMTP_CLIENT_USERNAME") != "" { + if viper.GetBool("SMTP_AUTHENTICATION_ENABLED") { smtpClientAuth := smtp.PlainAuth("", viper.GetString("SMTP_CLIENT_USERNAME"), viper.GetString("SMTP_CLIENT_PASSWORD"),