Merge pull request #16 from rchakode/feat/allow-unauthenticated-smtp

Feat/allow unauthenticated smtp
This commit is contained in:
Rodrigue Chakode 2022-05-09 10:41:33 +02:00 committed by GitHub
commit 0e9b9b54ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 6 deletions

View File

@ -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"

View File

@ -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`.

View File

@ -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")

View File

@ -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.GetBool("SMTP_AUTHENTICATION_ENABLED") {
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