Open platform to collect and prioritize feedback
fix: always wrap names in quotes when sending email (#528)
Guilherme Oenning593ede694c10
diff --git a/app/pkg/email/email.go b/app/pkg/email/email.goindex 949924cd6..2df866ad5 100644--- a/app/pkg/email/email.go+++ b/app/pkg/email/email.go@@ -3,6 +3,7 @@ package emailimport ("bytes""html/template"+ "net/mail""regexp""strings"@@ -96,6 +97,20 @@ func NewRecipient(name, address string, params Params) Recipient {}}+// Strings returns the RFC format to send emails via SMTP+func (r Recipient) String() string {+ if r.Address == "" {+ return ""+ }++ address := mail.Address{+ Name: r.Name,+ Address: r.Address,+ }++ return address.String()+}+var whitelist = env.GetEnvOrDefault("EMAIL_WHITELIST", "")var whitelistRegex = regexp.MustCompile(whitelist)var blacklist = env.GetEnvOrDefault("EMAIL_BLACKLIST", "")diff --git a/app/pkg/email/email_test.go b/app/pkg/email/email_test.goindex e7432dff3..77086fc8c 100644--- a/app/pkg/email/email_test.go+++ b/app/pkg/email/email_test.go@@ -131,3 +131,49 @@ func TestParamsMerge(t *testing.T) {"email": "[email protected]",})}++func TestRecipient_String(t *testing.T) {+ RegisterT(t)++ testCases := []struct {+ name string+ email string+ expected string+ }{+ {+ name: "Jon",+ email: "[email protected]",+ expected: `"Jon" <[email protected]>`,+ },+ {+ name: "Snow, Jon",+ email: "[email protected]",+ expected: `"Snow, Jon" <[email protected]>`,+ },+ {+ name: "",+ email: "[email protected]",+ expected: "<[email protected]>",+ },+ {+ name: "Jon's Home Account",+ email: "[email protected]",+ expected: `"Jon's Home Account" <[email protected]>`,+ },+ {+ name: `Jon "Great" Snow`,+ email: "[email protected]",+ expected: `"Jon \"Great\" Snow" <[email protected]>`,+ },+ {+ name: "Jon",+ email: "",+ expected: "",+ },+ }++ for _, testCase := range testCases {+ r := email.NewRecipient(testCase.name, testCase.email, email.Params{})+ Expect(r.String()).Equals(testCase.expected)+ }+}diff --git a/app/pkg/email/mailgun/mailgun.go b/app/pkg/email/mailgun/mailgun.goindex dccb42f3b..5ec171153 100644--- a/app/pkg/email/mailgun/mailgun.go+++ b/app/pkg/email/mailgun/mailgun.go@@ -54,8 +54,7 @@ func (s *Sender) BatchSend(ctx email.Context, templateName string, params email.}form := url.Values{}-- form.Add("from", fmt.Sprintf("%s <%s>", from, email.NoReply))+ form.Add("from", email.NewRecipient(from, email.NoReply, email.Params{}).String())form.Add("h:Reply-To", email.NoReply)form.Add("subject", message.Subject)form.Add("html", message.Body)@@ -69,7 +68,7 @@ func (s *Sender) BatchSend(ctx email.Context, templateName string, params email.for _, r := range to {if r.Address != "" {if email.CanSendTo(r.Address) {- form.Add("to", fmt.Sprintf("%s <%s>", r.Name, r.Address))+ form.Add("to", r.String())recipientVariables[r.Address] = r.Params} else {s.logger.Warnf("Skipping email to '@{Name} <@{Address}>'.", log.Props{diff --git a/app/pkg/email/mailgun/mailgun_test.go b/app/pkg/email/mailgun/mailgun_test.goindex bfc2ce703..d792fd01a 100644--- a/app/pkg/email/mailgun/mailgun_test.go+++ b/app/pkg/email/mailgun/mailgun_test.go@@ -53,8 +53,8 @@ func TestSend_Success(t *testing.T) {values, err := url.ParseQuery(string(bytes))Expect(err).IsNil()Expect(values).HasLen(6)- Expect(values.Get("to")).Equals("Jon Sow <[email protected]>")- Expect(values.Get("from")).Equals("Fider Test <[email protected]>")+ Expect(values.Get("to")).Equals(`"Jon Sow" <[email protected]>`)+ Expect(values.Get("from")).Equals(`"Fider Test" <[email protected]>`)Expect(values.Get("h:Reply-To")).Equals("[email protected]")Expect(values.Get("subject")).Equals("Message to: Hello")Expect(values["o:tag"][0]).Equals("template:echo_test")@@ -157,9 +157,9 @@ func TestBatch_Success(t *testing.T) {Expect(err).IsNil()Expect(values).HasLen(7)Expect(values["to"]).HasLen(2)- Expect(values["to"][0]).Equals("Jon Sow <[email protected]>")- Expect(values["to"][1]).Equals("Arya Stark <[email protected]>")- Expect(values.Get("from")).Equals("Fider Test <[email protected]>")+ Expect(values["to"][0]).Equals(`"Jon Sow" <[email protected]>`)+ Expect(values["to"][1]).Equals(`"Arya Stark" <[email protected]>`)+ Expect(values.Get("from")).Equals(`"Fider Test" <[email protected]>`)Expect(values.Get("h:Reply-To")).Equals("[email protected]")Expect(values.Get("subject")).Equals("Message to: %recipient.name%")Expect(values["o:tag"]).HasLen(2)diff --git a/app/pkg/email/smtp/smtp.go b/app/pkg/email/smtp/smtp.goindex 0d529b2b9..3daf206aa 100644--- a/app/pkg/email/smtp/smtp.go+++ b/app/pkg/email/smtp/smtp.go@@ -74,9 +74,9 @@ func (s *Sender) Send(ctx email.Context, templateName string, params email.Parammessage := email.RenderMessage(ctx, templateName, params.Merge(to.Params))b := builder{}- b.Set("From", fmt.Sprintf("%s <%s>", from, email.NoReply))+ b.Set("From", email.NewRecipient(from, email.NoReply, email.Params{}).String())b.Set("Reply-To", email.NoReply)- b.Set("To", fmt.Sprintf("%s <%s>", to.Name, to.Address))+ b.Set("To", to.String())b.Set("Subject", message.Subject)b.Set("MIME-version", "1.0")b.Set("Content-Type", "text/html; charset=\"UTF-8\"")diff --git a/app/pkg/email/smtp/smtp_test.go b/app/pkg/email/smtp/smtp_test.goindex 68eb4b1e6..1a7d9471a 100644--- a/app/pkg/email/smtp/smtp_test.go+++ b/app/pkg/email/smtp/smtp_test.go@@ -60,7 +60,7 @@ func TestSend_Success(t *testing.T) {Expect(requests[0].auth).Equals(gosmtp.PlainAuth("", "us3r", "p4ss", "localhost"))Expect(requests[0].from).Equals("[email protected]")Expect(requests[0].to).Equals([]string{"[email protected]"})- Expect(string(requests[0].body)).ContainsSubstring("From: Fider Test <[email protected]>\r\nReply-To: [email protected]\r\nTo: Jon Sow <[email protected]>\r\nSubject: Message to: Hello\r\nMIME-version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\n\r\n")+ Expect(string(requests[0].body)).ContainsSubstring("From: \"Fider Test\" <[email protected]>\r\nReply-To: [email protected]\r\nTo: \"Jon Sow\" <[email protected]>\r\nSubject: Message to: Hello\r\nMIME-version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\n\r\n")Expect(string(requests[0].body)).ContainsSubstring("Hello World Hello!")}func TestSend_SkipEmptyAddress(t *testing.T) {@@ -127,13 +127,13 @@ func TestBatch_Success(t *testing.T) {Expect(requests[0].auth).Equals(gosmtp.PlainAuth("", "us3r", "p4ss", "localhost"))Expect(requests[0].from).Equals("[email protected]")Expect(requests[0].to).Equals([]string{"[email protected]"})- Expect(string(requests[0].body)).ContainsSubstring("From: Fider Test <[email protected]>\r\nReply-To: [email protected]\r\nTo: Jon Sow <[email protected]>\r\nSubject: Message to: Jon\r\nMIME-version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\n\r\n")+ Expect(string(requests[0].body)).ContainsSubstring("From: \"Fider Test\" <[email protected]>\r\nReply-To: [email protected]\r\nTo: \"Jon Sow\" <[email protected]>\r\nSubject: Message to: Jon\r\nMIME-version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\n\r\n")Expect(string(requests[0].body)).ContainsSubstring("Hello World Jon!")Expect(requests[1].servername).Equals("localhost:1234")Expect(requests[1].auth).Equals(gosmtp.PlainAuth("", "us3r", "p4ss", "localhost"))Expect(requests[1].from).Equals("[email protected]")Expect(requests[1].to).Equals([]string{"[email protected]"})- Expect(string(requests[1].body)).ContainsSubstring("From: Fider Test <[email protected]>\r\nReply-To: [email protected]\r\nTo: Arya Stark <[email protected]>\r\nSubject: Message to: Arya\r\nMIME-version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\n\r\n")+ Expect(string(requests[1].body)).ContainsSubstring("From: \"Fider Test\" <[email protected]>\r\nReply-To: [email protected]\r\nTo: \"Arya Stark\" <[email protected]>\r\nSubject: Message to: Arya\r\nMIME-version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\n\r\n")Expect(string(requests[1].body)).ContainsSubstring("Hello World Arya!")}