/
smtp_notifier.go
154 lines (139 loc) · 3.47 KB
/
smtp_notifier.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package happening
import (
"crypto/tls"
"errors"
"fmt"
"log"
"net/smtp"
"net/url"
"strings"
)
type SMTPNotifier struct {
EnvironmentVariable string
DrilldownURL string
SMTPServerURL string
NoReplyName string
NoReplyEmail string
ContactName string
ContactEmail string
SkipSSLVerify bool
}
func NewSMTPNotifier(config ServerConfig) Notifier {
return &SMTPNotifier{
EnvironmentVariable: config.NOTIFIER_ENVIRONMENT_VARIABLE,
DrilldownURL: config.HAPPENING_SERVER_URL,
SMTPServerURL: config.NOTIFIER_SMTP_SERVER_URL,
NoReplyName: config.NOTIFIER_NO_REPLY_NAME,
NoReplyEmail: config.NOTIFIER_NO_REPLY_EMAIL,
ContactName: config.NOTIFIER_CONTACT_NAME,
ContactEmail: config.NOTIFIER_CONTACT_EMAIL,
}
}
func validateLine(line string) error {
if strings.ContainsAny(line, "\n\r") {
return errors.New("smtp: A line must not contain CR or LF")
}
return nil
}
func performSendMail(addr, serverName string, a smtp.Auth, from string, to string, msg []byte, skipVerify bool) error {
if err := validateLine(from); err != nil {
return err
}
if err := validateLine(to); err != nil {
return err
}
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
if ok, _ := c.Extension("STARTTLS"); ok {
config := &tls.Config{
ServerName: serverName,
InsecureSkipVerify: skipVerify,
}
if err = c.StartTLS(config); err != nil {
return err
}
}
if err = c.Mail(from); err != nil {
return err
}
if err = c.Rcpt(to); err != nil {
return err
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(msg)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
func (notifier *SMTPNotifier) sendMail(notifierMail NotifierMail) {
serverURL, err := url.Parse(notifier.SMTPServerURL)
if err != nil {
log.Panic(err)
}
skipVerify := false
if serverURL.Query().Get("sslVerify") == "disable" {
skipVerify = true
}
password, _ := serverURL.User.Password()
auth := smtp.PlainAuth("", serverURL.User.Username(), password, serverURL.Hostname())
toAddr := fmt.Sprintf("%s <%s>", notifier.ContactName, notifier.ContactEmail)
fromAddr := fmt.Sprintf("%s <%s>", notifier.NoReplyName, notifier.NoReplyEmail)
mail := fmt.Sprintf("From: %s\r\n", fromAddr)
mail += fmt.Sprintf("To: %s\r\n", toAddr)
mail += fmt.Sprintf("Subject: %s\r\n", notifierMail.Subject())
mail += "\r\n"
mail += notifierMail.Text()
mail += "\r\n"
err = performSendMail(
serverURL.Host,
serverURL.Hostname(),
auth,
notifier.NoReplyEmail,
notifier.ContactEmail,
[]byte(mail),
skipVerify,
)
if err != nil {
log.Printf("Sending mail to %s failed: %v", serverURL.Host, err)
}
}
func (notifier *SMTPNotifier) Alert(check Check) {
go notifier.sendMail(
CheckNotifierMail{
Check: check,
EnvironmentVariable: notifier.EnvironmentVariable,
DrilldownURL: notifier.DrilldownURL,
},
)
}
func (notifier *SMTPNotifier) Resolve(check Check) {
go notifier.sendMail(
CheckNotifierMail{
Check: check,
EnvironmentVariable: notifier.EnvironmentVariable,
DrilldownURL: notifier.DrilldownURL,
Resolved: true,
},
)
}
func (notifier *SMTPNotifier) Mail(event Event) {
go notifier.sendMail(
EventNotifierMail{
Event: event,
EnvironmentVariable: notifier.EnvironmentVariable,
DrilldownURL: notifier.DrilldownURL,
},
)
return
}