Skip to content

Commit ee64cde

Browse files
committed
新增支持LoginAuth认证方式适用于IBM、微软等部分邮箱服务
1 parent ff08d45 commit ee64cde

File tree

7 files changed

+74
-23
lines changed

7 files changed

+74
-23
lines changed

server/config/email.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package config
22

33
type Email struct {
4-
To string `mapstructure:"to" json:"to" yaml:"to"` // 收件人:多个以英文逗号分隔 例:[email protected] [email protected] 正式开发中请把此项目作为参数使用
5-
From string `mapstructure:"from" json:"from" yaml:"from"` // 发件人 你自己要发邮件的邮箱
6-
Host string `mapstructure:"host" json:"host" yaml:"host"` // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
7-
Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
8-
Nickname string `mapstructure:"nickname" json:"nickname" yaml:"nickname"` // 昵称 发件人昵称 通常为自己的邮箱
9-
Port int `mapstructure:"port" json:"port" yaml:"port"` // 端口 请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
10-
IsSSL bool `mapstructure:"is-ssl" json:"is-ssl" yaml:"is-ssl"` // 是否SSL 是否开启SSL
4+
To string `mapstructure:"to" json:"to" yaml:"to"` // 收件人:多个以英文逗号分隔 例:[email protected] [email protected] 正式开发中请把此项目作为参数使用
5+
From string `mapstructure:"from" json:"from" yaml:"from"` // 发件人 你自己要发邮件的邮箱
6+
Host string `mapstructure:"host" json:"host" yaml:"host"` // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
7+
Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
8+
Nickname string `mapstructure:"nickname" json:"nickname" yaml:"nickname"` // 昵称 发件人昵称 通常为自己的邮箱
9+
Port int `mapstructure:"port" json:"port" yaml:"port"` // 端口 请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
10+
IsSSL bool `mapstructure:"is-ssl" json:"is-ssl" yaml:"is-ssl"` // 是否SSL 是否开启SSL
11+
IsLoginAuth bool `mapstructure:"is-loginauth" json:"is-loginauth" yaml:"is-loginauth"` // 是否LoginAuth 是否使用LoginAuth认证方式(适用于IBM、微软邮箱服务器等)
1112
}

server/initialize/plugin_biz_v1.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package initialize
22

33
import (
44
"fmt"
5+
56
"github.com/flipped-aurora/gin-vue-admin/server/global"
67
"github.com/flipped-aurora/gin-vue-admin/server/plugin/email"
78
"github.com/flipped-aurora/gin-vue-admin/server/utils/plugin"
@@ -29,6 +30,7 @@ func bizPluginV1(group ...*gin.RouterGroup) {
2930
global.GVA_CONFIG.Email.Nickname,
3031
global.GVA_CONFIG.Email.Port,
3132
global.GVA_CONFIG.Email.IsSSL,
33+
global.GVA_CONFIG.Email.IsLoginAuth,
3234
))
3335
holder(public, private)
3436
}

server/plugin/email/README.MD

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
global.GVA_CONFIG.Email.Nickname,
1515
global.GVA_CONFIG.Email.Port,
1616
global.GVA_CONFIG.Email.IsSSL,
17+
global.GVA_CONFIG.Email.IsLoginAuth,
1718
))
1819

1920
同样也可以再传入时写死
@@ -26,6 +27,7 @@
2627
"登录密钥",
2728
465,
2829
true,
30+
true,
2931
))
3032

3133
### 2. 配置说明
@@ -34,13 +36,14 @@
3436
//其中 Form 和 Secret 通常来说就是用户名和密码
3537

3638
type Email struct {
37-
To string // 收件人:多个以英文逗号分隔 例:[email protected] [email protected] 正式开发中请把此项目作为参数使用 此处配置主要用于发送错误监控邮件
38-
From string // 发件人 你自己要发邮件的邮箱
39-
Host string // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
40-
Secret string // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
41-
Nickname string // 昵称 发件人昵称 自定义即可 可以不填
42-
Port int // 端口 请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
43-
IsSSL bool // 是否SSL 是否开启SSL
39+
To string // 收件人:多个以英文逗号分隔 例:[email protected] [email protected] 正式开发中请把此项目作为参数使用 此处配置主要用于发送错误监控邮件
40+
From string // 发件人 你自己要发邮件的邮箱
41+
Host string // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
42+
Secret string // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
43+
Nickname string // 昵称 发件人昵称 自定义即可 可以不填
44+
Port int // 端口 请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
45+
IsSSL bool // 是否SSL 是否开启SSL
46+
IsLoginAuth bool // 是否LoginAuth 是否使用LoginAuth认证方式(适用于IBM、微软邮箱服务器等)
4447
}
4548
#### 2-2 入参结构说明
4649
//其中 Form 和 Secret 通常来说就是用户名和密码

server/plugin/email/config/email.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package config
22

33
type Email struct {
4-
To string `mapstructure:"to" json:"to" yaml:"to"` // 收件人:多个以英文逗号分隔 例:[email protected] [email protected] 正式开发中请把此项目作为参数使用
5-
From string `mapstructure:"from" json:"from" yaml:"from"` // 发件人 你自己要发邮件的邮箱
6-
Host string `mapstructure:"host" json:"host" yaml:"host"` // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
7-
Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
8-
Nickname string `mapstructure:"nickname" json:"nickname" yaml:"nickname"` // 昵称 发件人昵称 通常为自己的邮箱
9-
Port int `mapstructure:"port" json:"port" yaml:"port"` // 端口 请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
10-
IsSSL bool `mapstructure:"is-ssl" json:"isSSL" yaml:"is-ssl"` // 是否SSL 是否开启SSL
4+
To string `mapstructure:"to" json:"to" yaml:"to"` // 收件人:多个以英文逗号分隔 例:[email protected] [email protected] 正式开发中请把此项目作为参数使用
5+
From string `mapstructure:"from" json:"from" yaml:"from"` // 发件人 你自己要发邮件的邮箱
6+
Host string `mapstructure:"host" json:"host" yaml:"host"` // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
7+
Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
8+
Nickname string `mapstructure:"nickname" json:"nickname" yaml:"nickname"` // 昵称 发件人昵称 通常为自己的邮箱
9+
Port int `mapstructure:"port" json:"port" yaml:"port"` // 端口 请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
10+
IsSSL bool `mapstructure:"is-ssl" json:"isSSL" yaml:"is-ssl"` // 是否SSL 是否开启SSL
11+
IsLoginAuth bool `mapstructure:"is-loginauth" json:"is-loginauth" yaml:"is-loginauth"` // 是否LoginAuth 是否使用LoginAuth认证
1112
}

server/plugin/email/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88

99
type emailPlugin struct{}
1010

11-
func CreateEmailPlug(To, From, Host, Secret, Nickname string, Port int, IsSSL bool) *emailPlugin {
11+
func CreateEmailPlug(To, From, Host, Secret, Nickname string, Port int, IsSSL bool, IsLoginAuth bool) *emailPlugin {
1212
global.GlobalConfig.To = To
1313
global.GlobalConfig.From = From
1414
global.GlobalConfig.Host = Host
1515
global.GlobalConfig.Secret = Secret
1616
global.GlobalConfig.Nickname = Nickname
1717
global.GlobalConfig.Port = Port
1818
global.GlobalConfig.IsSSL = IsSSL
19+
global.GlobalConfig.IsLoginAuth = IsLoginAuth
1920
return &emailPlugin{}
2021
}
2122

server/plugin/email/utils/email.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ func send(to []string, subject string, body string) error {
6060
host := global.GlobalConfig.Host
6161
port := global.GlobalConfig.Port
6262
isSSL := global.GlobalConfig.IsSSL
63+
isLoginAuth := global.GlobalConfig.IsLoginAuth
6364

64-
auth := smtp.PlainAuth("", from, secret, host)
65+
var auth smtp.Auth
66+
if isLoginAuth {
67+
auth = LoginAuth(from, secret)
68+
} else {
69+
auth = smtp.PlainAuth("", from, secret, host)
70+
}
6571
e := email.NewEmail()
6672
if nickname != "" {
6773
e.From = fmt.Sprintf("%s <%s>", nickname, from)
@@ -80,3 +86,37 @@ func send(to []string, subject string, body string) error {
8086
}
8187
return err
8288
}
89+
90+
// LoginAuth 用于IBM、微软邮箱服务器的LOGIN认证方式
91+
type loginAuth struct {
92+
username, password string
93+
}
94+
95+
func LoginAuth(username, password string) smtp.Auth {
96+
return &loginAuth{username, password}
97+
}
98+
99+
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
100+
return "LOGIN", []byte{}, nil
101+
}
102+
103+
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
104+
if more {
105+
switch string(fromServer) {
106+
case "Username:":
107+
return []byte(a.username), nil
108+
case "Password:":
109+
return []byte(a.password), nil
110+
default:
111+
// 邮箱服务器可能发送的其他提示信息
112+
prompt := strings.ToLower(string(fromServer))
113+
if strings.Contains(prompt, "username") || strings.Contains(prompt, "user") {
114+
return []byte(a.username), nil
115+
}
116+
if strings.Contains(prompt, "password") || strings.Contains(prompt, "pass") {
117+
return []byte(a.password), nil
118+
}
119+
}
120+
}
121+
return nil, nil
122+
}

web/src/view/systemTools/system/system.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@
202202
<el-form-item label="是否为ssl">
203203
<el-switch v-model="config.email['is-ssl']" />
204204
</el-form-item>
205+
<el-form-item label="是否LoginAuth认证">
206+
<el-switch v-model="config.email['is-loginauth']" />
207+
</el-form-item>
205208
<el-form-item label="secret">
206209
<el-input
207210
v-model.trim="config.email.secret"

0 commit comments

Comments
 (0)