Skip to content

Commit

Permalink
fix(api): support utf8 in mail subject
Browse files Browse the repository at this point in the history
close #5351

Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin committed Sep 30, 2020
1 parent 6a36965 commit e657e70
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
23 changes: 15 additions & 8 deletions engine/api/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,30 @@ func smtpClient(ctx context.Context) (*smtp.Client, error) {
// Here is the key, you need to call tls.Dial instead of smtp.Dial
// for smtp servers running on 465 that require an ssl connection
// from the very beginning (no starttls)
conn, errc := tls.Dial("tcp", servername, tlsconfig)
if errc != nil {
log.Warning(ctx, "Error with c.Dial:%s\n", errc.Error())
return nil, errc
conn, err := tls.Dial("tcp", servername, tlsconfig)
if err != nil {
log.Warning(ctx, "Error with c.Dial:%s\n", err.Error())
return nil, sdk.WithStack(err)
}

c, err = smtp.NewClient(conn, smtpHost)
if err != nil {
log.Warning(ctx, "Error with c.NewClient:%s\n", err.Error())
return nil, err
return nil, sdk.WithStack(err)
}
// TLS config
tlsconfig := &tls.Config{
InsecureSkipVerify: false,
ServerName: smtpHost,
}
c.StartTLS(tlsconfig)
if err := c.StartTLS(tlsconfig); err != nil {
return nil, sdk.WithStack(err)
}
} else {
c, err = smtp.Dial(servername)
if err != nil {
log.Warning(ctx, "Error with c.NewClient:%s\n", err.Error())
return nil, err
return nil, sdk.WithStack(err)
}
}

Expand Down Expand Up @@ -183,7 +185,12 @@ func SendEmail(ctx context.Context, subject string, mailContent *bytes.Buffer, u
headers := make(map[string]string)
headers["From"] = smtpFrom
headers["To"] = to.String()
headers["Subject"] = subject
if sdk.StringIsAscii(subject) {
headers["Subject"] = subject
} else {
// https://tools.ietf.org/html/rfc2047
headers["Subject"] = "=?UTF-8?Q?" + subject + "?="
}

// https://tools.ietf.org/html/rfc4021
headers["Date"] = time.Now().Format(time.RFC1123Z)
Expand Down
9 changes: 9 additions & 0 deletions sdk/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,15 @@ func (s *Int64Slice) Remove(v int64) {
*s = filtered
}

func StringIsAscii(s string) bool {
for _, r := range s {
if r > unicode.MaxASCII {
return false
}
}
return true
}

func RemoveNotPrintableChar(in string) string {
m := func(r rune) rune {
switch {
Expand Down
5 changes: 5 additions & 0 deletions sdk/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/stretchr/testify/assert"
)

func TestStringIsAscii(t *testing.T) {
assert.True(t, StringIsAscii("aaa"))
assert.False(t, StringIsAscii("aaa 🚀"))
}

func TestRemoveNotPrintableChar(t *testing.T) {
var tests = []struct {
in string
Expand Down

0 comments on commit e657e70

Please sign in to comment.