Merge pull request #16 from rchakode/feat/allow-unauthenticated-smtp
Feat/allow unauthenticated smtp
This commit is contained in:
		
						commit
						0e9b9b54ac
					
				| 
						 | 
					@ -13,6 +13,7 @@ env_variables:
 | 
				
			||||||
  TEMPLATE_CONTACT_REQUEST_REPLY: templates/template_reply_contact_request.html
 | 
					  TEMPLATE_CONTACT_REQUEST_REPLY: templates/template_reply_contact_request.html
 | 
				
			||||||
  SMTP_SERVER_ADDR: "smtp.mailgun.org:587"
 | 
					  SMTP_SERVER_ADDR: "smtp.mailgun.org:587"
 | 
				
			||||||
  SMTP_VERITY_CERT: true
 | 
					  SMTP_VERITY_CERT: true
 | 
				
			||||||
 | 
					  SMTP_AUTHENTICATION_ENABLED: true
 | 
				
			||||||
  SMTP_CLIENT_USERNAME: "postmaster@example.com"
 | 
					  SMTP_CLIENT_USERNAME: "postmaster@example.com"
 | 
				
			||||||
  SMTP_CLIENT_PASSWORD: "postmasterSecretPassWord"
 | 
					  SMTP_CLIENT_PASSWORD: "postmasterSecretPassWord"
 | 
				
			||||||
  CONTACT_REPLY_EMAIL: "noreply@example.com"
 | 
					  CONTACT_REPLY_EMAIL: "noreply@example.com"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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_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_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_USERNAME`: Set the username to connect to the SMTP server.
 | 
				
			||||||
* `SMTP_CLIENT_PASSWORD`: Set the password 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`.
 | 
					* `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`.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										1
									
								
								main.go
								
								
								
								
							
							
						
						
									
										1
									
								
								main.go
								
								
								
								
							| 
						 | 
					@ -76,6 +76,7 @@ func main() {
 | 
				
			||||||
	viper.SetDefault("CONTACT_REPLY_BCC_EMAIL", "contact@company.com")
 | 
						viper.SetDefault("CONTACT_REPLY_BCC_EMAIL", "contact@company.com")
 | 
				
			||||||
	viper.SetDefault("EMAIL_SUBJECT", "Thanks to try our product")
 | 
						viper.SetDefault("EMAIL_SUBJECT", "Thanks to try our product")
 | 
				
			||||||
	viper.SetDefault("DEMO_URL", "http://company.com/product-demo")
 | 
						viper.SetDefault("DEMO_URL", "http://company.com/product-demo")
 | 
				
			||||||
 | 
						viper.SetDefault("SMTP_AUTHENTICATION_ENABLED", true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	host := os.Getenv("HOST")
 | 
						host := os.Getenv("HOST")
 | 
				
			||||||
	port := os.Getenv("PORT")
 | 
						port := os.Getenv("PORT")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										15
									
								
								sendmail.go
								
								
								
								
							
							
						
						
									
										15
									
								
								sendmail.go
								
								
								
								
							| 
						 | 
					@ -75,10 +75,6 @@ func (m *SendMailRequest) Execute() error {
 | 
				
			||||||
	// Connect to the SMTP Server
 | 
						// Connect to the SMTP Server
 | 
				
			||||||
	smtpServerAddr := viper.GetString("SMTP_SERVER_ADDR")
 | 
						smtpServerAddr := viper.GetString("SMTP_SERVER_ADDR")
 | 
				
			||||||
	smtpServerHost, _, _ := net.SplitHostPort(smtpServerAddr)
 | 
						smtpServerHost, _, _ := net.SplitHostPort(smtpServerAddr)
 | 
				
			||||||
	smtpClientAuth := smtp.PlainAuth("",
 | 
					 | 
				
			||||||
		viper.GetString("SMTP_CLIENT_USERNAME"),
 | 
					 | 
				
			||||||
		viper.GetString("SMTP_CLIENT_PASSWORD"),
 | 
					 | 
				
			||||||
		smtpServerHost)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// TLS config
 | 
						// TLS config
 | 
				
			||||||
	tlsconfig := &tls.Config{
 | 
						tlsconfig := &tls.Config{
 | 
				
			||||||
| 
						 | 
					@ -100,8 +96,15 @@ func (m *SendMailRequest) Execute() error {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer smtpClient.Quit()
 | 
						defer smtpClient.Quit()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err = smtpClient.Auth(smtpClientAuth); err != nil {
 | 
						// Authenticate if configured
 | 
				
			||||||
		return fmt.Errorf("failed authenticating to smtp server (%s)", err)
 | 
						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
 | 
						// Initialize a mail transaction
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Reference in New Issue