package logger
import (
"strconv"
"webgame/modules/srvtime"
"github.com/astaxie/beego"
"gopkg.in/gomail.v2"
)
var host = "smtp.163.com" // smtp服务host
var port = 465 // ssl端口
var user = "hjj.gemini@163.com" // 发送方邮箱
var pass = "top1studiomail" // 授权码
//var dialer *gomail.Dialer
// 接收人
var to = []string{
"xxx@bianfeng.com",
"xxx@bianfeng.com",
//var to = []string{"huangjiajia@bianfeng.com"}
var errorMessage = ""
var iosHttpsErrorTimes = 0
//func init() {
// Info("logMail init success .")
//}
var timesBySubject = make(map[string]int)
func SendMail(subject string, body string) {
// 开发模式下不发邮件
if beego.BConfig.RunMode == "dev" {
return
}
// ios http访问错误,只计数,不触发邮件
if body == "invalid character '<' looking for beginning of value" {
iosHttpsErrorTimes++
return
}
// 对相同subject 进行累加计数
if _, ok := timesBySubject[subject]; ok {
timesBySubject[subject]++
} else {
timesBySubject[subject] = 1
}
errorMessage += subject + " " + strconv.Itoa(timesBySubject[subject]) + " " + srvtime.Now().String() + " " + body + "<br>"
// 第一次 第十次 第一百次 第一千次 第一万次 发送邮件
if timesBySubject[subject] != 1 &&
timesBySubject[subject] != 10 &&
timesBySubject[subject] != 30 &&
timesBySubject[subject] != 100 &&
timesBySubject[subject] != 1000 &&
timesBySubject[subject] != 10000 {
return
}
dialer := gomail.NewDialer(host, port, user, pass)
m := gomail.NewMessage()
m.SetHeader("From", "Game Server Mail"+"<"+user+">")
m.SetHeader("To", to...)
if timesBySubject[subject] != 1 {
subject += " " + strconv.Itoa(timesBySubject[subject]) + "+"
}
var sendBody = errorMessage + "Others: iosHttpsErrorTimes = " + strconv.Itoa(iosHttpsErrorTimes) + "<br>"
m.SetHeader("Subject", subject) //设置邮件主题
m.SetBody("text/html", sendBody) //设置邮件正文
dialer.DialAndSend(m)
}
go发送邮件
最新推荐文章于 2024-06-06 09:00:00 发布