🎨将邮箱smtp配置改为系统当中配置

This commit is contained in:
coward 2024-10-15 17:16:22 +08:00
parent fa04d9c83a
commit 331849522f
5 changed files with 34 additions and 19 deletions

View File

@ -7,6 +7,5 @@ type config struct {
Database *database `yaml:"database"` Database *database `yaml:"database"`
Cache *cache `yaml:"redis"` Cache *cache `yaml:"redis"`
File *file `yaml:"file"` File *file `yaml:"file"`
Mail *mail `yaml:"email"`
Wireguard *wireguard `yaml:"wireguard"` Wireguard *wireguard `yaml:"wireguard"`
} }

View File

@ -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"`
}

View File

@ -243,7 +243,14 @@ func (ClientApi) Download(c *gin.Context) {
return 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 { if err != nil {
response.R(c).FailedWithError("发送邮件失败") response.R(c).FailedWithError("发送邮件失败")
return return

View File

@ -90,6 +90,17 @@ func (s setting) GetAllSetting(blackList []string) (data []vo.SettingItem, err e
return 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 // Export
// @description: 导出 // @description: 导出
// @receiver s // @receiver s

View File

@ -2,28 +2,35 @@ package utils
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/jordan-wright/email" "github.com/jordan-wright/email"
"github.com/spf13/cast"
"mime" "mime"
"net/smtp" "net/smtp"
"net/textproto" "net/textproto"
"path/filepath" "path/filepath"
"wireguard-ui/config" "wireguard-ui/model"
) )
type mail struct { type mail struct {
*email.Email *email.Email
addr string addr string
auth smtp.Auth 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 var m mail
em := email.NewEmail() em := email.NewEmail()
m.Email = em m.Email = em
m.auth = smtp.PlainAuth("", config.Config.Mail.User, config.Config.Mail.Password, config.Config.Mail.Host) m.auth = smtp.PlainAuth("", mailConf["user"], mailConf["password"], mailConf["host"])
m.addr = fmt.Sprintf("%s:%d", config.Config.Mail.Host, config.Config.Mail.Port) m.addr = fmt.Sprintf("%s:%s", mailConf["host"], mailConf["port"])
m.conf = mailConf
return &m return &m
} }
@ -47,7 +54,7 @@ func (m *mail) VerifyConfig() (err error) {
// @param attacheFilePath // @param attacheFilePath
// @return err // @return err
func (m *mail) SendMail(to, subject, content, attacheFilePath string) (err error) { 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.To = []string{to}
m.Subject = subject m.Subject = subject
m.Text = []byte(content) m.Text = []byte(content)
@ -61,13 +68,13 @@ func (m *mail) SendMail(to, subject, content, attacheFilePath string) (err error
atch.Header = emHeader atch.Header = emHeader
} }
if config.Config.Mail.SkipTls { if cast.ToBool(m.conf["skipTls"]) {
return m.Send(m.addr, m.auth) return m.Send(m.addr, m.auth)
} }
tlsConfig := &tls.Config{} tlsConfig := &tls.Config{}
tlsConfig.InsecureSkipVerify = config.Config.Mail.SkipTls tlsConfig.InsecureSkipVerify = cast.ToBool(m.conf["skipTls"])
tlsConfig.ServerName = config.Config.Mail.Host tlsConfig.ServerName = m.conf["host"]
return m.SendWithTLS(m.addr, m.auth, tlsConfig) return m.SendWithTLS(m.addr, m.auth, tlsConfig)
} }