From 8f3d18f5a4e9b9d53a372c4706c5427ea796cfda Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 31 Dec 2022 20:53:35 +0800 Subject: [PATCH 1/2] Don't lookup mail server when using sendmail --- modules/setting/mailer.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/modules/setting/mailer.go b/modules/setting/mailer.go index 7324328ee30db..f214279f3d86a 100644 --- a/modules/setting/mailer.go +++ b/modules/setting/mailer.go @@ -176,14 +176,22 @@ func newMailService() { } } - // we want to warn if users use SMTP on a non-local IP; - // we might as well take the opportunity to check that it has an IP at all - ips := tryResolveAddr(MailService.SMTPAddr) - if MailService.Protocol == "smtp" { - for _, ip := range ips { - if !ip.IsLoopback() { - log.Warn("connecting over insecure SMTP protocol to non-local address is not recommended") - break + if MailService.Protocol == "sendmail" { + var err error + MailService.SendmailArgs, err = shellquote.Split(sec.Key("SENDMAIL_ARGS").String()) + if err != nil { + log.Error("Failed to parse Sendmail args: %s with error %v", CustomConf, err) + } + } else { + // we want to warn if users use SMTP on a non-local IP; + // we might as well take the opportunity to check that it has an IP at all + ips := tryResolveAddr(MailService.SMTPAddr) + if MailService.Protocol == "smtp" { + for _, ip := range ips { + if !ip.IsLoopback() { + log.Warn("connecting over insecure SMTP protocol to non-local address is not recommended") + break + } } } } @@ -214,14 +222,6 @@ func newMailService() { MailService.EnvelopeFrom = parsed.Address } - if MailService.Protocol == "sendmail" { - var err error - MailService.SendmailArgs, err = shellquote.Split(sec.Key("SENDMAIL_ARGS").String()) - if err != nil { - log.Error("Failed to parse Sendmail args: %s with error %v", CustomConf, err) - } - } - log.Info("Mail Service Enabled") } From eea0eae8af1d2fed196e0033d504cc8c3df40fe9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 9 Jan 2023 10:03:18 +0800 Subject: [PATCH 2/2] use switch --- modules/setting/mailer.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/setting/mailer.go b/modules/setting/mailer.go index f214279f3d86a..e7cc812eef8ad 100644 --- a/modules/setting/mailer.go +++ b/modules/setting/mailer.go @@ -176,15 +176,17 @@ func newMailService() { } } - if MailService.Protocol == "sendmail" { + // we want to warn if users use SMTP on a non-local IP; + // we might as well take the opportunity to check that it has an IP at all + // This check is not needed for sendmail + switch MailService.Protocol { + case "sendmail": var err error MailService.SendmailArgs, err = shellquote.Split(sec.Key("SENDMAIL_ARGS").String()) if err != nil { - log.Error("Failed to parse Sendmail args: %s with error %v", CustomConf, err) + log.Error("Failed to parse Sendmail args: '%s' with error %v", sec.Key("SENDMAIL_ARGS").String(), err) } - } else { - // we want to warn if users use SMTP on a non-local IP; - // we might as well take the opportunity to check that it has an IP at all + case "smtp", "smtps", "smtp+starttls", "smtp+unix": ips := tryResolveAddr(MailService.SMTPAddr) if MailService.Protocol == "smtp" { for _, ip := range ips { @@ -194,6 +196,7 @@ func newMailService() { } } } + case "dummy": // just mention and do nothing } if MailService.From != "" {