From 69b1dc23599234b4fd97058e56a55b3b6108e4c4 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 2 Aug 2024 15:30:46 +0200 Subject: [PATCH] Refactor: Handle missing Content-Type as plain text Added a new example email and modified tests to include an RFC 5322 format. Refactored the code to handle cases where the Content-Type header is missing by defaulting to plain text (7bit, US-ASCII). Deleted redundant test cases that no longer apply. --- eml.go | 10 +++++++++- eml_test.go | 43 ++++++++++++++----------------------------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/eml.go b/eml.go index 896e0c6..7e705f6 100644 --- a/eml.go +++ b/eml.go @@ -180,7 +180,15 @@ func parseEMLBodyParts(parsedMsg *netmail.Message, bodybuf *bytes.Buffer, msg *M // Extract the transfer encoding of the body mediatype, params, err := mime.ParseMediaType(parsedMsg.Header.Get(HeaderContentType.String())) if err != nil { - return fmt.Errorf("failed to extract content type: %w", err) + switch { + // If no Content-Type header is found, we assume that this is a plain text, 7bit, US-ASCII mail + case strings.EqualFold(err.Error(), "mime: no media type"): + mediatype = TypeTextPlain.String() + params = make(map[string]string) + params["charset"] = CharsetASCII.String() + default: + return fmt.Errorf("failed to extract content type: %w", err) + } } if value, ok := params["charset"]; ok { msg.SetCharset(Charset(value)) diff --git a/eml_test.go b/eml_test.go index 5704ce4..44bfb54 100644 --- a/eml_test.go +++ b/eml_test.go @@ -14,6 +14,16 @@ import ( ) const ( + // RFC 5322 example mail + // See: https://datatracker.ietf.org/doc/html/rfc5322#appendix-A.1.1 + exampleMailRFC5322A11 = `From: John Doe +To: Mary Smith +Subject: Saying Hello +Date: Fri, 21 Nov 1997 09:55:06 -0600 +Message-ID: <1234@local.machine.example> + +This is a message just to say hello. +So, "Hello".` exampleMailPlainNoEnc = `Date: Wed, 01 Nov 2023 00:00:00 +0000 MIME-Version: 1.0 Message-ID: <1305604950.683004066175.AAAAAAAAaaaaaaaaB@go-mail.dev> @@ -72,18 +82,6 @@ Cc: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: base64 -This plain text body should not be parsed as Base64. -` - exampleMailPlainNoContentType = `Date: Wed, 01 Nov 2023 00:00:00 +0000 -MIME-Version: 1.0 -Message-ID: <1305604950.683004066175.AAAAAAAAaaaaaaaaB@go-mail.dev> -Subject: Example mail // plain text without encoding -User-Agent: go-mail v0.4.0 // https://github.com/wneessen/go-mail -X-Mailer: go-mail v0.4.0 // https://github.com/wneessen/go-mail -From: "Toni Tester" -To: -Cc: - This plain text body should not be parsed as Base64. ` exampleMailPlainUnknownContentType = `Date: Wed, 01 Nov 2023 00:00:00 +0000 @@ -623,6 +621,10 @@ func TestEMLToMsgFromString(t *testing.T) { enc string sub string }{ + { + "RFC5322 A1.1", exampleMailRFC5322A11, "7bit", + "Saying Hello", + }, { "Plain text no encoding (7bit)", exampleMailPlain7Bit, "7bit", "Example mail // plain text without encoding", @@ -731,12 +733,6 @@ func TestEMLToMsgFromReaderFailing(t *testing.T) { t.Error("EML from Reader with unknown content type was supposed to fail, but didn't") } mailbuf.Reset() - mailbuf.WriteString(exampleMailPlainNoContentType) - _, err = EMLToMsgFromReader(mailbuf) - if err == nil { - t.Error("EML from Reader with no content type was supposed to fail, but didn't") - } - mailbuf.Reset() mailbuf.WriteString(exampleMailPlainUnsupportedTransferEnc) _, err = EMLToMsgFromReader(mailbuf) if err == nil { @@ -800,17 +796,6 @@ func TestEMLToMsgFromFileFailing(t *testing.T) { if err = os.RemoveAll(tempDir); err != nil { t.Error("failed to remove temp dir:", err) } - tempDir, tempFile, err = stringToTempFile(exampleMailPlainNoContentType, "testmail") - if err != nil { - t.Errorf("failed to write EML string to temp file: %s", err) - } - _, err = EMLToMsgFromFile(tempFile) - if err == nil { - t.Error("EML from Reader with no content type was supposed to fail, but didn't") - } - if err = os.RemoveAll(tempDir); err != nil { - t.Error("failed to remove temp dir:", err) - } tempDir, tempFile, err = stringToTempFile(exampleMailPlainUnsupportedTransferEnc, "testmail") if err != nil { t.Errorf("failed to write EML string to temp file: %s", err)