#145: Support for quoted-printable encoding in email parser

Added support for quoted-printable encoding in email parser to increase its functionality. The change includes a case handling feature for 'EncodingQP' and related conversions to allow for proper message body reading and encoding setting. This improves the robustness and the scope of email content types that the parser can handle."
This commit is contained in:
Winni Neessen 2023-10-13 16:04:07 +02:00
parent e7c717d0fc
commit a3b3deb467
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D

25
eml.go
View file

@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"mime"
"mime/quotedprintable"
nm "net/mail"
"os"
"strings"
@ -132,16 +133,24 @@ func parseEMLBodyParts(pm *nm.Message, mbbuf *bytes.Buffer, m *Msg) error {
m.SetCharset(Charset(v))
}
if cte := pm.Header.Get(HeaderContentTransferEnc.String()); cte != "" {
switch strings.ToLower(cte) {
case NoEncoding.String():
m.SetEncoding(NoEncoding)
}
}
cte := pm.Header.Get(HeaderContentTransferEnc.String())
switch strings.ToLower(mt) {
case TypeTextPlain.String():
m.SetBodyString(TypeTextPlain, mbbuf.String())
if cte == NoEncoding.String() {
m.SetEncoding(NoEncoding)
m.SetBodyString(TypeTextPlain, mbbuf.String())
break
}
if cte == EncodingQP.String() {
m.SetEncoding(EncodingQP)
qpr := quotedprintable.NewReader(mbbuf)
qpbuf := bytes.Buffer{}
if _, err := qpbuf.ReadFrom(qpr); err != nil {
return fmt.Errorf("failed to read quoted-printable body: %w", err)
}
m.SetBodyString(TypeTextPlain, qpbuf.String())
break
}
default:
}
return nil