1 Star 0 Fork 0

清汤兔子面 / tobox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gomail.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
agent_iyyn 提交于 2020-09-25 22:14 . first commit modules.
/*
@Time : 2020/6/2 11:13
@Author : wayos
@File : gomail.go
@Software: bill-service
*/
package mail
import (
"crypto/tls"
"gopkg.in/gomail.v2"
"strings"
"sync"
)
// Msg
type Msg struct {
Name string
User string
Password string
Host string
Port int
SendTo []string
Subject string
Body string
ContentType string
}
var (
// 邮件发送对象
mail *gomail.Message
// 邮件锁
lock *sync.Mutex
)
/*邮件内容类型*/
const (
// 文本内容
TextMail string = `text/plain`
// 前端内容
HtmlMail string = `text/html`
)
func init() {
mail = gomail.NewMessage()
lock = new(sync.Mutex)
}
// SendMail
func SendMail(msg *Msg) error {
lock.Lock()
defer lock.Unlock()
defer mail.Reset()
mail.SetHeader("From", mail.FormatAddress(msg.User, msg.Name))
mail.SetHeader("To", msg.SendTo...)
mail.SetHeader("Subject", msg.Subject)
mail.SetBody(parseType(msg.ContentType), msg.Body)
d := gomail.NewDialer(msg.Host, msg.Port, msg.User, msg.Password)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
return d.DialAndSend(mail)
}
// parseType
func parseType(mailType string) string {
var val string
mailType = strings.ToLower(mailType)
switch mailType {
case HtmlMail:
val = HtmlMail
default:
val = TextMail
}
return val
}
Go
1
https://gitee.com/agent_iyyn/tobox.git
git@gitee.com:agent_iyyn/tobox.git
agent_iyyn
tobox
tobox
v0.1.2

搜索帮助