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): fold long subject headers

Mike Kosulin22e6cdab3ec5
diff --git a/internal/services/email/header_encoding_test.go b/internal/services/email/header_encoding_test.go
index 3d85619..6a3ccaa 100644
--- a/internal/services/email/header_encoding_test.go
+++ b/internal/services/email/header_encoding_test.go
@@ -59,3 +59,35 @@ func TestBuildMessageSubjectSplitsBetweenWords(t *testing.T) {
}
}
}
+
+// RFC 5322 recommends header lines of at most 78 characters, and RFC 2047
+// limits a line that carries encoded-words to 76, so long subjects must be
+// folded.
+func TestBuildMessageFoldsLongSubject(t *testing.T) {
+ for _, subject := range []string{
+ strings.TrimSpace(strings.Repeat("New rooms found for 3 subscriptions. ", 30)),
+ strings.TrimSpace(strings.Repeat("Места появились по 3 подпискам. ", 30)),
+ } {
+ raw := buildMessage("[email protected]", []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") {
+ limit := 78
+ if encodedWordRe.MatchString(line) {
+ limit = 76
+ }
+ if len(line) > limit {
+ t.Errorf("header line is %d characters, want <= %d: %s", len(line), limit, line)
+ }
+ }
+
+ msg, err := mail.ReadMessage(strings.NewReader(string(raw)))
+ if err != nil {
+ t.Fatalf("built message does not parse: %v", err)
+ }
+ if got, _ := new(mime.WordDecoder).DecodeHeader(msg.Header.Get("Subject")); got != subject {
+ t.Errorf("folded subject decodes as %q, want %q", got, subject)
+ }
+ }
+}
diff --git a/internal/services/email/smtp_sender.go b/internal/services/email/smtp_sender.go
index 116844f..36e5db6 100644
--- a/internal/services/email/smtp_sender.go
+++ b/internal/services/email/smtp_sender.go
@@ -316,11 +316,12 @@ func isASCII(s string) bool {
// only at spaces; a break inside a word would reach the reader as a stray space.
func encodeHeader(s string) string {
if mime.BEncoding.Encode("UTF-8", s) == s {
- return s
+ return foldPlain(s)
}
- // 45 bytes encode to 60 base64 characters, which keeps each encoded-word
- // within the 75 characters RFC 2047 allows.
- const limit = 45
+ // 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.
+ const limit = 39
chunks := []string{""}
for i, w := range strings.Split(s, " ") {
if i > 0 {
@@ -339,7 +340,27 @@ func encodeHeader(s string) string {
for i, c := range chunks {
chunks[i] = "=?UTF-8?b?" + base64.StdEncoding.EncodeToString([]byte(c)) + "?="
}
- return strings.Join(chunks, " ")
+ return strings.Join(chunks, "\r\n ")
+}
+
+// foldPlain folds plain text at spaces so that, after "Subject: ", each line
+// stays within the 78 characters RFC 5322 §2.1.1 recommends.
+func foldPlain(s string) string {
+ var b strings.Builder
+ n := len("Subject: ")
+ for i, w := range strings.Split(s, " ") {
+ if i > 0 {
+ if w != "" && n+1+len(w) > 78 {
+ b.WriteString("\r\n")
+ n = 0
+ }
+ b.WriteByte(' ')
+ n++
+ }
+ b.WriteString(w)
+ n += len(w)
+ }
+ return b.String()
}
// hasHeader reports whether the caller-supplied headers already include key