diff --git a/config/config.go b/config/config.go index 29d36c0..06545c7 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,5 @@ type config struct { Database *database `yaml:"database"` Cache *cache `yaml:"redis"` File *file `yaml:"file"` - Mail *mail `yaml:"email"` Wireguard *wireguard `yaml:"wireguard"` } diff --git a/config/mail.go b/config/mail.go deleted file mode 100644 index 465265b..0000000 --- a/config/mail.go +++ /dev/null @@ -1,9 +0,0 @@ -package config - -type mail struct { - Host string `json:"host" yaml:"host"` - Port int `json:"port" yaml:"port"` - User string `json:"user" yaml:"user"` - Password string `json:"password" yaml:"password"` - SkipTls bool `json:"skipTls" yaml:"skipTls"` -} diff --git a/http/api/client.go b/http/api/client.go index e63a34d..5ace1fc 100644 --- a/http/api/client.go +++ b/http/api/client.go @@ -243,7 +243,14 @@ func (ClientApi) Download(c *gin.Context) { return } - err = utils.Mail().SendMail(data.Email, fmt.Sprintf("客户端: %s", data.Name), "请查收附件", outPath) + // 获取邮箱配置 + emailConf, err := service.Setting().GetByCode("EMAIL_SMTP") + if err != nil { + response.R(c).FailedWithError("获取邮箱配置失败,请先到设置页面的【其他】里面添加code为【EMAIL_SMTP】的具体配置") + return + } + + err = utils.Mail(emailConf).SendMail(data.Email, fmt.Sprintf("客户端: %s", data.Name), "请查收附件", outPath) if err != nil { response.R(c).FailedWithError("发送邮件失败") return diff --git a/service/setting.go b/service/setting.go index 0764f5f..e5e62e0 100644 --- a/service/setting.go +++ b/service/setting.go @@ -90,6 +90,17 @@ func (s setting) GetAllSetting(blackList []string) (data []vo.SettingItem, err e return } +// GetByCode +// @description: 获取指定code的配置 +// @receiver s +// @param code +// @return data +// @return err +func (s setting) GetByCode(code string) (data *model.Setting, err error) { + err = s.Model(&model.Setting{}).Where("code = ?", code).Take(&data).Error + return +} + // Export // @description: 导出 // @receiver s diff --git a/utils/mail.go b/utils/mail.go index 636790e..7ec1012 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -2,28 +2,35 @@ package utils import ( "crypto/tls" + "encoding/json" "errors" "fmt" "github.com/jordan-wright/email" + "github.com/spf13/cast" "mime" "net/smtp" "net/textproto" "path/filepath" - "wireguard-ui/config" + "wireguard-ui/model" ) type mail struct { *email.Email addr string auth smtp.Auth + conf map[string]string } -func Mail() *mail { +func Mail(conf *model.Setting) *mail { + // 解析配置文件 + var mailConf = make(map[string]string) + _ = json.Unmarshal([]byte(conf.Data), &mailConf) var m mail em := email.NewEmail() m.Email = em - m.auth = smtp.PlainAuth("", config.Config.Mail.User, config.Config.Mail.Password, config.Config.Mail.Host) - m.addr = fmt.Sprintf("%s:%d", config.Config.Mail.Host, config.Config.Mail.Port) + m.auth = smtp.PlainAuth("", mailConf["user"], mailConf["password"], mailConf["host"]) + m.addr = fmt.Sprintf("%s:%s", mailConf["host"], mailConf["port"]) + m.conf = mailConf return &m } @@ -47,7 +54,7 @@ func (m *mail) VerifyConfig() (err error) { // @param attacheFilePath // @return err func (m *mail) SendMail(to, subject, content, attacheFilePath string) (err error) { - m.From = fmt.Sprintf("wg-dashboard <%s>", config.Config.Mail.User) + m.From = fmt.Sprintf("wg-dashboard <%s>", m.conf["user"]) m.To = []string{to} m.Subject = subject m.Text = []byte(content) @@ -61,13 +68,13 @@ func (m *mail) SendMail(to, subject, content, attacheFilePath string) (err error atch.Header = emHeader } - if config.Config.Mail.SkipTls { + if cast.ToBool(m.conf["skipTls"]) { return m.Send(m.addr, m.auth) } tlsConfig := &tls.Config{} - tlsConfig.InsecureSkipVerify = config.Config.Mail.SkipTls - tlsConfig.ServerName = config.Config.Mail.Host + tlsConfig.InsecureSkipVerify = cast.ToBool(m.conf["skipTls"]) + tlsConfig.ServerName = m.conf["host"] return m.SendWithTLS(m.addr, m.auth, tlsConfig) }