From 3facbde703050ac84b047d7b2332b51e03895c6d Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 10 Feb 2024 13:36:42 +0100 Subject: [PATCH] Add new content types and refactor message writer Introduced "multipart/mixed" and "multipart/related" content types in encoding.go and updated msgwriter.go to accommodate these. Adjustments made in related tests for these new types. Additionally, removed unnecessary print statements and improved multipart alternative parsing in eml.go. --- eml.go | 7 ++----- encoding.go | 2 ++ encoding_test.go | 12 ++++++++++++ msgwriter.go | 4 ++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/eml.go b/eml.go index 632102d..bc96c0a 100644 --- a/eml.go +++ b/eml.go @@ -178,9 +178,6 @@ func parseEMLHeaders(mh *nm.Header, m *Msg) error { for _, h := range commonHeaders { if v := mh.Get(h.String()); v != "" { m.SetGenHeader(h, v) - if strings.EqualFold(h.String(), "subject") { - fmt.Printf("SUBJECT: %s\n", m.GetGenHeader(HeaderSubject)[0]) - } } } @@ -204,7 +201,8 @@ func parseEMLBodyParts(pm *nm.Message, bodybuf *bytes.Buffer, m *Msg) error { if err := parseEMLBodyPlain(mediatype, pm, bodybuf, m); err != nil { return fmt.Errorf("failed to parse plain body: %w", err) } - case strings.EqualFold(mediatype, TypeMultipartAlternative.String()): + case strings.EqualFold(mediatype, TypeMultipartAlternative.String()), + strings.EqualFold(mediatype, "multipart/mixed"): if err := parseEMLMultipartAlternative(params, bodybuf, m); err != nil { return fmt.Errorf("failed to parse multipart/alternative body: %w", err) } @@ -261,7 +259,6 @@ func parseEMLMultipartAlternative(params map[string]string, bodybuf *bytes.Buffe _ = mpart.Close() return fmt.Errorf("failed to read multipart: %w", err) } - fmt.Printf("CTE: %+v", params) mpContentType, ok := mpart.Header[HeaderContentType.String()] if !ok { diff --git a/encoding.go b/encoding.go index 39c58e3..2187e5f 100644 --- a/encoding.go +++ b/encoding.go @@ -140,6 +140,8 @@ const ( const ( TypeAppOctetStream ContentType = "application/octet-stream" TypeMultipartAlternative ContentType = "multipart/alternative" + TypeMultipartMixed ContentType = "multipart/mixed" + TypeMultipartRelated ContentType = "multipart/related" TypePGPSignature ContentType = "application/pgp-signature" TypePGPEncrypted ContentType = "application/pgp-encrypted" TypeTextHTML ContentType = "text/html" diff --git a/encoding_test.go b/encoding_test.go index 4a3affc..86f686a 100644 --- a/encoding_test.go +++ b/encoding_test.go @@ -40,6 +40,18 @@ func TestContentType_String(t *testing.T) { "ContentType: application/octet-stream", TypeAppOctetStream, "application/octet-stream", }, + { + "ContentType: multipart/alternative", TypeMultipartAlternative, + "multipart/alternative", + }, + { + "ContentType: multipart/mixed", TypeMultipartMixed, + "multipart/mixed", + }, + { + "ContentType: multipart/related", TypeMultipartRelated, + "multipart/related", + }, { "ContentType: application/pgp-signature", TypePGPSignature, "application/pgp-signature", diff --git a/msgwriter.go b/msgwriter.go index b2ff118..377435e 100644 --- a/msgwriter.go +++ b/msgwriter.go @@ -89,11 +89,11 @@ func (mw *msgWriter) writeMsg(m *Msg) { } if m.hasMixed() { - mw.startMP("mixed", m.boundary) + mw.startMP(MIMEMixed, m.boundary) mw.writeString(DoubleNewLine) } if m.hasRelated() { - mw.startMP("related", m.boundary) + mw.startMP(MIMERelated, m.boundary) mw.writeString(DoubleNewLine) } if m.hasAlt() {