1 Star 0 Fork 233

其实很简单/syncd

forked from dreamans/syncd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sendmail.go 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
dreamans 提交于 2019-01-10 22:24 . 修改包目录
// Copyright 2018 syncd 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 syncd
import (
"gopkg.in/gomail.v2"
)
type SendMail struct {
Enable int
Smtp string
Port int
User string
Pass string
dialer *gomail.Dialer
}
type SendMailMessage struct {
From string
To []string
Cc []string
Subject string
Body string
Attach string
mail *SendMail
}
func SendMailNew(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 (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 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/apersonlee/syncd.git
git@gitee.com:apersonlee/syncd.git
apersonlee
syncd
syncd
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385