🆕新增了邮件工具

This commit is contained in:
coward 2024-03-22 11:40:34 +08:00
parent 86c76add17
commit 8bcd3266d6
5 changed files with 106 additions and 5 deletions

View File

@ -3,9 +3,10 @@ package config
var Config *config
type config struct {
Http *http `yaml:"http"`
Database *database `yaml:"database"`
Redis *redis `yaml:"redis"`
File *file `yaml:"file"`
Wireguard wireguard `yaml:"wireguard"`
Http *http `yaml:"http"`
Database *database `yaml:"database"`
Redis *redis `yaml:"redis"`
File *file `yaml:"file"`
Mail *mail `yaml:"mail"`
Wireguard *wireguard `yaml:"wireguard"`
}

9
config/mail.go Normal file
View File

@ -0,0 +1,9 @@
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"`
}

2
go.mod
View File

@ -97,5 +97,7 @@ require (
google.golang.org/genproto v0.0.0-20221018160656-63c7b68cfc55 // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

4
go.sum
View File

@ -1266,6 +1266,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@ -1277,6 +1279,8 @@ gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/fsnotify/fsnotify.v1 v1.4.7/go.mod h1:Fyux9zXlo4rWoMSIzpn9fDAYjalPqJ/K1qJ27s+7ltE=
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=

85
utils/mail.go Normal file
View File

@ -0,0 +1,85 @@
package utils
import (
"crypto/tls"
"fmt"
"gitee.ltd/lxh/logger/log"
"gopkg.in/gomail.v2"
"mime"
"path/filepath"
"wireguard-dashboard/config"
)
type mail struct {
md *gomail.Dialer
}
func Mail() mail {
mailDialer := gomail.NewDialer(config.Config.Mail.Host, config.Config.Mail.Port, config.Config.Mail.User, config.Config.Mail.Password)
mailDialer.TLSConfig = &tls.Config{
InsecureSkipVerify: config.Config.Mail.SkipTls,
}
return mail{mailDialer}
}
// SendMail
// @description: 发送普通邮件
// @receiver mail
// @param subject
// @param toAddress
// @param content
// @return err
func (m mail) SendMail(subject, toAddress, content string) (err error) {
msg := gomail.NewMessage()
msg.SetHeader("From", msg.FormatAddress(m.md.Username, "wireguard-dashboard"))
msg.SetHeader("To", toAddress)
msg.SetHeader("Subject", subject)
msg.SetBody("text/plain", content)
if err = m.md.DialAndSend(msg); err != nil {
log.Errorf("发送普通邮件失败: %v", err.Error())
return
}
return
}
// SendMailWithAttach
// @description: 发送并携带附件
// @receiver m
// @param subject
// @param toAddress
// @param content
// @param attachPath
// @return err
func (m mail) SendMailWithAttach(subject, toAddress, content, attachPath string) (err error) {
msg := gomail.NewMessage()
msg.SetHeader("From", msg.FormatAddress(m.md.Username, "wireguard-dashboard"))
msg.SetHeader("To", toAddress)
msg.SetHeader("Subject", subject)
msg.SetBody("text/plain", content)
rename := m.getFileName(attachPath)
msg.Attach(attachPath, gomail.Rename(rename), gomail.SetHeader(map[string][]string{
"Content-Disposition": {
fmt.Sprintf(`attachment; filename="%s"`, mime.BEncoding.Encode("UTF-8", rename)),
},
}))
if err = m.md.DialAndSend(msg); err != nil {
log.Errorf("发送普通邮件失败: %v", err.Error())
return
}
return
}
// getFileName
// @description: 获取文件名
// @receiver m
// @param filePath
// @return string
func (m mail) getFileName(filePath string) string {
return filepath.Base(filePath)
}