How to decode mail body in Go

huangapple go评论75阅读模式
英文:

How to decode mail body in Go

问题

我正在开发一个电子邮件客户端,其中一部分是需要解码电子邮件正文。我正在使用IMAP包来获取邮件,但是没有“decode”方法。我还检查了net/mail包,但也没有找到合适的方法。似乎只有头部有一个解析器。有没有可以使用的库?

英文:

I'm working on an email client and part of this I need to decode the email body. I'm using the IMAP package to fetch the messages but there is no "decode" method. I also checked the net/mail package with no luck either. Only the headers seem to have a parser. Is there any lib that I can use?

答案1

得分: 9

一旦你使用net/mail解析了电子邮件并获得了一个Message对象,如果正文是quoted-printable编码(Content-Transfer-Encoding: quoted-printable):

  • 如果使用的是Go 1.5版本,可以使用标准库中的quotedprintable包。
  • 如果使用的是较旧的Go版本,可以使用我的替代包。

示例代码:

r := quotedprintable.NewReader(msg.Body)
body, err := ioutil.ReadAll(r) // body现在包含解码后的正文

如果正文是使用base64编码(Content-Transfer-Encoding: base64),你应该使用encoding/base64包。

英文:

Once you parsed the email with net/mail and have a Message, if the body is quoted-printable encoded (Content-Transfer-Encoding: quoted-printable):

Example:

r := quotedprintable.NewReader(msg.Body)
body, err := ioutil.ReadAll(r) // body now contains the decoded body

If the body is encoded using base64 (Content-Transfer-Encoding: base64), you should use the encoding/base64 package.

答案2

得分: 5

我使用github.com/jhillyerd/enmime有一些运气,可以分离出邮件的头部和正文部分。给定一个io.Reader r

// 解析消息正文
env, _ := enmime.ReadEnvelope(r)
// 可以通过Envelope.GetHeader(name)获取头部信息。
fmt.Printf("发件人: %v\n", env.GetHeader("From"))
// 地址类型的头部可以解析为解码后的mail.Address结构列表。
alist, _ := env.AddressList("To")
for _, addr := range alist {
  fmt.Printf("收件人: %s <%s>\n", addr.Name, addr.Address)
}
fmt.Printf("主题: %v\n", env.GetHeader("Subject"))

// 纯文本正文可通过mime.Text获得。
fmt.Printf("纯文本正文: %v 个字符\n", len(env.Text))

// HTML正文存储在mime.HTML中。
fmt.Printf("HTML正文: %v 个字符\n", len(env.HTML))

// mime.Inlines是内嵌附件的切片。
fmt.Printf("内嵌附件: %v\n", len(env.Inlines))

// mime.Attachments包含非内嵌附件。
fmt.Printf("附件: %v\n", len(env.Attachments))
英文:

I've had some luck using github.com/jhillyerd/enmime to break out both headers and body parts. Given an io.Reader r:

// Parse message body
env, _ := enmime.ReadEnvelope(r)
// Headers can be retrieved via Envelope.GetHeader(name).
fmt.Printf(&quot;From: %v\n&quot;, env.GetHeader(&quot;From&quot;))
// Address-type headers can be parsed into a list of decoded mail.Address structs.
alist, _ := env.AddressList(&quot;To&quot;)
for _, addr := range alist {
  fmt.Printf(&quot;To: %s &lt;%s&gt;\n&quot;, addr.Name, addr.Address)
}
fmt.Printf(&quot;Subject: %v\n&quot;, env.GetHeader(&quot;Subject&quot;))

// The plain text body is available as mime.Text.
fmt.Printf(&quot;Text Body: %v chars\n&quot;, len(env.Text))

// The HTML body is stored in mime.HTML.
fmt.Printf(&quot;HTML Body: %v chars\n&quot;, len(env.HTML))

// mime.Inlines is a slice of inlined attacments.
fmt.Printf(&quot;Inlines: %v\n&quot;, len(env.Inlines))

// mime.Attachments contains the non-inline attachments.
fmt.Printf(&quot;Attachments: %v\n&quot;, len(env.Attachments))

答案3

得分: 1

你可以检查像artagnon/ibex这样的项目,它使用了go-imap,看看是否提供了这个功能。例如,可以查看它的artagnon/ibex/imap.go#L288-L301测试代码。

var body []byte
cmd, err = imap.Wait(c.UIDFetch(set, "BODY.PEEK[]"))
if err != nil {
    fmt.Println(err.Error())
    return nil
}
body = imap.AsBytes(cmd.Data[0].MessageInfo().Attrs["BODY[]"])
cmd.Data = nil

bytestring, err := json.Marshal(MessageDetail{string(body)})
if err != nil {
    fmt.Println(err.Error())
    return nil
}
return bytestring
英文:

You can check if a project like artagnon/ibex, which uses the go-imap package, does provide that feature.
See for instance its artagnon/ibex/imap.go#L288-L301 test.

var body []byte
cmd, err = imap.Wait(c.UIDFetch(set, &quot;BODY.PEEK[]&quot;))
if (err != nil) {
	fmt.Println(err.Error())
	return nil
}
body = imap.AsBytes(cmd.Data[0].MessageInfo().Attrs[&quot;BODY[]&quot;])
cmd.Data = nil

bytestring, err := json.Marshal(MessageDetail{string(body)})
if (err != nil) {
	fmt.Println(err.Error())
	return nil
}
return bytestring

huangapple
  • 本文由 发表于 2014年7月22日 17:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/24883742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定