新增支持LoginAuth认证方式适用于IBM、微软等部分邮箱服务 (#2041)

This commit is contained in:
计乐心
2025-06-05 11:31:25 +08:00
committed by GitHub
parent ba63bf2371
commit 174721bc55
7 changed files with 74 additions and 23 deletions
+41 -1
View File
@@ -60,8 +60,14 @@ func send(to []string, subject string, body string) error {
host := global.GlobalConfig.Host
port := global.GlobalConfig.Port
isSSL := global.GlobalConfig.IsSSL
isLoginAuth := global.GlobalConfig.IsLoginAuth
auth := smtp.PlainAuth("", from, secret, host)
var auth smtp.Auth
if isLoginAuth {
auth = LoginAuth(from, secret)
} else {
auth = smtp.PlainAuth("", from, secret, host)
}
e := email.NewEmail()
if nickname != "" {
e.From = fmt.Sprintf("%s <%s>", nickname, from)
@@ -80,3 +86,37 @@ func send(to []string, subject string, body string) error {
}
return err
}
// LoginAuth 用于IBM、微软邮箱服务器的LOGIN认证方式
type loginAuth struct {
username, password string
}
func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
}
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte{}, 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:
// 邮箱服务器可能发送的其他提示信息
prompt := strings.ToLower(string(fromServer))
if strings.Contains(prompt, "username") || strings.Contains(prompt, "user") {
return []byte(a.username), nil
}
if strings.Contains(prompt, "password") || strings.Contains(prompt, "pass") {
return []byte(a.password), nil
}
}
}
return nil, nil
}