Skip to content

Commit

Permalink
Fixes #114 (#115)
Browse files Browse the repository at this point in the history
Added the To header
  • Loading branch information
timurstrekalov authored and fusiondog committed May 1, 2016
1 parent 895b981 commit 663c13a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
24 changes: 18 additions & 6 deletions notifier/email-notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"net/smtp"

log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
"strings"
)

var sendMail = smtp.SendMail

type EmailNotifier struct {
ClusterName string
Template string
Expand Down Expand Up @@ -83,15 +86,24 @@ func (emailNotifier *EmailNotifier) Notify(alerts Messages) bool {
return false
}

msg := ""
msg += fmt.Sprintf("From: \"%s\" <%s>\n", emailNotifier.SenderAlias, emailNotifier.SenderEmail)
msg += fmt.Sprintf("Subject: %s is %s\n", emailNotifier.ClusterName, overAllStatus)
msg += "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
msg += body.String()
msg := fmt.Sprintf(`From: "%s" <%s>
To: %s
Subject: %s is %s
MIME-version: 1.0;
Content-Type: text/html; charset="UTF-8";
%s
`,
emailNotifier.SenderAlias,
emailNotifier.SenderEmail,
strings.Join(emailNotifier.Receivers, ", "),
emailNotifier.ClusterName,
overAllStatus,
body.String())

addr := fmt.Sprintf("%s:%d", emailNotifier.Url, emailNotifier.Port)
auth := smtp.PlainAuth("", emailNotifier.Username, emailNotifier.Password, emailNotifier.Url)
if err := smtp.SendMail(addr, auth, emailNotifier.SenderEmail, emailNotifier.Receivers, []byte(msg)); err != nil {
if err := sendMail(addr, auth, emailNotifier.SenderEmail, emailNotifier.Receivers, []byte(msg)); err != nil {
log.Println("Unable to send notification:", err)
return false
}
Expand Down
72 changes: 72 additions & 0 deletions notifier/email-notifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package notifier

import (
"fmt"
"net/smtp"
"reflect"
"strings"
"testing"
)

func TestNotify(t *testing.T) {
oldSendMail := sendMail
defer func() {
sendMail = oldSendMail
}()

host := "mailserver.localdomain"
port := 123

expectedAddr := fmt.Sprintf("%s:%d", host, port)
expectedFrom := "[email protected]"
expectedTo := []string{"[email protected]", "[email protected]"}
expectedMsg := `From: "Some Sender" <[email protected]>
To: [email protected], [email protected]
Subject: Some Cluster is HEALTHY
MIME-version: 1.0;
Content-Type: text/html; charset="UTF-8";
<!DOCTYPE html>
`

sendMail = func(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
if addr != expectedAddr {
t.Errorf("expected %s, got %s", expectedAddr, addr)
}

if a == nil {
t.Error("auth must not be null")
}

if from != expectedFrom {
t.Errorf("expected %s, got %s", expectedFrom, from)
}

if !reflect.DeepEqual(to, expectedTo) {
t.Errorf("expected %s, got %s", expectedTo, to)
}

stringMsg := string(msg)
if !strings.HasPrefix(stringMsg, expectedMsg) {
t.Errorf("expected message to start with\n\n%s\n\ngot\n\n%s", expectedMsg, stringMsg)
}

return nil
}

notifier := EmailNotifier{
Username: "some username",
Password: "some password",
ClusterName: "Some Cluster",
Url: host,
Port: port,
SenderEmail: expectedFrom,
SenderAlias: "Some Sender",
Receivers: expectedTo,
}

if !notifier.Notify(make(Messages, 0)) {
t.Error("Notify must return true")
}
}

0 comments on commit 663c13a

Please sign in to comment.