Add base64 encoding support to email attachments

The updated code adds base64 encoding support to email attachments and inline content in eml.go. It does this by introducing a new dataReader which uses a base64 decoder if the content transfer encoding is base64. With this update, attachments with base64 content will be correctly decoded when processed.
This commit is contained in:
Winni Neessen 2024-06-28 11:54:30 +02:00
parent b709df4b2d
commit 84e6275cb2
Signed by: wneessen
GPG key ID: 385AC9889632126E

19
eml.go
View file

@ -388,13 +388,28 @@ func parseEMLAttachmentEmbed(contentDisposition []string, multiPart *multipart.P
if name, ok := optional["filename"]; ok { if name, ok := optional["filename"]; ok {
filename = name[1 : len(name)-1] filename = name[1 : len(name)-1]
} }
var dataReader io.Reader
dataReader = multiPart
contentTransferEnc, _ := parseMultiPartHeader(multiPart.Header.Get(HeaderContentTransferEnc.String()))
b64Decoder := base64.NewDecoder(base64.StdEncoding, multiPart)
if strings.EqualFold(contentTransferEnc, EncodingB64.String()) {
dataReader = b64Decoder
}
switch strings.ToLower(cdType) { switch strings.ToLower(cdType) {
case "attachment": case "attachment":
if err := msg.AttachReader(filename, multiPart); err != nil { if err := msg.AttachReader(filename, dataReader); err != nil {
return fmt.Errorf("failed to attach multipart body: %w", err) return fmt.Errorf("failed to attach multipart body: %w", err)
} }
case "inline": case "inline":
if err := msg.EmbedReader(filename, multiPart); err != nil { if contentID, _ := parseMultiPartHeader(multiPart.Header.Get(HeaderContentID.String())); contentID != "" {
if err := msg.EmbedReader(filename, dataReader, WithContentID(contentID)); err != nil {
return fmt.Errorf("failed to embed multipart body: %w", err)
}
return nil
}
if err := msg.EmbedReader(filename, dataReader); err != nil {
return fmt.Errorf("failed to embed multipart body: %w", err) return fmt.Errorf("failed to embed multipart body: %w", err)
} }
} }