From 90bc05e1d0c854724efc316a5cd2b81deea19e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 14 Aug 2021 18:02:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4mail=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mail/mail.go | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/mail/mail.go b/mail/mail.go index dbfb614..8fba041 100644 --- a/mail/mail.go +++ b/mail/mail.go @@ -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