Repository: jhillyerd/enmime
Pull request #324 (2024-02-12 09:54:21)

fix: do not setup MIME headers with the rawContent option set to true
Hi,

It's an improvement of the RawContent option to skip the function "setupMIMEHeaders" entirely. To avoid adding missing CTE header or try to correct them from the content.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/65636271/badge)](https://coveralls.io/builds/65636271)

coverage: 86.392% (+0.004%) from 86.388%
when pulling **cb4d1d8b30a0f1bf48bd05e3135ca3f050e22274 on jerjako:fix/raw-content-cte**
into **3b1b6352b3ff0a03dd6a564d0266d74f3f42924a on jhillyerd:main**.


Patch:
diff --git a/encode.go b/encode.go
index 5a6c522..132ab1b 100644
--- a/encode.go
+++ b/encode.go
@@ -50,7 +50,10 @@ func (p *Part) Encode(writer io.Writer) error {
 		}
 		p.Content = p.Content[:n]
 	}
-	cte := p.setupMIMEHeaders()
+	cte := teRaw
+	if p.parser == nil || !p.parser.rawContent {
+		cte = p.setupMIMEHeaders()
+	}
 	// Encode this part.
 	b := bufio.NewWriter(writer)
 	if err := p.encodeHeader(b); err != nil {
@@ -99,9 +102,7 @@ func (p *Part) setupMIMEHeaders() transferEncoding {
 
 	// If we are encoding a part that previously had content-transfer-encoding set, unset it so
 	// the correct encoding detection can be done below.
-	if p.parser != nil && !p.parser.rawContent {
-		p.Header.Del(hnContentEncoding)
-	}
+	p.Header.Del(hnContentEncoding)
 
 	cte := te7Bit
 	if len(p.Content) > 0 {


Test patch:
diff --git a/testdata/encode/parser-raw-content-html-option-false.raw.golden b/testdata/encode/parser-raw-content-html-option-false.raw.golden
index 05a297b..e8d686d 100644
--- a/testdata/encode/parser-raw-content-html-option-false.raw.golden
+++ b/testdata/encode/parser-raw-content-html-option-false.raw.golden
@@ -1,7 +1,9 @@
+Content-Transfer-Encoding: quoted-printable
 Content-Type: text/html; charset=utf-8
 From: me@me.com
 Mime-Version: 1.0
 Subject: Test subject
 To: you@you.com
 
-<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>
+<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph =
+=C3=A9=C3=A0.</p></body></html>
diff --git a/testdata/encode/parser-raw-content-html-option-true.raw.golden b/testdata/encode/parser-raw-content-html-option-true.raw.golden
index 7b680fd..209bc8b 100644
--- a/testdata/encode/parser-raw-content-html-option-true.raw.golden
+++ b/testdata/encode/parser-raw-content-html-option-true.raw.golden
@@ -1,9 +1,7 @@
-Content-Transfer-Encoding: quoted-printable
 Content-Type: text/html; charset=utf-8
 From: me@me.com
 Mime-Version: 1.0
 Subject: Test subject
 To: you@you.com
 
-<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.<=
-/p></body></html>
+<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph éà.</p></body></html>
diff --git a/testdata/encode/parser-raw-content-html-option.raw b/testdata/encode/parser-raw-content-html-option.raw
index 20266de..2197ffc 100644
--- a/testdata/encode/parser-raw-content-html-option.raw
+++ b/testdata/encode/parser-raw-content-html-option.raw
@@ -2,8 +2,6 @@ Subject: Test subject
 From: me@me.com
 To: you@you.com
 MIME-Version: 1.0
-Content-Transfer-Encoding: quoted-printable
 Content-Type: text/html; charset=utf-8
 
-<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.<=
-/p></body></html>
+<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph éà.</p></body></html>