“missing word in phrase: charset not supported”, when using the mail package

前端 未结 3 1804
名媛妹妹
名媛妹妹 2021-01-06 11:24

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

3条回答
  •  孤街浪徒
    2021-01-06 12:02

    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!

提交回复
热议问题