/
mailer.go
65 lines (56 loc) · 1.51 KB
/
mailer.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
package webwatch
import (
"fmt"
"net/smtp"
"strings"
"time"
)
// A Mailer sends emails.
type Mailer interface {
// SendRestarted sends a 'restarted' mail.
SendRestarted(now time.Time)
// SendReport sends a 'OK' or 'ERR' mail.
SendReport(now time.Time, ok bool, text string)
}
// NewMailer creates Mailer with a configuration.
func NewMailer(config MailConfig) Mailer {
return mailerImpl{config}
}
type mailerImpl struct {
config MailConfig
}
func (mi mailerImpl) SendRestarted(now time.Time) {
tstamp := now.UTC().Format(time.RFC3339)
body := fmt.Sprintf("%s\r\n", tstamp)
mi.send("restarted", body)
}
func (mi mailerImpl) SendReport(now time.Time, ok bool, text string) {
suffix := ""
if ok {
suffix = "OK"
} else {
suffix = "ERR"
}
tstamp := now.UTC().Format(time.RFC3339)
body := fmt.Sprintf("%s\r\n\r\n%s\r\n", tstamp, text)
mi.send(suffix, body)
}
func (mi mailerImpl) send(suffix, body string) {
subject := mi.config.Subject + " " + suffix
subject = strings.TrimSpace(subject)
INFO.Printf("send mail %q to %q\n", subject, mi.config.To)
auth := smtp.PlainAuth("", mi.config.Username, mi.config.Password, mi.config.Host)
addr := mi.config.Host + ":25"
msg := "To: " + mi.config.To + "\r\n" +
"Subject: " + subject + "\r\n" +
"Content-Type: text/plain; charset=utf-8\r\n" +
"\r\n" +
body + "\r\n" +
"\r\n"
err := smtp.SendMail(addr, auth, mi.config.From, []string{mi.config.To}, []byte(msg))
if err != nil {
INFO.Printf("cannot send mail: %s\n", err)
return
}
INFO.Printf("mail sent\n")
}