-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmailer.go
40 lines (34 loc) · 990 Bytes
/
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
package main
import (
"fmt"
"net/smtp"
"strings"
"sync"
"time"
)
var (
mailMux sync.Mutex
mailLastTime time.Time
)
func sendMail(mailBody string) {
mailMux.Lock()
defer mailMux.Unlock()
if d, _ := time.ParseDuration("10m"); time.Now().Sub(mailLastTime) >= d {
debug("* Sending warning mail ...")
from := fmt.Sprintf("%s <%s>", mailFromNameFlag, mailFromAddrFlag)
subject := "site-monitor warning"
contentType := "text/plain; charset=utf-8"
body := mailBody
to := strings.Join(mailToAddrFlag, ", ")
msg := fmt.Sprintf("To: %s\r\nFrom: %s\r\nSubject: %s\r\nContent-Type: %s\r\n\r\n%s",
to, from, subject, contentType, body)
auth := smtp.PlainAuth("", mailFromAddrFlag, mailFromPwdFlag, mailAuthHostFlag)
err := smtp.SendMail(mailServerAddrFlag, auth, mailFromAddrFlag, mailToAddrFlag, []byte(msg))
if err != nil {
debug("* Sending mail failed: %v", err)
} else {
debug("* Mail has been sent to %s", to)
}
mailLastTime = time.Now()
}
}