Merge pull request #48 from quinox/mailfix

Add date header to please strict mail servers
This commit is contained in:
Julius Volz 2015-04-19 23:37:40 +02:00
commit 99636c0e55
2 changed files with 12 additions and 1 deletions

View File

@ -41,6 +41,7 @@ const contentTypeJson = "application/json"
var bodyTmpl = template.Must(template.New("message").Parse(`From: Prometheus Alertmanager <{{.From}}> var bodyTmpl = template.Must(template.New("message").Parse(`From: Prometheus Alertmanager <{{.From}}>
To: {{.To}} To: {{.To}}
Date: {{.Date}}
Subject: [ALERT] {{.Alert.Labels.alertname}}: {{.Alert.Summary}} Subject: [ALERT] {{.Alert.Labels.alertname}}: {{.Alert.Summary}}
{{.Alert.Description}} {{.Alert.Description}}
@ -205,13 +206,19 @@ func (n *notifier) sendHipChatNotification(authToken string, roomId int32, color
} }
func writeEmailBody(w io.Writer, from string, to string, a *Alert) error { func writeEmailBody(w io.Writer, from string, to string, a *Alert) error {
return writeEmailBodyWithTime(w, from, to, a, time.Now())
}
func writeEmailBodyWithTime(w io.Writer, from string, to string, a *Alert, moment time.Time) error {
err := bodyTmpl.Execute(w, struct { err := bodyTmpl.Execute(w, struct {
From string From string
To string To string
Date string
Alert *Alert Alert *Alert
}{ }{
From: from, From: from,
To: to, To: to,
Date: moment.Format("Mon, 2 Jan 2006 15:04:05 -0700"),
Alert: a, Alert: a,
}) })
if err != nil { if err != nil {

View File

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"os" "os"
"testing" "testing"
"time"
) )
func TestWriteEmailBody(t *testing.T) { func TestWriteEmailBody(t *testing.T) {
@ -36,10 +37,13 @@ func TestWriteEmailBody(t *testing.T) {
}, },
} }
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
writeEmailBody(buf, "from@prometheus.io", "to@prometheus.io", event) location, _ := time.LoadLocation("Europe/Amsterdam")
moment := time.Date(1918, 11, 11, 11, 0, 3, 0, location)
writeEmailBodyWithTime(buf, "from@prometheus.io", "to@prometheus.io", event, moment)
expected := `From: Prometheus Alertmanager <from@prometheus.io> expected := `From: Prometheus Alertmanager <from@prometheus.io>
To: to@prometheus.io To: to@prometheus.io
Date: Mon, 11 Nov 1918 11:00:03 +0019
Subject: [ALERT] TestAlert: Testsummary Subject: [ALERT] TestAlert: Testsummary
Test alert description, something went wrong here. Test alert description, something went wrong here.