英文:
Parse email fields with Golang
问题
我正在尝试在Go中解析我的电子邮件,我需要帮助。
如何访问邮件的Content-type字段?
cmd, _ = c.Fetch(set, "BODY[HEADER]", "BODY[1]")
for cmd.InProgress() {
for _, rsp = range cmd.Data {
header := imap.AsBytes(rsp.MessageInfo().Attrs["BODY[HEADER]"])
body := imap.AsString(rsp.MessageInfo().Attrs["BODY[1]"])
if msg, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil {
通过这样做,我可以访问正文和标题,但是当电子邮件包含附件时,使用BODY[1]
会获取到所有的元数据,而不仅仅是纯文本。为了避免这种情况,我可以使用BODY[1.1]
,但是我需要根据Content-Type:[multipart/alternative]
进行条件判断,而我无法访问到该字段。
英文:
I am trying to parse my emails in Go and I need help.
How to get access to Content-type field of mail?
cmd, _ = c.Fetch(set, "BODY[HEADER]", "BODY[1]")
for cmd.InProgress() {
for _, rsp = range cmd.Data {
header := imap.AsBytes(rsp.MessageInfo().Attrs["BODY[HEADER]"])
body := imap.AsString(rsp.MessageInfo().Attrs["BODY[1]"])
if msg, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil {
with this I can gain access to Body and Header, but when email contain included file then with BODY[1]
I have all meta-data, not only pure text. To avoid that I can use BODY[1.1]
, but I need condition by Content-Type:[multipart/alternative]
and I can't gain access ot that field.
答案1
得分: 0
好的,以下是翻译好的内容:
好的,所以我自己找到了答案。但是无论如何,也许其他人对此感兴趣。你可以通过以下方式访问邮件的各个字段:
msg.Header.Get("Content-type")
而且你可以用任何标头部分的名称替换Content-type
。
fmt.println(msg)
可以知道它具有哪些字段名称。
英文:
Ok, so i figured it out by my self. But anyway, maybe someone else interested in that. You can have access to varios fields of mail by
msg.Header.Get("Content-type")
and instead of Content-type
you can put any header part name.
fmt.println(msg)
to know what name fields it have
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论