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.
This commit is contained in:
Winni Neessen 2024-06-19 10:52:09 +02:00
parent 04c41a1f10
commit 29305675d6
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D

50
eml.go
View file

@ -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)