-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from wneessen/feature/262_support-7bit-encoding
Add 7bit support for EML parsing
- Loading branch information
Showing
4 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,29 @@ This is a test mail. Please do not reply to this. Also this line is very long so | |
should be wrapped. | ||
Thank your for your business! | ||
The go-mail team | ||
-- | ||
This is a signature` | ||
exampleMailPlain7Bit = `Date: Wed, 01 Nov 2023 00:00:00 +0000 | ||
MIME-Version: 1.0 | ||
Message-ID: <[email protected]> | ||
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" <[email protected]> | ||
To: <[email protected]> | ||
Cc: <[email protected]> | ||
Content-Type: text/plain; charset=UTF-8 | ||
Content-Transfer-Encoding: 7bit | ||
Dear Customer, | ||
This is a test mail. Please do not reply to this. Also this line is very long so it | ||
should be wrapped. | ||
Thank your for your business! | ||
The go-mail team | ||
|
@@ -525,6 +548,72 @@ hw22iFHl7YlpOmedZvtMTfQffXeXnvI+rTKNxguyvDKvB7U4qQAAAAlwSFlzAAALEwAACxMBAJqc | |
GAAAABFJREFUCJljnMoAA0wMNGcCAEQrAKk9oHKhAAAAAElFTkSuQmCC | ||
--fe785e0384e2607697cc2ecb17cce003003bb7ca9112104f3e8ce727edb5--` | ||
exampleMultiPart7BitBase64 = `Date: Wed, 01 Nov 2023 00:00:00 +0000 | ||
MIME-Version: 1.0 | ||
Message-ID: <[email protected]> | ||
Subject: Example mail // 7bit with base64 attachment | ||
User-Agent: go-mail v0.4.1 // https://github.com/wneessen/go-mail | ||
X-Mailer: go-mail v0.4.1 // https://github.com/wneessen/go-mail | ||
From: "Toni Tester" <[email protected]> | ||
To: <[email protected]> | ||
Cc: <[email protected]> | ||
Content-Type: multipart/mixed; | ||
boundary="------------26A45336F6C6196BD8BBA2A2" | ||
This is a multi-part message in MIME format. | ||
--------------26A45336F6C6196BD8BBA2A2 | ||
Content-Type: text/plain; charset=US-ASCII; format=flowed | ||
Content-Transfer-Encoding: 7bit | ||
testtest | ||
testtest | ||
testtest | ||
testtest | ||
testtest | ||
testtest | ||
--------------26A45336F6C6196BD8BBA2A2 | ||
Content-Type: text/plain; charset=UTF-8; | ||
name="testfile.txt" | ||
Content-Transfer-Encoding: base64 | ||
Content-Disposition: attachment; | ||
filename="testfile.txt" | ||
VGhpcyBpcyBhIHRlc3QgaW4gQmFzZTY0 | ||
--------------26A45336F6C6196BD8BBA2A2--` | ||
exampleMultiPart8BitBase64 = `Date: Wed, 01 Nov 2023 00:00:00 +0000 | ||
MIME-Version: 1.0 | ||
Message-ID: <[email protected]> | ||
Subject: Example mail // 8bit with base64 attachment | ||
User-Agent: go-mail v0.4.1 // https://github.com/wneessen/go-mail | ||
X-Mailer: go-mail v0.4.1 // https://github.com/wneessen/go-mail | ||
From: "Toni Tester" <[email protected]> | ||
To: <[email protected]> | ||
Cc: <[email protected]> | ||
Content-Type: multipart/mixed; | ||
boundary="------------26A45336F6C6196BD8BBA2A2" | ||
This is a multi-part message in MIME format. | ||
--------------26A45336F6C6196BD8BBA2A2 | ||
Content-Type: text/plain; charset=US-ASCII; format=flowed | ||
Content-Transfer-Encoding: 8bit | ||
testtest | ||
testtest | ||
testtest | ||
testtest | ||
testtest | ||
testtest | ||
--------------26A45336F6C6196BD8BBA2A2 | ||
Content-Type: text/plain; charset=UTF-8; | ||
name="testfile.txt" | ||
Content-Transfer-Encoding: base64 | ||
Content-Disposition: attachment; | ||
filename="testfile.txt" | ||
VGhpcyBpcyBhIHRlc3QgaW4gQmFzZTY0 | ||
--------------26A45336F6C6196BD8BBA2A2--` | ||
) | ||
|
||
func TestEMLToMsgFromString(t *testing.T) { | ||
|
@@ -534,6 +623,10 @@ func TestEMLToMsgFromString(t *testing.T) { | |
enc string | ||
sub string | ||
}{ | ||
{ | ||
"Plain text no encoding (7bit)", exampleMailPlain7Bit, "7bit", | ||
"Example mail // plain text without encoding", | ||
}, | ||
{ | ||
"Plain text no encoding", exampleMailPlainNoEnc, "8bit", | ||
"Example mail // plain text without encoding", | ||
|
@@ -866,6 +959,58 @@ func TestEMLToMsgFromStringMultipartMixedAlternativeRelated(t *testing.T) { | |
} | ||
} | ||
|
||
func TestEMLToMsgFromStringMultipartMixedWith7Bit(t *testing.T) { | ||
wantSubject := "Example mail // 7bit with base64 attachment" | ||
msg, err := EMLToMsgFromString(exampleMultiPart7BitBase64) | ||
if err != nil { | ||
t.Errorf("EML multipart mixed with 7bit: %s", err) | ||
} | ||
if subject := msg.GetGenHeader(HeaderSubject); len(subject) > 0 && !strings.EqualFold(subject[0], wantSubject) { | ||
t.Errorf("EMLToMsgFromString of EML multipart mixed with 7bit: expected subject: %s,"+ | ||
" but got: %s", wantSubject, subject[0]) | ||
} | ||
if len(msg.parts) != 1 { | ||
t.Errorf("EMLToMsgFromString of EML multipart mixed with 7bit failed: expected 1 part, got: %d", | ||
len(msg.parts)) | ||
return | ||
} | ||
if !strings.EqualFold(msg.parts[0].GetEncoding().String(), EncodingUSASCII.String()) { | ||
t.Errorf("EMLToMsgFromString of EML multipart mixed with 7bit failed: expected encoding: %s, got %s", | ||
EncodingUSASCII.String(), msg.parts[0].GetEncoding().String()) | ||
} | ||
if len(msg.attachments) != 1 { | ||
t.Errorf("EMLToMsgFromString of EML multipart mixed with 7bit failed: expected 1 attachment, got: %d", | ||
len(msg.attachments)) | ||
return | ||
} | ||
} | ||
|
||
func TestEMLToMsgFromStringMultipartMixedWith8Bit(t *testing.T) { | ||
wantSubject := "Example mail // 8bit with base64 attachment" | ||
msg, err := EMLToMsgFromString(exampleMultiPart8BitBase64) | ||
if err != nil { | ||
t.Errorf("EML multipart mixed with 8bit: %s", err) | ||
} | ||
if subject := msg.GetGenHeader(HeaderSubject); len(subject) > 0 && !strings.EqualFold(subject[0], wantSubject) { | ||
t.Errorf("EMLToMsgFromString of EML multipart mixed with 8bit: expected subject: %s,"+ | ||
" but got: %s", wantSubject, subject[0]) | ||
} | ||
if len(msg.parts) != 1 { | ||
t.Errorf("EMLToMsgFromString of EML multipart mixed with 8bit failed: expected 1 part, got: %d", | ||
len(msg.parts)) | ||
return | ||
} | ||
if !strings.EqualFold(msg.parts[0].GetEncoding().String(), NoEncoding.String()) { | ||
t.Errorf("EMLToMsgFromString of EML multipart mixed with 8bit failed: expected encoding: %s, got %s", | ||
NoEncoding.String(), msg.parts[0].GetEncoding().String()) | ||
} | ||
if len(msg.attachments) != 1 { | ||
t.Errorf("EMLToMsgFromString of EML multipart mixed with 8bit failed: expected 1 attachment, got: %d", | ||
len(msg.attachments)) | ||
return | ||
} | ||
} | ||
|
||
// stringToTempFile is a helper method that will create a temporary file form a give data string | ||
func stringToTempFile(data, name string) (string, string, error) { | ||
tempDir, err := os.MkdirTemp("", fmt.Sprintf("*-%s", name)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters