forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail.go
More file actions
68 lines (56 loc) · 1.79 KB
/
gmail.go
File metadata and controls
68 lines (56 loc) · 1.79 KB
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
package checkup
import (
"bytes"
"crypto/tls"
"fmt"
"log"
"strings"
gomail "gopkg.in/gomail.v2"
)
// Gmail consist of all the sub components required to use gomail API
type Gmail struct {
Name string `json:"name"`
Username string `json:"username"`
Password string `json:"password"`
MailList string `json:"list"`
}
// Notify implements notifier interface
func (s Gmail) Notify(results []Result) error {
for _, result := range results {
if !result.Healthy {
s.Send(result)
}
}
return nil
}
func (s Gmail) NotifyAll(results []Result) error {
var result Result
var buf bytes.Buffer
buf.WriteString("<html lang=\"en\" encoding=\"utf-8\"><head><title>cybavo daily report</title></head><TABLE width=\"70%\" frame=\"none\" border=\"1\"><body><TR><TH>Title</TH> <TH>Endpoint</TH> <TH>status</TH></TR>")
for _, resultitem := range results {
tmp := fmt.Sprintf("<TR><TH>%s</TH><TH>%s </TH> <TH>%s</TH></TR>\n", resultitem.Title, resultitem.Endpoint, string(resultitem.Status()))
buf.WriteString(tmp)
}
buf.WriteString("</TABLE></body></html>")
result.Title = "Health Check status report."
result.Message = buf.String()
result.Healthy = true
s.Send(result)
return nil
}
// Send request via gomail API to create incident
func (s Gmail) Send(result Result) error {
d := gomail.NewDialer("smtp.gmail.com", 465, s.Username, s.Password)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
m := gomail.NewMessage()
m.SetHeader("From", s.Username)
m.SetHeader("To", strings.Split(s.MailList, ",")...)
m.SetHeader("Subject", fmt.Sprintf("%s: %s is %s", result.Title, result.Endpoint, string(result.Status())))
m.SetBody("text/html", result.Message)
err := d.DialAndSend(m)
if err != nil {
log.Printf("ERROR: %s", err)
}
log.Printf("Create request for %s", result.Endpoint)
return nil
}