-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
83 lines (72 loc) · 1.96 KB
/
mail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cron
import (
"strings"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/library/cron/send"
"github.com/admpub/nging/application/library/cron/writer"
alertRegistry "github.com/admpub/nging/application/registry/alert"
"github.com/webx-top/echo/defaults"
"github.com/webx-top/echo/param"
)
var (
// SendMail 发送Email
// @param toEmail 收信邮箱
// @param toUsername 收信人名称
// @param title 邮件标题
// @param content 邮件内容
// @param ccList 抄送地址
SendMail = send.Mail
// SendMailWithID 发送Email(带ID参数)
SendMailWithID = send.MailWithID
// SendMailWithNoticer 发送Email(带Noticer参数)
SendMailWithNoticer = send.MailWithNoticer
// SendMailWithIDAndNoticer 发送Email(带ID和Noticer参数)
SendMailWithIDAndNoticer = send.MailWithIDAndNoticer
NewCmdRec = writer.New
)
type OutputWriter = writer.OutputWriter
func OtherSender(params param.Store) error {
if alertRegistry.SendTopic == nil {
return nil
}
ctx := defaults.NewMockContext()
return alertRegistry.SendTopic(ctx, `cron`, params)
}
func EmailSender(params param.Store) error {
task, ok := params.Get(`task`).(dbschema.NgingTask)
if !ok {
return nil
}
var ccList []string
if len(task.NotifyEmail) > 0 {
ccList = strings.Split(task.NotifyEmail, "\n")
for index, email := range ccList {
email = strings.TrimSpace(email)
if len(email) == 0 {
continue
}
ccList[index] = email
}
}
if len(ccList) == 0 {
return nil
}
toEmail := ccList[0]
toUsername := strings.SplitN(toEmail, "@", 2)[0]
if len(ccList) > 1 {
ccList = append([]string{}, ccList[1:]...)
} else {
ccList = []string{}
}
ct, ok := params.Get(`content`).(send.ContentType)
if !ok {
return nil
}
content := ct.EmailContent(params)
return SendMail(toEmail, toUsername, params.String(`title`), content, ccList...)
}
func init() {
AddSender(EmailSender)
AddSender(OtherSender)
alertRegistry.Topics.Add(`cron`, `定时任务`)
}