mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-25 15:10:48 +01:00
Making slight progress on EML parsing
This commit is contained in:
parent
d8d2a6e714
commit
d733b6e17d
1 changed files with 31 additions and 11 deletions
24
eml.go
24
eml.go
|
@ -3,6 +3,7 @@ package mail
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
nm "net/mail"
|
||||
"os"
|
||||
)
|
||||
|
@ -22,10 +23,19 @@ func EMLToMsg(fp string) (*Msg, error) {
|
|||
return m, fmt.Errorf("failed to parse EML file: %w", err)
|
||||
}
|
||||
|
||||
// Parse the header
|
||||
if err := parseEMLHeaders(&pm.Header, m); err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -45,6 +55,8 @@ func readEML(fp string) (*nm.Message, error) {
|
|||
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 {
|
||||
commonHeaders := []Header{
|
||||
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)
|
||||
}
|
||||
}
|
||||
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
|
||||
pal, err := nm.ParseAddressList(v)
|
||||
if err != nil {
|
||||
|
@ -68,10 +86,11 @@ func parseEMLHeaders(mh *nm.Header, m *Msg) error {
|
|||
for _, a := range pal {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract date from message
|
||||
d, err := mh.Date()
|
||||
|
@ -93,5 +112,6 @@ func parseEMLHeaders(mh *nm.Header, m *Msg) error {
|
|||
m.SetGenHeader(h, v)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue