summaryrefslogtreecommitdiff
path: root/app/email.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/email.go')
-rw-r--r--app/email.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/app/email.go b/app/email.go
new file mode 100644
index 0000000..b497bf7
--- /dev/null
+++ b/app/email.go
@@ -0,0 +1,58 @@
+package main
+
+import (
+ "bytes"
+ "github.com/jordan-wright/email"
+ "log"
+ "net/smtp"
+ "os"
+)
+
+func SendEmail(u []User, h []Host) {
+ log.Println("Sending E-Mail")
+ if len(u) == 0 {
+ return
+ }
+ if len(h) == 0 {
+ return
+ }
+ log.Println("Sending E-Mail - Checked")
+ adresses := getEmails(u)
+ h = getHosts(h)
+ e := &email.Email{
+ To: adresses,
+ From: "StatusPage <" + os.Getenv("STATUS_HTTP_ADRESS") + ">",
+ Subject: "Host is down (" + h[0].Host + ")",
+ HTML: getBody(h),
+ }
+ err := e.Send(os.Getenv("STATUS_HTTP_MAILER"), smtp.PlainAuth("", os.Getenv("STATUS_HTTP_ADRESS"), os.Getenv("STATUS_HTTP_PASSWORD"), os.Getenv("STATUS_HTTP_MAILER")))
+ log.Println(err)
+}
+
+func getEmails(u []User) []string {
+ var emails []string
+ for k, _ := range u {
+ emails = append(emails, u[k].Email)
+ }
+ return emails
+}
+
+func getHosts(hosts []Host) []Host {
+ var h []Host
+ for k, _ := range hosts {
+ if !hosts[k].Monitored {
+ h = append(h, hosts[k])
+ }
+ }
+ return h
+}
+
+func getBody(hosts []Host) []byte {
+
+ index := emailTempl.Lookup("index.html")
+
+ var ret bytes.Buffer
+ index.ExecuteTemplate(&ret, "index.html", hosts)
+
+ return ret.Bytes()
+}