1 Star 0 Fork 0

coolops/pandora

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sendmail.go 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
nanxuezhi 提交于 2019-06-19 19:57 . init
// Copyright 2019 pandora Author. All Rights Reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package pandora
import (
"strings"
"gopkg.in/gomail.v2"
)
type SendMail struct {
Enable int
Smtp string
Port int
User string
Pass string
dialer *gomail.Dialer
}
func NewSendMail(mail *SendMail) *SendMail {
mail.dialer = gomail.NewPlainDialer(mail.Smtp, mail.Port, mail.User, mail.Pass)
return mail
}
func (mail *SendMail) send(msg *SendMailMessage) error {
if mail.Enable == 0 {
return nil
}
msg.mail = mail
m := msg.NewMessage()
if err := mail.dialer.DialAndSend(m); err != nil {
return err
}
return nil
}
func (mail *SendMail) Send(msg *SendMailMessage) {
if err := mail.send(msg); err != nil {
App.Logger.Error(
"send mail failed, to[%s], cc[%s], subject[%s]",
strings.Join(msg.To, ","),
strings.Join(msg.Cc, ","),
msg.Subject,
)
}
}
func (mail *SendMail) AsyncSend(msg *SendMailMessage) {
go mail.Send(msg)
}
type SendMailMessage struct {
From string
To []string
Cc []string
Subject string
Body string
Attach string
mail *SendMail
}
func (m *SendMailMessage) NewMessage() *gomail.Message {
mailMsg := gomail.NewMessage()
if m.From == "" {
mailMsg.SetHeader("From", m.mail.User)
} else {
mailMsg.SetHeader("From", m.From)
}
mailMsg.SetHeader("To", m.To...)
if len(m.Cc) > 0 {
mailMsg.SetHeader("Cc", m.Cc...)
}
mailMsg.SetHeader("Subject", m.Subject)
mailMsg.SetBody("text/html", m.Body)
if m.Attach != "" {
mailMsg.Attach(m.Attach)
}
return mailMsg
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/coolops/pandora.git
git@gitee.com:coolops/pandora.git
coolops
pandora
pandora
master

搜索帮助