From b0bf224bb148f08d362ff6ee1bb17a47ac0b54b7 Mon Sep 17 00:00:00 2001 From: Ian Li Date: Sat, 25 Nov 2023 15:40:05 +0800 Subject: [PATCH] feat: support LOGIN as SMTP authentication method. --- common/constants.go | 1 + common/email.go | 35 ++++++++++++++++++++- i18n/en.json | 1 + model/option.go | 3 ++ web/default/src/components/SystemSetting.js | 10 +++++- 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/common/constants.go b/common/constants.go index 9ee791df..45dc9ad7 100644 --- a/common/constants.go +++ b/common/constants.go @@ -62,6 +62,7 @@ var SMTPPort = 587 var SMTPAccount = "" var SMTPFrom = "" var SMTPToken = "" +var SMTPAuthLoginEnabled = false var GitHubClientId = "" var GitHubClientSecret = "" diff --git a/common/email.go b/common/email.go index b915f0f9..7df0d088 100644 --- a/common/email.go +++ b/common/email.go @@ -4,12 +4,39 @@ import ( "crypto/rand" "crypto/tls" "encoding/base64" + "errors" "fmt" "net/smtp" "strings" "time" ) +type loginAuth struct { + username, password string +} + +func LoginAuth(username, password string) smtp.Auth { + return &loginAuth{username, password} +} + +func (a *loginAuth) Start(_ *smtp.ServerInfo) (string, []byte, error) { + return "LOGIN", []byte(a.username), nil +} + +func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { + if more { + switch string(fromServer) { + case "Username:": + return []byte(a.username), nil + case "Password:": + return []byte(a.password), nil + default: + return nil, errors.New("unknown command from server during login auth") + } + } + return nil, nil +} + func SendEmail(subject string, receiver string, content string) error { if SMTPFrom == "" { // for compatibility SMTPFrom = SMTPAccount @@ -37,7 +64,13 @@ func SendEmail(subject string, receiver string, content string) error { "Date: %s\r\n"+ "Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n", receiver, SystemName, SMTPFrom, encodedSubject, messageId, time.Now().Format(time.RFC1123Z), content)) - auth := smtp.PlainAuth("", SMTPAccount, SMTPToken, SMTPServer) + + var auth smtp.Auth + if SMTPAuthLoginEnabled { + auth = LoginAuth(SMTPAccount, SMTPToken) + } else { + auth = smtp.PlainAuth("", SMTPAccount, SMTPToken, SMTPServer) + } addr := fmt.Sprintf("%s:%d", SMTPServer, SMTPPort) to := strings.Split(receiver, ";") diff --git a/i18n/en.json b/i18n/en.json index 774be837..4b65613c 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -330,6 +330,7 @@ "通常和邮箱地址保持一致": "Usually consistent with the email address", "SMTP 访问凭证": "SMTP Access Credential", "敏感信息不会发送到前端显示": "Sensitive information will not be displayed in the frontend", + "使用 SMTP LOGIN 认证方式": "Use LOGIN as SMTP authentication method", "保存 SMTP 设置": "Save SMTP Settings", "配置 GitHub OAuth App": "Configure GitHub OAuth App", "用以支持通过 GitHub 进行登录注册": "To support login & registration via GitHub", diff --git a/model/option.go b/model/option.go index 20575c9a..b665b6f9 100644 --- a/model/option.go +++ b/model/option.go @@ -47,6 +47,7 @@ func InitOptionMap() { common.OptionMap["SMTPPort"] = strconv.Itoa(common.SMTPPort) common.OptionMap["SMTPAccount"] = "" common.OptionMap["SMTPToken"] = "" + common.OptionMap["SMTPAuthLoginEnabled"] = strconv.FormatBool(common.SMTPAuthLoginEnabled) common.OptionMap["Notice"] = "" common.OptionMap["About"] = "" common.OptionMap["HomePageContent"] = "" @@ -159,6 +160,8 @@ func updateOptionMap(key string, value string) (err error) { common.DisplayInCurrencyEnabled = boolValue case "DisplayTokenStatEnabled": common.DisplayTokenStatEnabled = boolValue + case "SMTPAuthLoginEnabled": + common.SMTPAuthLoginEnabled = boolValue } } switch key { diff --git a/web/default/src/components/SystemSetting.js b/web/default/src/components/SystemSetting.js index 7b34ce5b..bcce7737 100644 --- a/web/default/src/components/SystemSetting.js +++ b/web/default/src/components/SystemSetting.js @@ -16,6 +16,7 @@ const SystemSetting = () => { SMTPAccount: '', SMTPFrom: '', SMTPToken: '', + SMTPAuthLoginEnabled: '', ServerAddress: '', Footer: '', WeChatAuthEnabled: '', @@ -72,6 +73,7 @@ const SystemSetting = () => { case 'TurnstileCheckEnabled': case 'EmailDomainRestrictionEnabled': case 'RegisterEnabled': + case 'SMTPAuthLoginEnabled': value = inputs[key] === 'true' ? 'false' : 'true'; break; default: @@ -103,7 +105,7 @@ const SystemSetting = () => { } if ( name === 'Notice' || - name.startsWith('SMTP') || + (name.startsWith('SMTP') && !name.endsWith('Enabled')) || name === 'ServerAddress' || name === 'GitHubClientId' || name === 'GitHubClientSecret' || @@ -411,6 +413,12 @@ const SystemSetting = () => { checked={inputs.RegisterEnabled === 'true'} placeholder='敏感信息不会发送到前端显示' /> + 保存 SMTP 设置