Enable SSL in SMTP
This commit is contained in:
		
							parent
							
								
									efbc36d61f
								
							
						
					
					
						commit
						abaeac442f
					
				|  | @ -3,9 +3,9 @@ package emailer | ||||||
| import ( | import ( | ||||||
| 	"crypto/tls" | 	"crypto/tls" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"time" |  | ||||||
| 
 |  | ||||||
| 	mail "github.com/xhit/go-simple-mail/v2" | 	mail "github.com/xhit/go-simple-mail/v2" | ||||||
|  | 	"strings" | ||||||
|  | 	"time" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type SmtpMail struct { | type SmtpMail struct { | ||||||
|  | @ -14,13 +14,14 @@ type SmtpMail struct { | ||||||
| 	username   string | 	username   string | ||||||
| 	password   string | 	password   string | ||||||
| 	authType   mail.AuthType | 	authType   mail.AuthType | ||||||
|  | 	encryption mail.Encryption | ||||||
| 	noTLSCheck bool | 	noTLSCheck bool | ||||||
| 	fromName   string | 	fromName   string | ||||||
| 	from       string | 	from       string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func authType(authType string) mail.AuthType { | func authType(authType string) mail.AuthType { | ||||||
| 	switch authType { | 	switch strings.ToUpper(authType) { | ||||||
| 	case "PLAIN": | 	case "PLAIN": | ||||||
| 		return mail.AuthPlain | 		return mail.AuthPlain | ||||||
| 	case "LOGIN": | 	case "LOGIN": | ||||||
|  | @ -30,8 +31,19 @@ func authType(authType string) mail.AuthType { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func NewSmtpMail(hostname string, port int, username string, password string, noTLSCheck bool, auth string, fromName, from string) *SmtpMail { | func encryptionType(encryptionType string) mail.Encryption { | ||||||
| 	ans := SmtpMail{hostname: hostname, port: port, username: username, password: password, noTLSCheck: noTLSCheck, fromName: fromName, from: from, authType: authType(auth)} | 	switch strings.ToUpper(encryptionType) { | ||||||
|  | 	case "SSL": | ||||||
|  | 		return mail.EncryptionSSL | ||||||
|  | 	case "SSLTLS": | ||||||
|  | 		return mail.EncryptionSSLTLS | ||||||
|  | 	default: | ||||||
|  | 		return mail.EncryptionSTARTTLS | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func NewSmtpMail(hostname string, port int, username string, password string, noTLSCheck bool, auth string, fromName, from string, encryption string) *SmtpMail { | ||||||
|  | 	ans := SmtpMail{hostname: hostname, port: port, username: username, password: password, noTLSCheck: noTLSCheck, fromName: fromName, from: from, authType: authType(auth), encryption: encryptionType(encryption)} | ||||||
| 	return &ans | 	return &ans | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -50,7 +62,7 @@ func (o *SmtpMail) Send(toName string, to string, subject string, content string | ||||||
| 	server.Authentication = o.authType | 	server.Authentication = o.authType | ||||||
| 	server.Username = o.username | 	server.Username = o.username | ||||||
| 	server.Password = o.password | 	server.Password = o.password | ||||||
| 	server.Encryption = mail.EncryptionSTARTTLS | 	server.Encryption = o.encryption | ||||||
| 	server.KeepAlive = false | 	server.KeepAlive = false | ||||||
| 	server.ConnectTimeout = 10 * time.Second | 	server.ConnectTimeout = 10 * time.Second | ||||||
| 	server.SendTimeout = 10 * time.Second | 	server.SendTimeout = 10 * time.Second | ||||||
|  |  | ||||||
							
								
								
									
										7
									
								
								main.go
								
								
								
								
							
							
						
						
									
										7
									
								
								main.go
								
								
								
								
							|  | @ -33,6 +33,7 @@ var ( | ||||||
| 	flagSmtpPassword   string | 	flagSmtpPassword   string | ||||||
| 	flagSmtpAuthType   string = "None" | 	flagSmtpAuthType   string = "None" | ||||||
| 	flagSmtpNoTLSCheck bool   = false | 	flagSmtpNoTLSCheck bool   = false | ||||||
|  | 	flagSmtpEncryption string = "STARTTLS" | ||||||
| 	flagSendgridApiKey string | 	flagSendgridApiKey string | ||||||
| 	flagEmailFrom      string | 	flagEmailFrom      string | ||||||
| 	flagEmailFromName  string = "WireGuard UI" | 	flagEmailFromName  string = "WireGuard UI" | ||||||
|  | @ -60,7 +61,8 @@ func init() { | ||||||
| 	flag.StringVar(&flagSmtpUsername, "smtp-username", util.LookupEnvOrString("SMTP_USERNAME", flagSmtpUsername), "SMTP Password") | 	flag.StringVar(&flagSmtpUsername, "smtp-username", util.LookupEnvOrString("SMTP_USERNAME", flagSmtpUsername), "SMTP Password") | ||||||
| 	flag.StringVar(&flagSmtpPassword, "smtp-password", util.LookupEnvOrString("SMTP_PASSWORD", flagSmtpPassword), "SMTP Password") | 	flag.StringVar(&flagSmtpPassword, "smtp-password", util.LookupEnvOrString("SMTP_PASSWORD", flagSmtpPassword), "SMTP Password") | ||||||
| 	flag.BoolVar(&flagSmtpNoTLSCheck, "smtp-no-tls-check", util.LookupEnvOrBool("SMTP_NO_TLS_CHECK", flagSmtpNoTLSCheck), "Disable TLS verification for SMTP. This is potentially dangerous.") | 	flag.BoolVar(&flagSmtpNoTLSCheck, "smtp-no-tls-check", util.LookupEnvOrBool("SMTP_NO_TLS_CHECK", flagSmtpNoTLSCheck), "Disable TLS verification for SMTP. This is potentially dangerous.") | ||||||
| 	flag.StringVar(&flagSmtpAuthType, "smtp-auth-type", util.LookupEnvOrString("SMTP_AUTH_TYPE", flagSmtpAuthType), "SMTP Auth Type : Plain or None.") | 	flag.StringVar(&flagSmtpEncryption, "smtp-use-ssl", util.LookupEnvOrString("SMTP_USE_SSL", flagSmtpEncryption), "SMTP Encryption : SSL, SSLTLS or STARTTLS (by default)") | ||||||
|  | 	flag.StringVar(&flagSmtpAuthType, "smtp-auth-type", util.LookupEnvOrString("SMTP_AUTH_TYPE", flagSmtpAuthType), "SMTP Auth Type : Plain, Login or None.") | ||||||
| 	flag.StringVar(&flagSendgridApiKey, "sendgrid-api-key", util.LookupEnvOrString("SENDGRID_API_KEY", flagSendgridApiKey), "Your sendgrid api key.") | 	flag.StringVar(&flagSendgridApiKey, "sendgrid-api-key", util.LookupEnvOrString("SENDGRID_API_KEY", flagSendgridApiKey), "Your sendgrid api key.") | ||||||
| 	flag.StringVar(&flagEmailFrom, "email-from", util.LookupEnvOrString("EMAIL_FROM_ADDRESS", flagEmailFrom), "'From' email address.") | 	flag.StringVar(&flagEmailFrom, "email-from", util.LookupEnvOrString("EMAIL_FROM_ADDRESS", flagEmailFrom), "'From' email address.") | ||||||
| 	flag.StringVar(&flagEmailFromName, "email-from-name", util.LookupEnvOrString("EMAIL_FROM_NAME", flagEmailFromName), "'From' email name.") | 	flag.StringVar(&flagEmailFromName, "email-from-name", util.LookupEnvOrString("EMAIL_FROM_NAME", flagEmailFromName), "'From' email name.") | ||||||
|  | @ -78,6 +80,7 @@ func init() { | ||||||
| 	util.SmtpPassword = flagSmtpPassword | 	util.SmtpPassword = flagSmtpPassword | ||||||
| 	util.SmtpAuthType = flagSmtpAuthType | 	util.SmtpAuthType = flagSmtpAuthType | ||||||
| 	util.SmtpNoTLSCheck = flagSmtpNoTLSCheck | 	util.SmtpNoTLSCheck = flagSmtpNoTLSCheck | ||||||
|  | 	util.SmtpEncryption = flagSmtpEncryption | ||||||
| 	util.SendgridApiKey = flagSendgridApiKey | 	util.SendgridApiKey = flagSendgridApiKey | ||||||
| 	util.EmailFrom = flagEmailFrom | 	util.EmailFrom = flagEmailFrom | ||||||
| 	util.EmailFromName = flagEmailFromName | 	util.EmailFromName = flagEmailFromName | ||||||
|  | @ -138,7 +141,7 @@ func main() { | ||||||
| 	if util.SendgridApiKey != "" { | 	if util.SendgridApiKey != "" { | ||||||
| 		sendmail = emailer.NewSendgridApiMail(util.SendgridApiKey, util.EmailFromName, util.EmailFrom) | 		sendmail = emailer.NewSendgridApiMail(util.SendgridApiKey, util.EmailFromName, util.EmailFrom) | ||||||
| 	} else { | 	} else { | ||||||
| 		sendmail = emailer.NewSmtpMail(util.SmtpHostname, util.SmtpPort, util.SmtpUsername, util.SmtpPassword, util.SmtpNoTLSCheck, util.SmtpAuthType, util.EmailFromName, util.EmailFrom) | 		sendmail = emailer.NewSmtpMail(util.SmtpHostname, util.SmtpPort, util.SmtpUsername, util.SmtpPassword, util.SmtpNoTLSCheck, util.SmtpAuthType, util.EmailFromName, util.EmailFrom, util.SmtpEncryption) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	app.GET(util.BasePath+"/_health", handler.Health()) | 	app.GET(util.BasePath+"/_health", handler.Health()) | ||||||
|  |  | ||||||
|  | @ -11,6 +11,7 @@ var ( | ||||||
| 	SmtpUsername   string | 	SmtpUsername   string | ||||||
| 	SmtpPassword   string | 	SmtpPassword   string | ||||||
| 	SmtpNoTLSCheck bool | 	SmtpNoTLSCheck bool | ||||||
|  | 	SmtpEncryption string | ||||||
| 	SmtpAuthType   string | 	SmtpAuthType   string | ||||||
| 	SendgridApiKey string | 	SendgridApiKey string | ||||||
| 	EmailFrom      string | 	EmailFrom      string | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue