/
smtp.go
52 lines (43 loc) · 1.25 KB
/
smtp.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
package reporter
import (
"fmt"
"github.com/DMKEBUSINESSGMBH/dmkhunter/model"
"net/smtp"
)
type MessageFormatter struct {
topic string
}
func (f MessageFormatter) Format(stack model.ViolationStack) []byte {
msgBody := ""
header := "Subject: Alert from %s (%d changes)\r\n\r\n"
header = fmt.Sprintf(header, f.topic, len(stack.All()))
for _, violation := range stack.All() {
msgBody += violation.Message + "\r\n"
}
return []byte(header + msgBody)
}
func NewSmtpReporter(username string, passwords string, host string, recipients []string, fromAddress string, topic string) SmtpReporter {
return SmtpReporter{
username: username,
password: passwords,
host: host,
recipientList: recipients,
from: fromAddress,
topic: topic,
}
}
type SmtpReporter struct {
username string
password string
host string
recipientList []string
from string
topic string
}
func (s SmtpReporter) Send(stack model.ViolationStack) error {
formatter := MessageFormatter{topic: s.topic}
auth := smtp.CRAMMD5Auth(s.username, s.password)
//auth := smtp.PlainAuth("", s.username, s.password, s.host)
err := smtp.SendMail(s.host, auth, s.from, s.recipientList, formatter.Format(stack))
return err
}