-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathemail.go
56 lines (49 loc) · 1.16 KB
/
email.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
package main
import (
"log"
"net"
"net/smtp"
"time"
)
var emailTimer *time.Timer
var emailBuffer string
func debouncedSendEmail(to string, body string, config MadnsConfig) {
if emailTimer == nil {
emailBuffer = emailBuffer + body
emailDelay := time.Duration(config.SMTPDelay)
if emailDelay == 0 {
emailDelay = 60 // default to 60 second email delay
}
emailTimer = time.NewTimer(emailDelay * time.Second)
go func() {
<-emailTimer.C
emailTimer.Stop()
smtpSend(to, emailBuffer, config)
emailBuffer = ""
emailTimer = nil
}()
} else {
emailBuffer = emailBuffer + body
}
}
func smtpSend(to string, body string, config MadnsConfig) {
from := config.SMTPUser
pass := config.SMTPPassword
authhost, _, err := net.SplitHostPort(config.SMTPServer)
if err != nil {
log.Printf("SMTPServer field syntax error: %s", err)
return
}
msg := "From: " + from + "\n" +
"To: " + to + "\n" +
"Subject: Madns Alert\n\n" +
body
err = smtp.SendMail(config.SMTPServer,
smtp.PlainAuth("", from, pass, authhost),
from, []string{to}, []byte(msg))
if err != nil {
log.Printf("SMTP error: %s", err)
return
}
log.Print("sent email to " + to)
}