2022-10-17 12:04:29 +00:00
|
|
|
package mail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2022-11-04 02:38:59 +00:00
|
|
|
"github/fthvgb1/wp-go/config"
|
2022-10-17 12:04:29 +00:00
|
|
|
"gopkg.in/gomail.v2"
|
|
|
|
"mime"
|
2022-11-16 07:46:55 +00:00
|
|
|
"path"
|
2022-10-17 12:04:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AttacheFile struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2022-11-16 07:46:55 +00:00
|
|
|
func SendMail(mailTo []string, subject string, body string, files ...string) error {
|
2022-10-17 12:04:29 +00:00
|
|
|
m := gomail.NewMessage(
|
|
|
|
gomail.SetEncoding(gomail.Base64),
|
|
|
|
)
|
2022-11-15 08:36:21 +00:00
|
|
|
c := config.Conf.Load()
|
2022-10-17 12:04:29 +00:00
|
|
|
m.SetHeader("From",
|
2022-11-15 08:36:21 +00:00
|
|
|
m.FormatAddress(c.Mail.User,
|
|
|
|
c.Mail.Alias,
|
2022-10-17 12:04:29 +00:00
|
|
|
))
|
|
|
|
m.SetHeader("To", mailTo...)
|
|
|
|
m.SetHeader("Subject", subject)
|
|
|
|
m.SetBody("text/html", body)
|
|
|
|
|
2022-11-16 07:46:55 +00:00
|
|
|
for _, file := range files {
|
|
|
|
_, f := path.Split(file)
|
|
|
|
m.Attach(file,
|
|
|
|
gomail.Rename(f), //重命名
|
2022-10-17 12:04:29 +00:00
|
|
|
gomail.SetHeader(map[string][]string{
|
|
|
|
"Content-Disposition": {
|
2022-11-16 07:46:55 +00:00
|
|
|
fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", f)),
|
2022-10-17 12:04:29 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
d := gomail.NewDialer(
|
2022-11-15 08:36:21 +00:00
|
|
|
c.Mail.Host,
|
|
|
|
c.Mail.Port,
|
|
|
|
c.Mail.User,
|
|
|
|
c.Mail.Pass,
|
2022-10-17 12:04:29 +00:00
|
|
|
)
|
2022-11-15 08:36:21 +00:00
|
|
|
if c.Mail.Ssl {
|
2022-10-17 12:04:29 +00:00
|
|
|
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
}
|
|
|
|
err := d.DialAndSend(m)
|
|
|
|
return err
|
|
|
|
}
|