调整mail实例

This commit is contained in:
白茶清欢 2021-08-14 18:02:54 +08:00
parent ad91ec88c7
commit 90bc05e1d0

View File

@ -14,12 +14,7 @@ import (
"gopkg.in/gomail.v2"
)
var (
// Mail 邮件服务实例
Mail *mail
)
type mail struct {
type Mail struct {
conf map[string]string
port int
mailDialer *gomail.Dialer
@ -30,24 +25,22 @@ type mail struct {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 1:32 上午 2021/8/13
func NewMailMessage(fromMail string, fromMailName string, pass string, host string, port int) *mail {
if Mail == nil {
Mail = &mail{}
if len(fromMailName) == 0 {
fromMailName = "系统邮件"
}
Mail.conf = map[string]string{
"user": fromMail,
"pass": pass,
"host": host,
"port": fmt.Sprintf("%v", port),
"from_mail_name": fromMailName,
}
Mail.port = port
Mail.mailDialer = gomail.NewDialer(Mail.conf["host"], port, Mail.conf["user"], Mail.conf["pass"])
func NewMailMessage(fromMail string, fromMailName string, pass string, host string, port int) *Mail {
m := &Mail{}
if len(fromMailName) == 0 {
fromMailName = "系统邮件"
}
return Mail
m.conf = map[string]string{
"user": fromMail,
"pass": pass,
"host": host,
"port": fmt.Sprintf("%v", port),
"from_mail_name": fromMailName,
}
m.port = port
m.mailDialer = gomail.NewDialer(m.conf["host"], port, m.conf["user"], m.conf["pass"])
return m
}
// BaseSend 基础邮件发送
@ -55,7 +48,7 @@ func NewMailMessage(fromMail string, fromMailName string, pass string, host stri
// Author : go_developer@163.com<白茶清欢>
//
// Date : 1:14 上午 2021/8/13
func (m *mail) BaseSend(mailTo []string, subject string, body string) error {
func (m *Mail) BaseSend(mailTo []string, subject string, body string) error {
message := gomail.NewMessage()
message.SetHeader("From", message.FormatAddress(m.conf["user"], m.conf["from_mail_name"])) //这种方式可以添加别名即“XX官方”
//说明如果是用网易邮箱账号发送以下方法别名可以是中文如果是qq企业邮箱以下方法用中文别名会报错需要用上面此方法转码
@ -75,7 +68,7 @@ func (m *mail) BaseSend(mailTo []string, subject string, body string) error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 1:05 下午 2021/8/14
func (m *mail) TplSend(mailTo []string, subject string, tpl string, bindData map[string]string) error {
func (m *Mail) TplSend(mailTo []string, subject string, tpl string, bindData map[string]string) error {
var (
tplContent []byte
err error