From e87d3b55abee3db0de9c6a4b74c0ad1ee955fd63 Mon Sep 17 00:00:00 2001 From: ddvk Date: Mon, 9 Nov 2020 10:18:27 +0100 Subject: [PATCH] trim addresses --- internal/email/smtp.go | 7 ++++++- internal/email/smtp_test.go | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/email/smtp.go b/internal/email/smtp.go index 33f6578d..f25881ee 100644 --- a/internal/email/smtp.go +++ b/internal/email/smtp.go @@ -47,6 +47,11 @@ func Strip(msg string) string { return msg } +// workaround for go < 1.15 +func TrimAddresses(address string) string { + return strings.Trim(strings.Trim(address, ","), " ") +} + func (b *EmailBuilder) AddFile(name string, data []byte) { if b.fileNames == nil || b.files == nil { b.fileNames = []string{name} @@ -71,7 +76,7 @@ func (b *EmailBuilder) Send() (err error) { if err != nil { return err } - to, err := mail.ParseAddressList(b.To) + to, err := mail.ParseAddressList(TrimAddresses(b.To)) if err != nil { return err } diff --git a/internal/email/smtp_test.go b/internal/email/smtp_test.go index 609ea71a..35b2a5d2 100644 --- a/internal/email/smtp_test.go +++ b/internal/email/smtp_test.go @@ -7,12 +7,12 @@ import ( ) func TestParseEmptyAddress(t *testing.T) { - addreses := ", email@domain.com , " + addreses := TrimAddresses(", email@domain.com , blah@blah, ") to, err := mail.ParseAddressList(addreses) if err != nil { t.Error(err) } - if len(to) > 1 { + if len(to) > 2 { t.Error("more than 2") } t.Log(to)