Merge pull request #272 from wneessen/bug/271_parsing-rfc5322-example-a11-fails

Refactor: Handle missing Content-Type as plain text
This commit is contained in:
Winni Neessen 2024-08-02 15:39:23 +02:00 committed by GitHub
commit f701b233be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 30 deletions

10
eml.go
View file

@ -180,7 +180,15 @@ func parseEMLBodyParts(parsedMsg *netmail.Message, bodybuf *bytes.Buffer, msg *M
// Extract the transfer encoding of the body // Extract the transfer encoding of the body
mediatype, params, err := mime.ParseMediaType(parsedMsg.Header.Get(HeaderContentType.String())) mediatype, params, err := mime.ParseMediaType(parsedMsg.Header.Get(HeaderContentType.String()))
if err != nil { 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 { if value, ok := params["charset"]; ok {
msg.SetCharset(Charset(value)) msg.SetCharset(Charset(value))

View file

@ -14,6 +14,16 @@ import (
) )
const ( const (
// RFC 5322 example mail
// See: https://datatracker.ietf.org/doc/html/rfc5322#appendix-A.1.1
exampleMailRFC5322A11 = `From: John Doe <jdoe@machine.example>
To: Mary Smith <mary@example.net>
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 exampleMailPlainNoEnc = `Date: Wed, 01 Nov 2023 00:00:00 +0000
MIME-Version: 1.0 MIME-Version: 1.0
Message-ID: <1305604950.683004066175.AAAAAAAAaaaaaaaaB@go-mail.dev> Message-ID: <1305604950.683004066175.AAAAAAAAaaaaaaaaB@go-mail.dev>
@ -72,18 +82,6 @@ Cc: <go-mail+cc@go-mail.dev>
Content-Type: text/plain; charset=UTF-8 Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64 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" <go-mail@go-mail.dev>
To: <go-mail+test@go-mail.dev>
Cc: <go-mail+cc@go-mail.dev>
This plain text body should not be parsed as Base64. This plain text body should not be parsed as Base64.
` `
exampleMailPlainUnknownContentType = `Date: Wed, 01 Nov 2023 00:00:00 +0000 exampleMailPlainUnknownContentType = `Date: Wed, 01 Nov 2023 00:00:00 +0000
@ -623,6 +621,10 @@ func TestEMLToMsgFromString(t *testing.T) {
enc string enc string
sub string sub string
}{ }{
{
"RFC5322 A1.1", exampleMailRFC5322A11, "7bit",
"Saying Hello",
},
{ {
"Plain text no encoding (7bit)", exampleMailPlain7Bit, "7bit", "Plain text no encoding (7bit)", exampleMailPlain7Bit, "7bit",
"Example mail // plain text without encoding", "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") t.Error("EML from Reader with unknown content type was supposed to fail, but didn't")
} }
mailbuf.Reset() 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) mailbuf.WriteString(exampleMailPlainUnsupportedTransferEnc)
_, err = EMLToMsgFromReader(mailbuf) _, err = EMLToMsgFromReader(mailbuf)
if err == nil { if err == nil {
@ -800,17 +796,6 @@ func TestEMLToMsgFromFileFailing(t *testing.T) {
if err = os.RemoveAll(tempDir); err != nil { if err = os.RemoveAll(tempDir); err != nil {
t.Error("failed to remove temp dir:", err) 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") tempDir, tempFile, err = stringToTempFile(exampleMailPlainUnsupportedTransferEnc, "testmail")
if err != nil { if err != nil {
t.Errorf("failed to write EML string to temp file: %s", err) t.Errorf("failed to write EML string to temp file: %s", err)