Making slight progress on EML parsing

This commit is contained in:
Winni Neessen 2023-09-21 13:50:36 +02:00
parent d8d2a6e714
commit d733b6e17d
Signed by: wneessen
GPG key ID: 385AC9889632126E

24
eml.go
View file

@ -3,6 +3,7 @@ package mail
import ( import (
"errors" "errors"
"fmt" "fmt"
"mime"
nm "net/mail" nm "net/mail"
"os" "os"
) )
@ -22,10 +23,19 @@ func EMLToMsg(fp string) (*Msg, error) {
return m, fmt.Errorf("failed to parse EML file: %w", err) return m, fmt.Errorf("failed to parse EML file: %w", err)
} }
// Parse the header
if err := parseEMLHeaders(&pm.Header, m); err != nil { if err := parseEMLHeaders(&pm.Header, m); err != nil {
return m, fmt.Errorf("failed to parse EML headers: %w", err) return m, fmt.Errorf("failed to parse EML headers: %w", err)
} }
// Extract the transfer encoding of the body
x, y, err := mime.ParseMediaType(pm.Header.Get(HeaderContentType.String()))
if err != nil {
return m, fmt.Errorf("failed to extract content type: %w", err)
}
fmt.Printf("Encoding: %s\n", x)
fmt.Printf("Params: %+v\n", y)
return m, nil return m, nil
} }
@ -45,6 +55,8 @@ func readEML(fp string) (*nm.Message, error) {
return pm, nil return pm, nil
} }
// parseEMLHeaders will check the EML headers for the most common headers and set the
// according settings in the Msg
func parseEMLHeaders(mh *nm.Header, m *Msg) error { func parseEMLHeaders(mh *nm.Header, m *Msg) error {
commonHeaders := []Header{ commonHeaders := []Header{
HeaderContentType, HeaderImportance, HeaderInReplyTo, HeaderListUnsubscribe, HeaderContentType, HeaderImportance, HeaderInReplyTo, HeaderListUnsubscribe,
@ -59,7 +71,13 @@ func parseEMLHeaders(mh *nm.Header, m *Msg) error {
return fmt.Errorf(`failed to parse "From:" header: %w`, err) return fmt.Errorf(`failed to parse "From:" header: %w`, err)
} }
} }
if v := mh.Get(HeaderTo.String()); v != "" { ahl := map[AddrHeader]func(...string) error{
HeaderTo: m.To,
HeaderCc: m.Cc,
HeaderBcc: m.Bcc,
}
for h, f := range ahl {
if v := mh.Get(h.String()); v != "" {
var als []string var als []string
pal, err := nm.ParseAddressList(v) pal, err := nm.ParseAddressList(v)
if err != nil { if err != nil {
@ -68,10 +86,11 @@ func parseEMLHeaders(mh *nm.Header, m *Msg) error {
for _, a := range pal { for _, a := range pal {
als = append(als, a.String()) als = append(als, a.String())
} }
if err := m.To(als...); err != nil { if err := f(als...); err != nil {
return fmt.Errorf(`failed to parse "To:" header: %w`, err) return fmt.Errorf(`failed to parse "To:" header: %w`, err)
} }
} }
}
// Extract date from message // Extract date from message
d, err := mh.Date() d, err := mh.Date()
@ -93,5 +112,6 @@ func parseEMLHeaders(mh *nm.Header, m *Msg) error {
m.SetGenHeader(h, v) m.SetGenHeader(h, v)
} }
} }
return nil return nil
} }