Explicitly decide whether to use TLS in mailer's configuration (#5024)

* explicitly decide on using TLS for mail connections

* explicitly decide on using TLS for mail connections

* keep compatibility
This commit is contained in:
Lanre Adelowo 2018-11-26 20:21:42 +01:00 committed by techknowlogick
parent ce9a5173fe
commit 3a1ed82529
4 changed files with 10 additions and 6 deletions

View file

@ -388,6 +388,8 @@ SKIP_VERIFY =
USE_CERTIFICATE = false USE_CERTIFICATE = false
CERT_FILE = custom/mailer/cert.pem CERT_FILE = custom/mailer/cert.pem
KEY_FILE = custom/mailer/key.pem KEY_FILE = custom/mailer/key.pem
; Should SMTP connection use TLS
IS_TLS_ENABLED = false
; Mail from address, RFC 5322. This can be just an email address, or the `"Name" <email@example.com>` format ; Mail from address, RFC 5322. This can be just an email address, or the `"Name" <email@example.com>` format
FROM = FROM =
; Mailer user name and password ; Mailer user name and password

View file

@ -222,6 +222,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
`FROM` and `SENDMAIL_PATH`. `FROM` and `SENDMAIL_PATH`.
- `SENDMAIL_PATH`: **sendmail**: The location of sendmail on the operating system (can be - `SENDMAIL_PATH`: **sendmail**: The location of sendmail on the operating system (can be
command or full path). command or full path).
- ``IS_TLS_ENABLED`` : **false** : Decide if SMTP connections should use TLS.
## Cache (`cache`) ## Cache (`cache`)

View file

@ -122,11 +122,10 @@ func (s *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
} }
defer conn.Close() defer conn.Close()
isSecureConn := false isSecureConn := opts.IsTLSEnabled || (strings.HasSuffix(port, "465"))
// Start TLS directly if the port ends with 465 (SMTPS protocol) // Start TLS directly if the port ends with 465 (SMTPS protocol)
if strings.HasSuffix(port, "465") { if isSecureConn {
conn = tls.Client(conn, tlsconfig) conn = tls.Client(conn, tlsconfig)
isSecureConn = true
} }
client, err := smtp.NewClient(conn, host) client, err := smtp.NewClient(conn, host)

View file

@ -1523,6 +1523,7 @@ type Mailer struct {
SkipVerify bool SkipVerify bool
UseCertificate bool UseCertificate bool
CertFile, KeyFile string CertFile, KeyFile string
IsTLSEnabled bool
// Sendmail sender // Sendmail sender
UseSendmail bool UseSendmail bool
@ -1556,6 +1557,7 @@ func newMailService() {
UseCertificate: sec.Key("USE_CERTIFICATE").MustBool(), UseCertificate: sec.Key("USE_CERTIFICATE").MustBool(),
CertFile: sec.Key("CERT_FILE").String(), CertFile: sec.Key("CERT_FILE").String(),
KeyFile: sec.Key("KEY_FILE").String(), KeyFile: sec.Key("KEY_FILE").String(),
IsTLSEnabled: sec.Key("IS_TLS_ENABLED").MustBool(),
UseSendmail: sec.Key("USE_SENDMAIL").MustBool(), UseSendmail: sec.Key("USE_SENDMAIL").MustBool(),
SendmailPath: sec.Key("SENDMAIL_PATH").MustString("sendmail"), SendmailPath: sec.Key("SENDMAIL_PATH").MustString("sendmail"),