I need to parse a multi-part email files read from unix maildir. Can you please suggest an appropriate library(s) to do this?
The emails are sucked in via IMAP and dumped to a maildir. I need to parse those email files and extract all the parts including header, base64 attachments, html parts and plaintext parts.
Thanks
EDIT
I know I can search for the libraries with keywords and stuff but I also would like some opinions on quality and experience if possible.
I can deal with the actual maildir and picking up mail files. My concern is the parsing of the multipart emails (being fed as strings) and extracting individual parts.
Here is my example: The part missing is extracting the attachments. Please let me know if you figured that part out... Been scratching my head to extract attachments for weeks now...
import (
"fmt"
"io/ioutil"
"net/mail"
)
func extractEmail(mail *mail.Message){
header := mail.Header
fmt.Println(header.Get("Date"))
fmt.Println(header.Get("From"))
fmt.Println(header.Get("To"))
fmt.Println(header.Get("cc"))
fmt.Println(header.Get("bcc"))
fmt.Println(header.Get("Subject"))
body, err := ioutil.ReadAll(mail.Body)
if err != nil {
checkErr(err, "Reading Body")
}
fmt.Println(body)
}
I've had some luck doing this with the github.com/jhillyerd/enmime package. Given an io.Reader
r
:
// Parse message body
env, _ := enmime.ReadEnvelope(r)
// Headers can be retrieved via Envelope.GetHeader(name).
fmt.Printf("From: %v
", env.GetHeader("From"))
// Address-type headers can be parsed into a list of decoded mail.Address structs.
alist, _ := env.AddressList("To")
for _, addr := range alist {
fmt.Printf("To: %s <%s>
", addr.Name, addr.Address)
}
fmt.Printf("Subject: %v
", env.GetHeader("Subject"))
// The plain text body is available as mime.Text.
fmt.Printf("Text Body: %v chars
", len(env.Text))
// The HTML body is stored in mime.HTML.
fmt.Printf("HTML Body: %v chars
", len(env.HTML))
// mime.Inlines is a slice of inlined attacments.
fmt.Printf("Inlines: %v
", len(env.Inlines))
// mime.Attachments contains the non-inline attachments.
fmt.Printf("Attachments: %v
", len(env.Attachments))