Skip to content

Commit

Permalink
Configure ReplyTo email for notification sender
Browse files Browse the repository at this point in the history
  • Loading branch information
rndstr committed Sep 25, 2018
1 parent ae6fc88 commit 53ac69a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion notification-eventmanager/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ func TestReceiver_Email(t *testing.T) {
email := <-emails
assert.NotNil(t, email)

assert.Equal(t, "<support@weave.works>", email.Sender)
assert.Equal(t, "<notifications@weave.works>", email.Sender)
}

func TestReceiver_RemoveAllEventTypes(t *testing.T) {
Expand Down
11 changes: 7 additions & 4 deletions notification-sender/cmd/sender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
stackdriverLogID string
emailURI string
emailFrom string
emailReplyTo string
)

flag.StringVar(&logLevel, "log.level", "info", "Logging level to use: debug | info | warn | error")
Expand All @@ -51,7 +52,8 @@ func main() {
// upper and lower case alphanumeric characters, forward-slash, underscore, hyphen, and period.
flag.StringVar(&stackdriverLogID, "stackdriverLogID", "WeaveCloud", "LogID for stackdriver notifications")
flag.StringVar(&emailURI, "emailURI", "", "uri of smtp server to send email through, of the format: smtp://username:password@hostname:port. Email-uri must be provided. For local development, you can set this to: log://, which will log all emails.")
flag.StringVar(&emailFrom, "emailFrom", "Weave Cloud <support@weave.works>", "From address for emails.")
flag.StringVar(&emailFrom, "emailFrom", "Weave Cloud <notifications@weave.works>", "From address for emails.")
flag.StringVar(&emailReplyTo, "emailReplyTo", "Weave Cloud <support@weave.works>", "Reply-To for emails.")

flag.Parse()

Expand All @@ -60,13 +62,14 @@ func main() {
return
}

if err := sender.ValidateEmailSender(emailURI, emailFrom); err != nil {
if err := sender.ValidateEmailSender(emailURI, emailFrom, emailReplyTo); err != nil {
log.Fatalf("cannot validate email sender (URI: %s, From: %s), error: %s", emailURI, emailFrom, err)
}

es := &sender.EmailSender{
URI: emailURI,
From: emailFrom,
URI: emailURI,
From: emailFrom,
ReplyTo: emailReplyTo,
}

ss := &sender.SlackSender{
Expand Down
18 changes: 10 additions & 8 deletions notification-sender/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ const (

// EmailSender contains creds to send emails
type EmailSender struct {
URI string
From string
URI string
From string
ReplyTo string
}

func waitForMailService(uri, from string) error {
func waitForMailService(uri, from, replyto string) error {
deadline := time.Now().Add(timeout)
var err error
for tries := 0; time.Now().Before(deadline); tries++ {
err = parseAndSend(uri, from, []string{"weaveworkstest@gmail.com"}, "Email sender validation", from)
err = parseAndSend(uri, from, replyto, []string{"weaveworkstest@gmail.com"}, "Email sender validation", from)
if err == nil {
return nil
}
Expand All @@ -49,8 +50,8 @@ func waitForMailService(uri, from string) error {
}

// ValidateEmailSender validates uri and from for email sender by sending test email
func ValidateEmailSender(uri, from string) error {
if err := waitForMailService(uri, from); err != nil {
func ValidateEmailSender(uri, from, replyto string) error {
if err := waitForMailService(uri, from, replyto); err != nil {
return errors.Wrap(err, "email sender validation failed")
}
log.Debug("email sender validated successfully")
Expand Down Expand Up @@ -85,14 +86,14 @@ func (es *EmailSender) Send(_ context.Context, addr json.RawMessage, notif types
}

addresses := strings.Split(addrStr, EmailSeparator)
if err := parseAndSend(es.URI, es.From, addresses, notifData.Subject, notifData.Body); err != nil {
if err := parseAndSend(es.URI, es.From, es.ReplyTo, addresses, notifData.Subject, notifData.Body); err != nil {
return errors.Wrap(err, "cannot parse and send email")
}

return nil
}

func parseAndSend(uri, from string, addresses []string, subject, body string) error {
func parseAndSend(uri, from string, replyto string, addresses []string, subject, body string) error {
u, err := url.Parse(uri)
if err != nil {
return errors.Wrapf(err, "cannot parse email URI %s", uri)
Expand Down Expand Up @@ -127,6 +128,7 @@ func parseAndSend(uri, from string, addresses []string, subject, body string) er
m.SetHeader("From", from)
m.SetHeader("To", formatted...)
m.SetHeader("Subject", subject)
m.SetHeader("Reply-To", replyto)
m.SetBody("text/html", body)
uid := uuid.New()
m.SetHeader("X-Entity-Ref-ID", uid.String())
Expand Down

0 comments on commit 53ac69a

Please sign in to comment.