I\'m trying to parse emails and I get this kind of errors using the mail package. Is it a bug on the mail package or something I should handle myself ?
missing
Alexey Vasiliev's MIT-licensed http://github.com/le0pard/go-falcon/ includes a parser
package that applies whichever encoding package is needed to decode the headers (the meat is in utils.go).
package main
import (
"bufio"
"bytes"
"fmt"
"net/textproto"
"github.com/le0pard/go-falcon/parser"
)
var msg = []byte(`Subject: =?gb18030?B?u9i4tKO6ILvYuLSjulBhbGFjZSBXZXN0bWluc3Rl?=
=?gb18030?B?cjogMDEtMDctMjAxNCAtIDA0LTA3LTIwMTQ=?=
`)
func main() {
tpr := textproto.NewReader(bufio.NewReader(bytes.NewBuffer(msg)))
mh, err := tpr.ReadMIMEHeader()
if err != nil {
panic(err)
}
for name, vals := range mh {
for _, val := range vals {
val = parser.MimeHeaderDecode(val)
fmt.Print(name, ": ", val, "\n")
}
}
}
It looks like its parser.FixEncodingAndCharsetOfPart is used by the package to decode/convert content as well, though with a couple of extra allocations caused by converting the []byte
body to/from a string
. If you don't find the API works for you, you might at least be able to use the code to see how it can be done.
Found via godoc.org's "...and is imported by 3 packages" link from encoding/simplifiedchinese -- hooray godoc.org!