增加利用程序发邮件能力
This commit is contained in:
parent
0140502988
commit
187b476fa6
4
go.mod
4
go.mod
@ -4,7 +4,7 @@ go 1.15
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/buger/jsonparser v1.1.1
|
github.com/buger/jsonparser v1.1.1
|
||||||
github.com/ddliu/go-httpclient v0.6.9 // indirect
|
github.com/ddliu/go-httpclient v0.6.9
|
||||||
github.com/gin-gonic/gin v1.7.2
|
github.com/gin-gonic/gin v1.7.2
|
||||||
github.com/go-redis/redis/v8 v8.3.4
|
github.com/go-redis/redis/v8 v8.3.4
|
||||||
github.com/go-redis/redis_rate/v9 v9.1.1
|
github.com/go-redis/redis_rate/v9 v9.1.1
|
||||||
@ -17,6 +17,8 @@ require (
|
|||||||
github.com/stretchr/testify v1.7.0
|
github.com/stretchr/testify v1.7.0
|
||||||
github.com/tidwall/gjson v1.8.1
|
github.com/tidwall/gjson v1.8.1
|
||||||
go.uber.org/zap v1.17.0
|
go.uber.org/zap v1.17.0
|
||||||
|
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||||
|
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
|
||||||
gopkg.in/yaml.v2 v2.3.0
|
gopkg.in/yaml.v2 v2.3.0
|
||||||
gorm.io/driver/mysql v1.0.0
|
gorm.io/driver/mysql v1.0.0
|
||||||
gorm.io/gorm v1.21.12
|
gorm.io/gorm v1.21.12
|
||||||
|
4
go.sum
4
go.sum
@ -144,9 +144,13 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
|
|||||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||||
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
|
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
|
||||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
|
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/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
68
mail/mail.go
Normal file
68
mail/mail.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
// Package mail 邮件发送配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 1:14 上午 2021/8/13
|
||||||
|
package mail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gopkg.in/gomail.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Mail 邮件服务实例
|
||||||
|
Mail *mail
|
||||||
|
)
|
||||||
|
|
||||||
|
type mail struct {
|
||||||
|
conf map[string]string
|
||||||
|
port int
|
||||||
|
mailDialer *gomail.Dialer
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMailMessage 获取邮件消息实例
|
||||||
|
//
|
||||||
|
// 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"])
|
||||||
|
}
|
||||||
|
return Mail
|
||||||
|
}
|
||||||
|
|
||||||
|
// BaseSend 基础邮件发送
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 1:14 上午 2021/8/13
|
||||||
|
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企业邮箱,以下方法用中文别名,会报错,需要用上面此方法转码
|
||||||
|
//m.SetHeader("From", "FB Sample"+"<"+mailConn["user"]+">") //这种方式可以添加别名,即“FB Sample”, 也可以直接用<code>m.SetHeader("From",mailConn["user"])</code> 读者可以自行实验下效果
|
||||||
|
//m.SetHeader("From", mailConn["user"])
|
||||||
|
message.SetHeader("To", mailTo...) //发送给多个用户
|
||||||
|
message.SetHeader("Subject", subject) //设置邮件主题
|
||||||
|
message.SetBody("text/html", body) //设置邮件正文
|
||||||
|
|
||||||
|
d := gomail.NewDialer(m.conf["host"], m.port, m.conf["user"], m.conf["pass"])
|
||||||
|
|
||||||
|
return d.DialAndSend(message)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user