mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-23 22:20:51 +01:00
Compare commits
No commits in common. "4c88dce2a854e46cc665ebd5f6502bae57d512c7" and "52ef13a5d54f2b9986be9ca0f496b74e764578eb" have entirely different histories.
4c88dce2a8
...
52ef13a5d5
2 changed files with 15 additions and 71 deletions
35
eml.go
35
eml.go
|
@ -70,7 +70,6 @@ func EMLToMsgFromFile(filePath string) (*Msg, error) {
|
|||
if err = parseEMLBodyParts(parsedMsg, bodybuf, msg); err != nil {
|
||||
return msg, fmt.Errorf("failed to parse EML body parts: %w", err)
|
||||
}
|
||||
//fmt.Printf("FOO: %+v\n", msg)
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
@ -160,10 +159,6 @@ func parseEMLHeaders(mailHeader *netmail.Header, msg *Msg) error {
|
|||
// Extract common headers
|
||||
for _, header := range commonHeaders {
|
||||
if value := mailHeader.Get(header.String()); value != "" {
|
||||
if strings.EqualFold(header.String(), HeaderContentType.String()) &&
|
||||
strings.HasPrefix(value, TypeMultipartMixed.String()) {
|
||||
continue
|
||||
}
|
||||
msg.SetGenHeader(header, value)
|
||||
}
|
||||
}
|
||||
|
@ -189,10 +184,9 @@ func parseEMLBodyParts(parsedMsg *netmail.Message, bodybuf *bytes.Buffer, msg *M
|
|||
return fmt.Errorf("failed to parse plain body: %w", err)
|
||||
}
|
||||
case strings.EqualFold(mediatype, TypeMultipartAlternative.String()),
|
||||
strings.EqualFold(mediatype, TypeMultipartMixed.String()),
|
||||
strings.EqualFold(mediatype, TypeMultipartRelated.String()):
|
||||
strings.EqualFold(mediatype, TypeMultipartMixed.String()):
|
||||
if err = parseEMLMultipart(params, bodybuf, msg); err != nil {
|
||||
return fmt.Errorf("failed to parse multipart body: %w", err)
|
||||
return fmt.Errorf("failed to parse multipart/alternative body: %w", err)
|
||||
}
|
||||
default:
|
||||
}
|
||||
|
@ -248,27 +242,9 @@ ReadNextPart:
|
|||
return fmt.Errorf("failed to get next part of multipart message: %w", err)
|
||||
}
|
||||
for err == nil {
|
||||
// Multipart/related parts need to be parsed seperately
|
||||
if contentTypeSlice, ok := multiPart.Header[HeaderContentType.String()]; ok && len(contentTypeSlice) == 1 {
|
||||
contentType, _ := parseMultiPartHeader(contentTypeSlice[0])
|
||||
if strings.EqualFold(contentType, TypeMultipartRelated.String()) {
|
||||
relatedPart := &netmail.Message{
|
||||
Header: netmail.Header(multiPart.Header),
|
||||
Body: multiPart,
|
||||
}
|
||||
relatedBuf := &bytes.Buffer{}
|
||||
if _, err = relatedBuf.ReadFrom(multiPart); err != nil {
|
||||
return fmt.Errorf("failed to read related multipart message to buffer: %w", err)
|
||||
}
|
||||
if err := parseEMLBodyParts(relatedPart, relatedBuf, msg); err != nil {
|
||||
return fmt.Errorf("failed to parse related multipart body: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Content-Disposition header means we have an attachment or embed
|
||||
if contentDisposition, ok := multiPart.Header[HeaderContentDisposition.String()]; ok {
|
||||
if err = parseEMLAttachmentEmbed(contentDisposition, multiPart, msg); err != nil {
|
||||
if err := parseEMLAttachmentEmbed(contentDisposition, multiPart, msg); err != nil {
|
||||
return fmt.Errorf("failed to parse attachment/embed: %w", err)
|
||||
}
|
||||
goto ReadNextPart
|
||||
|
@ -285,9 +261,6 @@ ReadNextPart:
|
|||
return fmt.Errorf("failed to get content-type from part")
|
||||
}
|
||||
contentType, optional := parseMultiPartHeader(multiPartContentType[0])
|
||||
if strings.EqualFold(contentType, TypeMultipartRelated.String()) {
|
||||
goto ReadNextPart
|
||||
}
|
||||
part := msg.newPart(ContentType(contentType))
|
||||
if charset, ok := optional["charset"]; ok {
|
||||
part.SetCharset(Charset(charset))
|
||||
|
@ -343,7 +316,7 @@ func parseEMLContentTypeCharset(mailHeader *netmail.Header, msg *Msg) {
|
|||
msg.SetCharset(Charset(charset))
|
||||
}
|
||||
msg.setEncoder()
|
||||
if contentType != "" && !strings.EqualFold(contentType, TypeMultipartMixed.String()) {
|
||||
if contentType != "" {
|
||||
msg.SetGenHeader(HeaderContentType, contentType)
|
||||
}
|
||||
}
|
||||
|
|
51
eml_test.go
51
eml_test.go
|
@ -152,33 +152,6 @@ ICAgc2V2ZXJhbAogICAgICAgICAgICBuZXdsaW5lcwoJICAgICAgICAgICAgYW5kCgkgICAgc3Bh
|
|||
Y2VzCiAgICAgaW4KICBpdAouCgpBcyB3ZWxsIGFzIGFuIGVtb2ppOiDwn5mCCg==
|
||||
|
||||
--45c75ff528359022eb03679fbe91877d75343f2e1f8193e349deffa33ff7--`
|
||||
exampleMailPlainB64WithEmbed = `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 base64 with embed
|
||||
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" <go-mail@go-mail.dev>
|
||||
To: <go-mail+test@go-mail.dev>
|
||||
Cc: <go-mail+cc@go-mail.dev>
|
||||
Content-Type: multipart/related;
|
||||
boundary=ffbcfb94b44e5297325102f6ced05b3b37f1d70fc38a5e78dc73c1a8434b
|
||||
|
||||
--ffbcfb94b44e5297325102f6ced05b3b37f1d70fc38a5e78dc73c1a8434b
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
|
||||
This is a test body string
|
||||
--ffbcfb94b44e5297325102f6ced05b3b37f1d70fc38a5e78dc73c1a8434b
|
||||
Content-Disposition: inline; filename="pixel.png"
|
||||
Content-Id: <pixel.png>
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Type: image/png; name="pixel.png"
|
||||
|
||||
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIW2NgYGD4DwABBAEAwS2O
|
||||
UAAAAABJRU5ErkJggg==
|
||||
|
||||
--ffbcfb94b44e5297325102f6ced05b3b37f1d70fc38a5e78dc73c1a8434b--`
|
||||
)
|
||||
|
||||
func TestEMLToMsgFromString(t *testing.T) {
|
||||
|
@ -305,20 +278,18 @@ func TestEMLToMsgFromStringWithAttachment(t *testing.T) {
|
|||
t.Errorf("EMLToMsgFromString of EML with attachment failed: expected no. of attachments: %d, but got: %d",
|
||||
1, len(msg.attachments))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEMLToMsgFromStringWithEmbed(t *testing.T) {
|
||||
wantSubject := "Example mail // plain text base64 with embed"
|
||||
msg, err := EMLToMsgFromString(exampleMailPlainB64WithEmbed)
|
||||
if err != nil {
|
||||
t.Errorf("EML with embed failed: %s", err)
|
||||
contentTypeHeader := msg.GetGenHeader(HeaderContentType)
|
||||
if len(contentTypeHeader) != 1 {
|
||||
t.Errorf("EMLToMsgFromString of EML with attachment failed: expected no. of content-type header: %d, "+
|
||||
"but got: %d", 1, len(contentTypeHeader))
|
||||
}
|
||||
if subject := msg.GetGenHeader(HeaderSubject); len(subject) > 0 && !strings.EqualFold(subject[0], wantSubject) {
|
||||
t.Errorf("EMLToMsgFromString of EML with embed failed: expected subject: %s, but got: %s",
|
||||
wantSubject, subject[0])
|
||||
contentTypeSplit := strings.SplitN(contentTypeHeader[0], "; ", 2)
|
||||
if len(contentTypeSplit) != 2 {
|
||||
t.Error("failed to split Content-Type header")
|
||||
return
|
||||
}
|
||||
if len(msg.embeds) != 1 {
|
||||
t.Errorf("EMLToMsgFromString of EML with embed failed: expected no. of embeds: %d, but got: %d",
|
||||
1, len(msg.attachments))
|
||||
if !strings.EqualFold(contentTypeSplit[0], "multipart/mixed") {
|
||||
t.Errorf("EMLToMsgFromString of EML with attachment failed: expected content-type: %s, "+
|
||||
"but got: %s", "multipart/mixed", contentTypeSplit[0])
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue