Skip to content

Commit

Permalink
add subject encoding for proper non-ascii symbols representation
Browse files Browse the repository at this point in the history
Originally done in umputun/remark42#888
by @VeselovAlex.
  • Loading branch information
paskal authored and umputun committed Apr 28, 2022
1 parent eaf5e8a commit 779dedc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion email.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net"
Expand Down Expand Up @@ -209,7 +210,7 @@ func (em *Sender) buildMessage(text string, params Params) (message string, err
}
message = addHeader(message, "From", params.From)
message = addHeader(message, "To", strings.Join(params.To, ","))
message = addHeader(message, "Subject", params.Subject)
message = addHeader(message, "Subject", mime.BEncoding.Encode("utf-8", params.Subject))

withAttachments := len(params.Attachments) > 0
withInlineImg := len(params.InlineImages) > 0
Expand Down
4 changes: 2 additions & 2 deletions email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ func TestEmail_buildMessageWithMIME(t *testing.T) {
msg, err := e.buildMessage("this is a test\n12345\n", Params{
From: "from@example.com",
To: []string{"to@example.com"},
Subject: "subj",
Subject: "non-ascii symbols: Привет",
})
require.NoError(t, err)
assert.Contains(t, msg, "Content-Transfer-Encoding: quoted-printable\nContent-Type: text/html; charset=\"UTF-8\"", msg)
assert.Contains(t, msg, "From: from@example.com\nTo: to@example.com\nSubject: subj\nMIME-version: 1.0", msg)
assert.Contains(t, msg, "From: from@example.com\nTo: to@example.com\nSubject: =?utf-8?b?bm9uLWFzY2lpIHN5bWJvbHM6INCf0YDQuNCy0LXRgg==?=\nMIME-version: 1.0", msg)
assert.Contains(t, msg, "\n\nthis is a test\r\n12345\r\n", msg)
assert.Contains(t, msg, "Date: ", msg)
}
Expand Down

0 comments on commit 779dedc

Please sign in to comment.