Extract and set charset from email content type

The diff modifies how the email library handles the extraction of the mime media type from an email header. It uses the mime.ParseMediaType function to parse the content type header. The function gives back the media type as a string and a mapping of different associated parameters. This mapping was previously just printed, but now the charset parameter is also used for setting the charset of the email if it exists.
This commit is contained in:
Winni Neessen 2023-09-27 11:29:58 +02:00
parent d733b6e17d
commit 3d50370a4c
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D

9
eml.go
View file

@ -29,12 +29,15 @@ func EMLToMsg(fp string) (*Msg, error) {
} }
// Extract the transfer encoding of the body // Extract the transfer encoding of the body
x, y, err := mime.ParseMediaType(pm.Header.Get(HeaderContentType.String())) mi, ar, err := mime.ParseMediaType(pm.Header.Get(HeaderContentType.String()))
if err != nil { if err != nil {
return m, fmt.Errorf("failed to extract content type: %w", err) return m, fmt.Errorf("failed to extract content type: %w", err)
} }
fmt.Printf("Encoding: %s\n", x) if v, ok := ar["charset"]; ok {
fmt.Printf("Params: %+v\n", y) m.SetCharset(Charset(v))
}
fmt.Printf("Encoding: %s\n", mi)
fmt.Printf("Params: %+v\n", ar)
return m, nil return m, nil
} }