From 29305675d67e2a9c2b9bc39b1cc5995fb3d6e768 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 19 Jun 2024 10:52:09 +0200 Subject: [PATCH] Refactor EML encoding and content-type parsing to separate functions The commit includes extraction of blocks of code related to EML message encoding and content-type parsing into their own separate functions. By doing so, it improves code readability and maintainability. --- eml.go | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/eml.go b/eml.go index 9db20ab..b59a2ca 100644 --- a/eml.go +++ b/eml.go @@ -112,26 +112,8 @@ func parseEMLHeaders(mailHeader *netmail.Header, msg *Msg) error { } // Extract content type, charset and encoding first - if value := mailHeader.Get(HeaderContentTransferEnc.String()); value != "" { - switch { - case strings.EqualFold(value, EncodingQP.String()): - msg.SetEncoding(EncodingQP) - case strings.EqualFold(value, EncodingB64.String()): - msg.SetEncoding(EncodingB64) - default: - msg.SetEncoding(NoEncoding) - } - } - if value := mailHeader.Get(HeaderContentType.String()); value != "" { - contentType, charSet := parseContentType(value) - if charSet != "" { - msg.SetCharset(Charset(charSet)) - } - msg.setEncoder() - if contentType != "" { - msg.SetGenHeader(HeaderContentType, contentType) - } - } + parseEMLEncoding(mailHeader, msg) + parseEMLContentTypeCharset(mailHeader, msg) // Extract address headers if value := mailHeader.Get(HeaderFrom.String()); value != "" { @@ -297,6 +279,34 @@ func parseEMLMultipartAlternative(params map[string]string, bodybuf *bytes.Buffe return nil } +// parseEMLEncoding parses and determines the encoding of the message +func parseEMLEncoding(mailHeader *netmail.Header, msg *Msg) { + if value := mailHeader.Get(HeaderContentTransferEnc.String()); value != "" { + switch { + case strings.EqualFold(value, EncodingQP.String()): + msg.SetEncoding(EncodingQP) + case strings.EqualFold(value, EncodingB64.String()): + msg.SetEncoding(EncodingB64) + default: + msg.SetEncoding(NoEncoding) + } + } +} + +// parseEMLContentTypeCharset parses and determines the charset and content type of the message +func parseEMLContentTypeCharset(mailHeader *netmail.Header, msg *Msg) { + if value := mailHeader.Get(HeaderContentType.String()); value != "" { + contentType, charSet := parseContentType(value) + if charSet != "" { + msg.SetCharset(Charset(charSet)) + } + msg.setEncoder() + if contentType != "" { + msg.SetGenHeader(HeaderContentType, contentType) + } + } +} + // handleEMLMultiPartBase64Encoding sets the content body of a base64 encoded Part func handleEMLMultiPartBase64Encoding(multiPartData []byte, part *Part) error { part.SetEncoding(EncodingB64)