使用net/mail解析电子邮件

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

Parsing emails using net/mail

问题

我目前正在使用net/mail在golang中解析一封电子邮件。

import (
  "net/mail"
  "io"
  "strings"
)

func main() {
  email := "some email received"

  reader := strings.NewReader(emailInput)
  msg, err := mail.ReadMessage(inputReader)
  check(err)

  body, err := io.ReadAll(msg.Body)
  check(err)

  fmt.Println(string(body))
}

这段代码可以很好地处理纯文本邮件。但是,当我使用Apple Mail应用程序发送包含HTML的电子邮件时,返回的正文如下所示:

--Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
    charset=3Dus-ascii

fn main() {
  println!("Hello world!");
}


--Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
Content-Transfer-Encoding: 7bit
Content-Type: text/html;
    charset=3Dus-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; charset=
=3Dus-ascii"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode=
: space; line-break: after-white-space;" class=3D""><pre class=3D"" style=
="color: rgb(209, 209, 209); background-color: rgb(0, 0, 0);">fn <span cl=
ass="" style="color: rgb(230, 97, 112); font-weight: bold;">main</span>=
<span class="" style="color: rgb(210, 205, 134);">(</span><span class=
="" style="color: rgb(210, 205, 134);">)</span> <span class="" style=
="color: rgb(176, 96, 176);">{</span>
  println<span class="" style="color: rgb(210, 205, 134);">!</span><spa=
n class="" style="color: rgb(210, 205, 134);">(</span><span class="" =
style="color: rgb(2, 208, 69);">""</span><span class="" style="color: =
rgb(0, 196, 196);">Hello world!</span><span class="" style="color: rgb(=
2, 208, 69);">""</span><span class="" style="color: rgb(210, 205, 134);"=
>)</span><span class="" style="color: rgb(176, 96, 176);">;</span>
<span class="" style="color: rgb(176, 96, 176);">}</span></pre><div cla=
ss=""><br class=""></div><img src="https://u26515437.ct.sendgrid.net/=
wf/open?upn=3DDIOvMy23aag1zrlqvNJSXvalij334tYGiXBPjhGDZmVFp8I6wml2yWuZJN5Gy=
bSje8vz4sPJIshSAHwJ3q0VXXT-2Bc6PQlllUxVtR29EWnCSN5hiChQAIjXAqR6Wybp-2BX4xjr=
0G6ey9dIx77zxVAowA1r-2FRITFD4Og2jn-2FC3wCWfBUGLplPfTxygFPM8q8w0tCivLExebLwa=
m7q-2Flq-2B4-2FZM1Ekzac-2BOWr4XOH8pFo9-2B4-3D" alt="" width="1" height=
="1" border="0" style="height:1px !important;width:1px !important;bor=
der-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;m=
argin-right:0 !important;margin-left:0 !important;padding-top:0 !important;=
padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !impo=
rtant;"/></body></html>
--Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743--

当我使用SendGrid将这个正文发送给自己时,我收到了以下电子邮件:
使用net/mail解析电子邮件

类似的情况也会发生在附件上。我应该如何正确解析这封电子邮件,以便可以将其发送给另一个电子邮件地址?

英文:

I'm currently parsing an email using net/mail in golang.

import (
  "net/mail"
  "io"
  "strings"
)

func main() {
  email := "some email received"

  reader := strings.NewReader(emailInput)
  msg, err := mail.ReadMessage(inputReader)
  check(err)

  body, err := io.ReadAll(msg.Body)
  check(err)

  fmt.Println(string(body))
}

This works fine for plain text emails. But when I send an email using the Apple Mail app containing html, the following body was returned:

--Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=3Dus-ascii

fn main() {
  println!("Hello world!");
}


--Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
Content-Transfer-Encoding: 7bit
Content-Type: text/html;
	charset=3Dus-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; charset=
=3Dus-ascii"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode=
: space; line-break: after-white-space;" class=3D""><pre class=3D"" style=
=3D"color: rgb(209, 209, 209); background-color: rgb(0, 0, 0);">fn <span cl=
ass=3D"" style=3D"color: rgb(230, 97, 112); font-weight: bold;">main</span>=
<span class=3D"" style=3D"color: rgb(210, 205, 134);">(</span><span class=
=3D"" style=3D"color: rgb(210, 205, 134);">)</span> <span class=3D"" style=
=3D"color: rgb(176, 96, 176);">{</span>
  println<span class=3D"" style=3D"color: rgb(210, 205, 134);">!</span><spa=
n class=3D"" style=3D"color: rgb(210, 205, 134);">(</span><span class=3D"" =
style=3D"color: rgb(2, 208, 69);">"</span><span class=3D"" style=3D"color: =
rgb(0, 196, 196);">Hello world!</span><span class=3D"" style=3D"color: rgb(=
2, 208, 69);">"</span><span class=3D"" style=3D"color: rgb(210, 205, 134);"=
>)</span><span class=3D"" style=3D"color: rgb(176, 96, 176);">;</span>
<span class=3D"" style=3D"color: rgb(176, 96, 176);">}</span></pre><div cla=
ss=3D""><br class=3D""></div><img src=3D"https://u26515437.ct.sendgrid.net/=
wf/open?upn=3DDIOvMy23aag1zrlqvNJSXvalij334tYGiXBPjhGDZmVFp8I6wml2yWuZJN5Gy=
bSje8vz4sPJIshSAHwJ3q0VXXT-2Bc6PQlllUxVtR29EWnCSN5hiChQAIjXAqR6Wybp-2BX4xjr=
0G6ey9dIx77zxVAowA1r-2FRITFD4Og2jn-2FC3wCWfBUGLplPfTxygFPM8q8w0tCivLExebLwa=
m7q-2Flq-2B4-2FZM1Ekzac-2BOWr4XOH8pFo9-2B4-3D" alt=3D"" width=3D"1" height=
=3D"1" border=3D"0" style=3D"height:1px !important;width:1px !important;bor=
der-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;m=
argin-right:0 !important;margin-left:0 !important;padding-top:0 !important;=
padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !impo=
rtant;"/></body></html>
--Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743--

And when sending this body to myself using SendGrid, I get the following email:
使用net/mail解析电子邮件

Something similar will happen for attachements. How do I properly parse this email so I can send it again to another email-address?

答案1

得分: 1

如果您想在另一封邮件中重用消息内容(例如使用重定向),仅包含正文是不够的,您还需要包含邮件头部。

具体来说,您至少需要包含原始的Content-Type头部,该头部显示了如何解释正文。在您的情况下,它包含一些multipart/*的内容类型(例如multipart/mixed、multipart/related、multipart/alternative)和分隔邮件正文中各个部分的边界。如果这不是一个多部分正文,则Content-Type包含字符集,该字符集确定所使用的文本编码,例如utf-8、iso-8859-15等。

对于非多部分正文,您还需要包含原始的Content-Transfer-Encoding头部,该头部确定正文在传输过程中的编码方式,例如base64、quoted-printable、7bit等。

英文:

If you want to reuse a message content in another mail (like with a redirect) it is not enough to include the body but you also need to include the mail header too.

Specifically you need to include at least the original Content-Type header, which shows how the body should be interpreted. In your case it contains some multipart/* content-type (i.e. something like multipart/mixed, multipart/related, multipart/alternative) and the boundary which separates the parts in the mail body. If this is not a multipart body the Content-Type contains the charset, which determines the text encoding used, i.e. utf-8, iso-8859-15, ....

For a non-multipart body you also need to include the original Content-Transfer-Encoding header which determines how the body is encoded for transfer, i.e. base64, quoted-printable, 7bit, ...

huangapple
  • 本文由 发表于 2023年1月15日 16:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75123796.html
匿名

发表评论

匿名网友

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

确定