58 Star 193 Fork 74

hidu/mysql-schema-sync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
email.go 2.40 KB
一键复制 编辑 原始数据 按行查看 历史
hidu 提交于 2015-10-29 23:12 +08:00 . use internal
package internal
import (
"fmt"
"log"
"net/smtp"
"strings"
)
// EmailStruct email conf info
type EmailStruct struct {
SendMailAble bool `json:"send_mail"`
SMTPHost string `json:"smtp_host"`
From string `json:"from"`
Password string `json:"password"`
To string `json:"to"`
}
const tableStyle = `
<sTyle type='text/css'>
table {border-collapse: collapse;border-spacing: 0;}
.tb_1{border:1px solid #cccccc;table-layout:fixed;word-break:break-all;width: 100%;background:#ffffff;margin-bottom:5px}
.tb_1 caption{text-align: center;background: #F0F4F6;font-weight: bold;padding-top: 5px;height: 25px;border:1px solid #cccccc;border-bottom:none}
.tb_1 a{margin:0 3px 0 3px}
.tb_1 tr th,.tb_1 tr td{padding: 3px;border:1px solid #cccccc;line-height:20px}
.tb_1 thead tr th{font-weight:bold;text-align: center;background:#e3eaee}
.tb_1 tbody tr th{text-align: right;background:#f0f4f6;padding-right:5px}
.tb_1 tfoot{color:#cccccc}
.td_c td{text-align: center}
.td_r td{text-align: right}
.t_c{text-align: center !important;}
.t_r{text-align: right !important;}
.t_l{text-align: left !important;}
</stYle>
`
// SendMail send mail
func (m *EmailStruct) SendMail(title string, body string) {
if !m.SendMailAble {
log.Println("disbale send email")
return
}
if m.SMTPHost == "" || m.From == "" || m.To == "" {
log.Println("smtp_host,from,to is empty")
return
}
addrInfo := strings.Split(m.SMTPHost, ":")
if len(addrInfo) != 2 {
log.Println("smtp_host wrong,eg: host_name:25")
return
}
auth := smtp.PlainAuth("", m.From, m.Password, addrInfo[0])
_sendTo := strings.Split(m.To, ";")
var sendTo []string
for _, _to := range _sendTo {
_to = strings.TrimSpace(_to)
if _to != "" && strings.Contains(_to, "@") {
sendTo = append(sendTo, _to)
}
}
if len(sendTo) < 1 {
log.Println("mail receiver is empty")
return
}
body = tableStyle + "\n" + body
body += "<br/><hr style='border:none;border-top:1px solid #ccc'/><center>Powered by <a href='" + AppURL + "'>mysql-schema-sync</a>&nbsp;" + Version + "</center>"
msgBody := fmt.Sprintf("To: %s\r\nContent-Type: text/html;charset=utf-8\r\nSubject: %s\r\n\r\n%s", strings.Join(sendTo, ";"), title, body)
err := smtp.SendMail(m.SMTPHost, auth, m.From, sendTo, []byte(msgBody))
if err == nil {
log.Println("send mail success")
} else {
log.Println("send mail failed,err:", err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hidu/mysql-schema-sync.git
git@gitee.com:hidu/mysql-schema-sync.git
hidu
mysql-schema-sync
mysql-schema-sync
21680d8e8e24

搜索帮助