-
Notifications
You must be signed in to change notification settings - Fork 1
/
email_alarm.go
53 lines (43 loc) · 1.2 KB
/
email_alarm.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
package gerty
import (
"net/smtp"
)
// ensure we always implement Alarm
var _ Alarm = (*EmailAlarm)(nil)
type EmailAlarm struct {
Host string
Port string
User string
Pass string
From string
To string
Home string
}
func NewEmailAlarm(host, port, user, pass, from, to, home string) *EmailAlarm {
return &EmailAlarm{host, port, user, pass, from, to, home}
}
func (alarm EmailAlarm) Name() string {
return "Email Alarm"
}
func (alarm EmailAlarm) NotifyError(monitor Monitor) error {
auth := smtp.PlainAuth("", alarm.User, alarm.Pass, alarm.Host)
data := emailContents{alarm.From, alarm.To, alarm.Home,
monitor.Name(), monitor.Description()}
bytes, err := EmailTemplate(data)
if err != nil {
logger.Printf("error while creating the email template %v", err)
return err
}
address := alarm.Host + ":" + alarm.Port
logger.Printf("template generated, sending email to `%s`", address)
err = smtp.SendMail(address, auth, alarm.From, []string{alarm.To}, bytes)
if err != nil {
logger.Printf("error while sending the alarm email %v", err)
return err
}
return nil
}
// TODO: create email template with restore message.
func (alarm EmailAlarm) NotifyRestore(monitor Monitor) error {
return nil
}