/
gmail.go
64 lines (50 loc) · 1.33 KB
/
gmail.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
package notifiers
import (
"errors"
"net/smtp"
"strings"
)
func init() {
registerNotifier("gmail", NewGmailNotifier)
}
type Gmail struct {
name string
user string
pass string
notificationAddresses []string
}
var NewGmailNotifier = func(config Config) (Notifier, error) {
if config.Type != "gmail" {
return nil, errors.New("gmail: invalid config type")
}
if config.Config["user"] == "" {
return nil, errors.New("gmail: invalid user")
}
if config.Config["pass"] == "" {
return nil, errors.New("gmail: invalid pass")
}
if config.Config["notification_addresses"] == "" {
return nil, errors.New("gmail: invalid notification addresses")
}
return Notifier(Gmail{
name: config.Name,
user: config.Config["user"],
pass: config.Config["pass"],
notificationAddresses: strings.Split(config.Config["notification_addresses"], ","),
}), nil
}
func (a Gmail) Name() string {
return a.name
}
func (a Gmail) Notify(msg Message) error {
body := "To: " + strings.Join(a.notificationAddresses, ",") +
"\r\nSubject: " + msg.DefaultMessage +
"\r\n\r\n" + msg.DefaultMessage
auth := smtp.PlainAuth("", a.user, a.pass, "smtp.gmail.com")
err := smtp.SendMail("smtp.gmail.com:587", auth, a.user,
a.notificationAddresses, []byte(body))
if err != nil {
return err
}
return nil
}