Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

fix(email): break default sender name only between words

Mike Kosulin97a1af3acd76
diff --git a/internal/services/email/header_encoding_test.go b/internal/services/email/header_encoding_test.go
index 6a3ccaa..9e6494f 100644
--- a/internal/services/email/header_encoding_test.go
+++ b/internal/services/email/header_encoding_test.go
@@ -91,3 +91,37 @@ func TestBuildMessageFoldsLongSubject(t *testing.T) {
}
}
}
+
+// The workspace default sender name is encoded the same way, so a long
+// non-ASCII name must also break only between words and stay within the
+// RFC 2047 line limit.
+func TestBuildMessageDefaultSenderNameSplitsBetweenWords(t *testing.T) {
+ const name = "Акме — компания, которая производит абсолютно всё"
+ from := resolveSenderWithDefault("[email protected]", name)
+ raw := buildMessage(from, []string{"[email protected]"},
+ "subject", "", "body", nil, nil, "", "", false)
+
+ head, _, _ := strings.Cut(string(raw), "\r\n\r\n")
+ for _, line := range strings.Split(head, "\r\n") {
+ if encodedWordRe.MatchString(line) && len(line) > 76 {
+ t.Errorf("header line is %d characters, want <= 76: %s", len(line), line)
+ }
+ }
+
+ msg, err := mail.ReadMessage(strings.NewReader(string(raw)))
+ if err != nil {
+ t.Fatalf("built message does not parse: %v", err)
+ }
+ addrs, err := msg.Header.AddressList("From")
+ if err != nil || len(addrs) != 1 || addrs[0].Name != name {
+ t.Fatalf("From parses as %v (%v), want name %q", addrs, err, name)
+ }
+
+ naive := encodedWordRe.ReplaceAllStringFunc(msg.Header.Get("From"), func(w string) string {
+ s, _ := new(mime.WordDecoder).Decode(w)
+ return s
+ })
+ if want := name + " <[email protected]>"; strings.Join(strings.Fields(naive), " ") != want {
+ t.Errorf("From arrives as %q, want %q", naive, want)
+ }
+}
diff --git a/internal/services/email/service.go b/internal/services/email/service.go
index f9adaeb..0b84fe7 100644
--- a/internal/services/email/service.go
+++ b/internal/services/email/service.go
@@ -1405,6 +1405,9 @@ func resolveSenderWithDefault(from, defaultName string) string {
if defaultName == "" {
return from
}
+ if !isASCII(defaultName) {
+ return strings.Join(encodeWords(defaultName), " ") + " <" + addr.Address + ">"
+ }
return (&mail.Address{Name: defaultName, Address: addr.Address}).String()
}
diff --git a/internal/services/email/smtp_sender.go b/internal/services/email/smtp_sender.go
index 36e5db6..4b10ed6 100644
--- a/internal/services/email/smtp_sender.go
+++ b/internal/services/email/smtp_sender.go
@@ -310,14 +310,20 @@ func isASCII(s string) bool {
return true
}
-// encodeHeader encodes s as RFC 2047 encoded-words for an unstructured header
-// such as Subject. RFC 2047 ignores the whitespace between encoded-words, but
-// some relays keep it when they re-encode the header, so encoded-words break
-// only at spaces; a break inside a word would reach the reader as a stray space.
+// encodeHeader encodes s for an unstructured header such as Subject, folding
+// its encoded-words onto separate lines.
func encodeHeader(s string) string {
if mime.BEncoding.Encode("UTF-8", s) == s {
return foldPlain(s)
}
+ return strings.Join(encodeWords(s), "\r\n ")
+}
+
+// encodeWords encodes s as RFC 2047 encoded-words. RFC 2047 ignores the
+// whitespace between encoded-words, but some relays keep it when they
+// re-encode the header, so encoded-words break only at spaces; a break inside
+// a word would reach the reader as a stray space.
+func encodeWords(s string) []string {
// 39 bytes encode to 52 base64 characters, so even the first line, after
// "Subject: ", stays within the 76 characters RFC 2047 allows a line that
// carries encoded-words.
@@ -340,7 +346,7 @@ func encodeHeader(s string) string {
for i, c := range chunks {
chunks[i] = "=?UTF-8?b?" + base64.StdEncoding.EncodeToString([]byte(c)) + "?="
}
- return strings.Join(chunks, "\r\n ")
+ return chunks
}
// foldPlain folds plain text at spaces so that, after "Subject: ", each line
@@ -391,7 +397,9 @@ func newBoundary() string {
func buildMessage(from string, to []string, subject, htmlBody, textBody string, attachments []models.Attachment, headers map[string]string, listUnsubscribeURL, listUnsubscribeMailto string, listUnsubscribePost bool) []byte {
var b strings.Builder
- fmt.Fprintf(&b, "From: %s\r\n", from)
+ // Fold between encoded-words only when writing the header: from is still
+ // parsed with net/mail, which cannot read a folded address.
+ fmt.Fprintf(&b, "From: %s\r\n", strings.ReplaceAll(from, "?= =?", "?=\r\n =?"))
fmt.Fprintf(&b, "To: %s\r\n", strings.Join(to, ", "))
fmt.Fprintf(&b, "Subject: %s\r\n", encodeHeader(subject))