Compare commits

..

No commits in common. "f701b233bec5ba16a0ffbd1c766de346bec9be8a" and "3ca2968158fd85dcf303d0f35afb681331a32955" have entirely different histories.

2 changed files with 30 additions and 23 deletions

8
eml.go
View file

@ -180,16 +180,8 @@ 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 {
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))
}

View file

@ -14,16 +14,6 @@ import (
)
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
MIME-Version: 1.0
Message-ID: <1305604950.683004066175.AAAAAAAAaaaaaaaaB@go-mail.dev>
@ -82,6 +72,18 @@ Cc: <go-mail+cc@go-mail.dev>
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" <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.
`
exampleMailPlainUnknownContentType = `Date: Wed, 01 Nov 2023 00:00:00 +0000
@ -621,10 +623,6 @@ 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",
@ -733,6 +731,12 @@ 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 {
@ -796,6 +800,17 @@ 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)