我试图解析电子邮件,但是使用邮件包我遇到了这种错误。这是邮件包上的错误还是我应该处理的事情?
missing word in phrase: charset not supported: "gb18030"
charset not supported: "koi8-r"
missing word in phrase: charset not supported: "ks_c_5601-1987"
我该如何解决?我想我应该使用charset,但我不确定如何 。这是电子邮件标题的样子
Received: from smtpbg303.qq.com ([184.105.206.26]) by mx-ha.gmx.net
(mxgmxus001) with ESMTPS (Nemesis) id 0MAOx2-1X2yNC2ZFC-00BaVU for
<sormester@lobbyist.com>; Sat, 14 Jun 2014 18:11:48 +0200
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201307;
t=1402762305; bh=imEvSr8IPsqWTXU63xUHRv+wuQG+Tcz2mPP9ai4rrE4=;
h=X-QQ-FEAT:X-QQ-SSF:X-HAS-ATTACH:X-QQ-BUSINESS-ORIGIN:
X-Originating-IP:In-Reply-To:References:X-QQ-STYLE:X-QQ-mid:From:To:Subject:Mime-Version:Content-Type:Content-Transfer-Encoding:Date:
X-Priority:Message-ID:X-QQ-MIME:X-Mailer:X-QQ-Mailer:
X-QQ-ReplyHash:X-QQ-SENDSIZE:X-QQ-FName:X-QQ-LocalIP;
b=QXs4CveboS8nG6htN9W6amC3X+F7X3ZtFrt6jrjWI+RmbvqBuTCVmX9IlaqCX84H8
n14x2Wp7x4kDYcNRqhe+HjTpf715TTQXc4d40b9e38frC/5qIhpMtYNsD8iEJwRzHW
U3xi8Yq7OCIB303fIpytx8tOjexQpZKSHbJ7ecX0=
X-QQ-FEAT: zaIfg0hwV2pIDflZYPQUsuPPXG5wtRVHJU6PiOYLBBA=
X-QQ-SSF: 00010000000000F000000000000000L
X-HAS-ATTACH: no
X-QQ-BUSINESS-ORIGIN: 2
X-Originating-IP: 180.155.99.102
In-Reply-To: <trinity-b7c6d611-52fd-4afa-b739-2deb243532a6-1402761364579@3capp-mailcom-lxa05>
References: <97e07dab7c2d1a005ed928c4350690e0@hotels-desk.co.uk>,
<tencent_105D3DC11702F53465C0025D@qq.com>
<trinity-b7c6d611-52fd-4afa-b739-2deb243532a6-1402761364579@3capp-mailcom-lxa05>
X-QQ-STYLE:
X-QQ-mid: webmail474t1402762303t356131
From: "=?gb18030?B?08bTzg==?=" <38438nx@qq.com>
To: "=?gb18030?B?V2lsaGVsbSBLdW1tZXI=?=" <sormester@lobbyist.com>
Subject: =?gb18030?B?u9i4tKO6ILvYuLSjulBhbGFjZSBXZXN0bWluc3Rl?=
=?gb18030?B?cjogMDEtMDctMjAxNCAtIDA0LTA3LTIwMTQ=?=
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_NextPart_539C743F_08A07490_0157E268"
Content-Transfer-Encoding: 8Bit
Date: Sun, 15 Jun 2014 00:11:43 +0800
X-Priority: 3
Message-ID: <tencent_573A737E73016B9F5A3D10C1@qq.com>
X-QQ-MIME: TCMime 1.0 by Tencent
X-Mailer: QQMail 2.x
X-QQ-Mailer: QQMail 2.x
X-QQ-ReplyHash: 170675637
X-QQ-SENDSIZE: 520
X-QQ-FName: 7B2EFFAD16B8462B84D3499A4CC7DDEF
X-QQ-LocalIP: 163.177.66.155
Envelope-To: <sormester@lobbyist.com>
X-GMX-Antispam: 0 (Mail was not recognized as spam); Detail=V3;
X-GMX-Antivirus: 0 (no virus found)
编辑:
我已尝试使用charset包但它没有效果。我仍然在相同的消息上得到相同的错误。
import "code.google.com/p/go-imap/go1/imap"
header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"])
r, err := charset.NewReader("UTF-8", bytes.NewReader(header))
if err != nil {
log.Fatal(err)
}
fmt.Printf("new char is %v", r)
msg, err := mail.ReadMessage(r)
if err != nil {
log.Fatal(err)
return mgs, err
}
mg.From, err = msg.Header.AddressList("From")
if err != nil {
log.Errorf("NO FROM msg %s, err %v", header, err)
return
}
邮件包似乎只能解码rfc2047
,但charset包不支持此
character set "rfc2047" not found
似乎mahonia可以解决这个问题吗?
答案 0 :(得分:0)
我希望这可以帮助那些可能会考虑Go来处理电子邮件(即开发客户端应用程序)的人。看起来标准的Go标准库还不够成熟,无法用于电子邮件处理。它不会处理多部分,不同的字符集等。经过近一天尝试不同的黑客和软件包之后,我决定抛弃go代码并使用旧的优秀JavaMail解决方案。
答案 1 :(得分:0)
Alexey Vasiliev的麻省理工学院许可http://github.com/le0pard/go-falcon/包含一个parser
软件包,该软件包适用于解码标头所需的编码软件包(该软件位于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")
}
}
}
看起来它的parser.FixEncodingAndCharsetOfPart
用于解码/转换内容,但是通过将[]byte
正文转换为{{1}来导致一些额外的分配}。如果您没有找到适合您的API,您至少可以使用该代码来了解它是如何完成的。
通过godoc.org发现的“......并且由3个包导入”链接来自encoding/simplifiedchinese - hooray godoc.org!
答案 2 :(得分:0)
我一直在使用github.com/jhillyerd/enmime,这似乎没有问题。它会解析标题和正文内容。给定self.toolbar = NavigationToolbar(self.canvas, self)
io.Reader
:
r