add: 提交
This commit is contained in:
111
features/fm/email.go
Normal file
111
features/fm/email.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package fm
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"ems.agt/lib/dborm"
|
||||
"ems.agt/lib/log"
|
||||
"ems.agt/restagent/config"
|
||||
|
||||
"gopkg.in/gomail.v2"
|
||||
)
|
||||
|
||||
func AlarmEmailForward(alarmData *Alarm) error {
|
||||
log.Info("AlarmEmailForward processing... ")
|
||||
|
||||
message := `
|
||||
<p> Hello information,</p>
|
||||
test, test
|
||||
<p style="text-indent:2em">Best Wishes!</p>
|
||||
`
|
||||
|
||||
// QQ 邮箱:
|
||||
// SMTP 服务器地址:smtp.qq.com(SSL协议端口:465/994 | 非SSL协议端口:25)
|
||||
// 163 邮箱:
|
||||
// SMTP 服务器地址:smtp.163.com(端口:25)
|
||||
// host := "mail.agrandtech.com"
|
||||
// port := 25
|
||||
// userName := "smtpext@agrandtech.com"
|
||||
// password := "1000smtp@omc!"
|
||||
|
||||
host := config.GetYamlConfig().Alarm.Email.Smtp
|
||||
port := int(config.GetYamlConfig().Alarm.Email.Port)
|
||||
userName := config.GetYamlConfig().Alarm.Email.User
|
||||
password := config.GetYamlConfig().Alarm.Email.Password
|
||||
|
||||
m := gomail.NewMessage()
|
||||
m.SetHeader("From", userName) // 发件人
|
||||
//m.SetHeader("From", "alias"+"<"+"aliastest"+">") // 增加发件人别名
|
||||
|
||||
emails, err := dborm.XormGetAlarmForward("Email")
|
||||
if err != nil {
|
||||
log.Error("Failed to XormGetAlarmForward:", err)
|
||||
return err
|
||||
} else if emails == nil || len(*emails) == 0 {
|
||||
err := errors.New("not found forward email list")
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
forwardLog := &dborm.AlarmForwardLog{
|
||||
NeType: alarmData.NeType,
|
||||
NeID: alarmData.NeId,
|
||||
AlarmID: alarmData.AlarmId,
|
||||
AlarmTitle: alarmData.AlarmTitle,
|
||||
AlarmSeq: alarmData.AlarmSeq,
|
||||
EventTime: alarmData.EventTime,
|
||||
ToUser: strings.Join(*emails, ","),
|
||||
}
|
||||
for _, email := range *emails {
|
||||
m.SetHeader("To", email) // 收件人,可以多个收件人,但必须使用相同的 SMTP 连接
|
||||
}
|
||||
|
||||
//m.SetHeader("To", "zhangshuzhong@agrandtech.com", "simonzhangsz@outlook.com") // 收件人,可以多个收件人,但必须使用相同的 SMTP 连接
|
||||
//m.SetHeader("Cc", "******@qq.com") // 抄送,可以多个
|
||||
//m.SetHeader("Bcc", "******@qq.com") // 暗送,可以多个
|
||||
m.SetHeader("Subject", "Alarm from OMC!") // 邮件主题
|
||||
|
||||
// text/html 的意思是将文件的 content-type 设置为 text/html 的形式,浏览器在获取到这种文件时会自动调用html的解析器对文件进行相应的处理。
|
||||
// 可以通过 text/html 处理文本格式进行特殊处理,如换行、缩进、加粗等等
|
||||
//m.SetBody("text/html", fmt.Sprintf(message, *alarm))
|
||||
m.SetBody("text/html", message)
|
||||
|
||||
// text/plain的意思是将文件设置为纯文本的形式,浏览器在获取到这种文件时并不会对其进行处理
|
||||
// m.SetBody("text/plain", "纯文本")
|
||||
// m.Attach("test.sh") // 附件文件,可以是文件,照片,视频等等
|
||||
// m.Attach("lolcatVideo.mp4") // 视频
|
||||
// m.Attach("lolcat.jpg") // 照片
|
||||
|
||||
d := gomail.NewDialer(
|
||||
host,
|
||||
port,
|
||||
userName,
|
||||
password,
|
||||
)
|
||||
// 关闭SSL协议认证
|
||||
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
|
||||
if err := d.DialAndSend(m); err != nil {
|
||||
operResult := fmt.Sprintf("Failed to DialAndSend:%v", err)
|
||||
log.Error(operResult)
|
||||
forwardLog.OperResult = operResult
|
||||
affected, err := dborm.XormInsertAlarmForwardLog(forwardLog)
|
||||
if err != nil && affected <= 0 {
|
||||
log.Error("Failed to insert data:", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
operResult := fmt.Sprintf("Email sent successfully!:", err)
|
||||
log.Error(operResult)
|
||||
forwardLog.OperResult = operResult
|
||||
affected, err := dborm.XormInsertAlarmForwardLog(forwardLog)
|
||||
if err != nil && affected <= 0 {
|
||||
log.Error("Failed to insert data:", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user