mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
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:
parent
b709df4b2d
commit
84e6275cb2
1 changed files with 17 additions and 2 deletions
19
eml.go
19
eml.go
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue