fix(email): stop dropping the first paragraph of plain-text bodies
extractBody re-split the already-isolated body on the first blank line it found, intending to strip a stray content-header block some MTAs emit. That heuristic can't distinguish a header block from an ordinary blank line between paragraphs, so any multi-paragraph plain-text email silently lost everything before the first paragraph break.
This commit is contained in:
1 parent
ce4f43dd8d
commit
6f26d790ba
2 files changed
+17
-5
No files matched your search
@@ -244,11 +244,6 @@ func extractBody(raw string) string {
|
||||
}
|
||||
|
||||
// Non-multipart: strip trailing SMTP dots.
|
||||
body = strings.TrimSpace(body)
|
||||
// Remove any quoted-printable or base64 content headers.
|
||||
if parts := strings.SplitN(body, "\r\n\r\n", 2); len(parts) == 2 {
|
||||
body = parts[1]
|
||||
}
|
||||
return strings.TrimSpace(body)
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,23 @@ func TestExtractBodyPlain(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestExtractBodyMultiParagraph pins that a non-multipart plain-text body
|
||||
// keeps every paragraph. extractBody used to re-split the body on the
|
||||
// first "\r\n\r\n" it found -- intended to strip a stray content-header
|
||||
// block some MTAs emit -- but that heuristic can't tell a header block
|
||||
// from an ordinary blank line between paragraphs, so it silently dropped
|
||||
// the first paragraph of any multi-paragraph email.
|
||||
func TestExtractBodyMultiParagraph(t *testing.T) {
|
||||
raw := "From: test@example.com\r\nSubject: Hello\r\n\r\nFirst paragraph.\r\n\r\nSecond paragraph."
|
||||
got := extractBody(raw)
|
||||
if !strings.Contains(got, "First paragraph.") {
|
||||
t.Errorf("extractBody = %q, want it to contain %q", got, "First paragraph.")
|
||||
}
|
||||
if !strings.Contains(got, "Second paragraph.") {
|
||||
t.Errorf("extractBody = %q, want it to contain %q", got, "Second paragraph.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractBodyMultipart(t *testing.T) {
|
||||
raw := "From: test@example.com\r\nContent-Type: multipart/alternative; boundary=abc123\r\n\r\n--abc123\r\nContent-Type: text/plain\r\n\r\nplain text here\r\n--abc123\r\nContent-Type: text/html\r\n\r\n<html></html>\r\n--abc123--"
|
||||
if got := extractBody(raw); got != "plain text here" {
|
||||
|
||||
Reference in new issue
Block a user