Compare commits

..

1 Commits

Author SHA1 Message Date
Leo Q
ec6ad24810 feat: support smtp without auth (#1101) 2024-07-03 22:23:49 +08:00
5 changed files with 22 additions and 52 deletions

View File

@@ -63,7 +63,6 @@ var SMTPPort = 587
var SMTPAccount = ""
var SMTPFrom = ""
var SMTPToken = ""
var SMTPAuthLoginEnabled = false
var GitHubClientId = ""
var GitHubClientSecret = ""

View File

@@ -4,38 +4,16 @@ import (
"crypto/rand"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"github.com/songquanpeng/one-api/common/config"
"net"
"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 shouldAuth() bool {
return config.SMTPAccount != "" || config.SMTPToken != ""
}
func SendEmail(subject string, receiver string, content string) error {
@@ -68,21 +46,24 @@ 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, config.SystemName, config.SMTPFrom, encodedSubject, messageId, time.Now().Format(time.RFC1123Z), content))
var auth smtp.Auth
if config.SMTPAuthLoginEnabled {
auth = LoginAuth(config.SMTPAccount, config.SMTPToken)
} else {
auth = smtp.PlainAuth("", config.SMTPAccount, config.SMTPToken, config.SMTPServer)
}
auth := smtp.PlainAuth("", config.SMTPAccount, config.SMTPToken, config.SMTPServer)
addr := fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort)
to := strings.Split(receiver, ";")
if config.SMTPPort == 465 {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: config.SMTPServer,
if config.SMTPPort == 465 || !shouldAuth() {
// need advanced client
var conn net.Conn
var err error
if config.SMTPPort == 465 {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: config.SMTPServer,
}
conn, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort), tlsConfig)
} else {
conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort))
}
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort), tlsConfig)
if err != nil {
return err
}
@@ -91,8 +72,10 @@ func SendEmail(subject string, receiver string, content string) error {
return err
}
defer client.Close()
if err = client.Auth(auth); err != nil {
return err
if shouldAuth() {
if err = client.Auth(auth); err != nil {
return err
}
}
if err = client.Mail(config.SMTPFrom); err != nil {
return err

View File

@@ -330,7 +330,6 @@
"通常和邮箱地址保持一致": "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",

View File

@@ -45,7 +45,6 @@ func InitOptionMap() {
config.OptionMap["SMTPPort"] = strconv.Itoa(config.SMTPPort)
config.OptionMap["SMTPAccount"] = ""
config.OptionMap["SMTPToken"] = ""
config.OptionMap["SMTPAuthLoginEnabled"] = strconv.FormatBool(config.SMTPAuthLoginEnabled)
config.OptionMap["Notice"] = ""
config.OptionMap["About"] = ""
config.OptionMap["HomePageContent"] = ""
@@ -151,8 +150,6 @@ func updateOptionMap(key string, value string) (err error) {
config.DisplayInCurrencyEnabled = boolValue
case "DisplayTokenStatEnabled":
config.DisplayTokenStatEnabled = boolValue
case "SMTPAuthLoginEnabled":
config.SMTPAuthLoginEnabled = boolValue
}
}
switch key {

View File

@@ -18,7 +18,6 @@ const SystemSetting = () => {
SMTPAccount: '',
SMTPFrom: '',
SMTPToken: '',
SMTPAuthLoginEnabled: '',
ServerAddress: '',
Footer: '',
WeChatAuthEnabled: '',
@@ -77,7 +76,6 @@ const SystemSetting = () => {
case 'TurnstileCheckEnabled':
case 'EmailDomainRestrictionEnabled':
case 'RegisterEnabled':
case 'SMTPAuthLoginEnabled':
value = inputs[key] === 'true' ? 'false' : 'true';
break;
default:
@@ -109,7 +107,7 @@ const SystemSetting = () => {
}
if (
name === 'Notice' ||
(name.startsWith('SMTP') && !name.endsWith('Enabled')) ||
name.startsWith('SMTP') ||
name === 'ServerAddress' ||
name === 'GitHubClientId' ||
name === 'GitHubClientSecret' ||
@@ -446,12 +444,6 @@ const SystemSetting = () => {
checked={inputs.RegisterEnabled === 'true'}
placeholder='敏感信息不会发送到前端显示'
/>
<Form.Checkbox
checked={inputs.SMTPAuthLoginEnabled === 'true'}
label='使用 SMTP LOGIN 认证方式'
name='SMTPAuthLoginEnabled'
onChange={handleInputChange}
/>
</Form.Group>
<Form.Button onClick={submitSMTP}>保存 SMTP 设置</Form.Button>
<Divider />