使用net/mail解析电子邮件

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

Parsing emails using net/mail

问题

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

  1. import (
  2. "net/mail"
  3. "io"
  4. "strings"
  5. )
  6. func main() {
  7. email := "some email received"
  8. reader := strings.NewReader(emailInput)
  9. msg, err := mail.ReadMessage(inputReader)
  10. check(err)
  11. body, err := io.ReadAll(msg.Body)
  12. check(err)
  13. fmt.Println(string(body))
  14. }

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

  1. --Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
  2. Content-Transfer-Encoding: 7bit
  3. Content-Type: text/plain;
  4. charset=3Dus-ascii
  5. fn main() {
  6. println!("Hello world!");
  7. }
  8. --Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
  9. Content-Transfer-Encoding: 7bit
  10. Content-Type: text/html;
  11. charset=3Dus-ascii
  12. <html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; charset=
  13. =3Dus-ascii"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode=
  14. : space; line-break: after-white-space;" class=3D""><pre class=3D"" style=
  15. ="color: rgb(209, 209, 209); background-color: rgb(0, 0, 0);">fn <span cl=
  16. ass="" style="color: rgb(230, 97, 112); font-weight: bold;">main</span>=
  17. <span class="" style="color: rgb(210, 205, 134);">(</span><span class=
  18. ="" style="color: rgb(210, 205, 134);">)</span> <span class="" style=
  19. ="color: rgb(176, 96, 176);">{</span>
  20. println<span class="" style="color: rgb(210, 205, 134);">!</span><spa=
  21. n class="" style="color: rgb(210, 205, 134);">(</span><span class="" =
  22. style="color: rgb(2, 208, 69);">""</span><span class="" style="color: =
  23. rgb(0, 196, 196);">Hello world!</span><span class="" style="color: rgb(=
  24. 2, 208, 69);">""</span><span class="" style="color: rgb(210, 205, 134);"=
  25. >)</span><span class="" style="color: rgb(176, 96, 176);">;</span>
  26. <span class="" style="color: rgb(176, 96, 176);">}</span></pre><div cla=
  27. ss=""><br class=""></div><img src="https://u26515437.ct.sendgrid.net/=
  28. wf/open?upn=3DDIOvMy23aag1zrlqvNJSXvalij334tYGiXBPjhGDZmVFp8I6wml2yWuZJN5Gy=
  29. bSje8vz4sPJIshSAHwJ3q0VXXT-2Bc6PQlllUxVtR29EWnCSN5hiChQAIjXAqR6Wybp-2BX4xjr=
  30. 0G6ey9dIx77zxVAowA1r-2FRITFD4Og2jn-2FC3wCWfBUGLplPfTxygFPM8q8w0tCivLExebLwa=
  31. m7q-2Flq-2B4-2FZM1Ekzac-2BOWr4XOH8pFo9-2B4-3D" alt="" width="1" height=
  32. ="1" border="0" style="height:1px !important;width:1px !important;bor=
  33. der-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;m=
  34. argin-right:0 !important;margin-left:0 !important;padding-top:0 !important;=
  35. padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !impo=
  36. rtant;"/></body></html>
  37. --Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743--

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

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

英文:

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

  1. import (
  2. "net/mail"
  3. "io"
  4. "strings"
  5. )
  6. func main() {
  7. email := "some email received"
  8. reader := strings.NewReader(emailInput)
  9. msg, err := mail.ReadMessage(inputReader)
  10. check(err)
  11. body, err := io.ReadAll(msg.Body)
  12. check(err)
  13. fmt.Println(string(body))
  14. }

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:

  1. --Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
  2. Content-Transfer-Encoding: 7bit
  3. Content-Type: text/plain;
  4. charset=3Dus-ascii
  5. fn main() {
  6. println!("Hello world!");
  7. }
  8. --Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
  9. Content-Transfer-Encoding: 7bit
  10. Content-Type: text/html;
  11. charset=3Dus-ascii
  12. <html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; charset=
  13. =3Dus-ascii"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode=
  14. : space; line-break: after-white-space;" class=3D""><pre class=3D"" style=
  15. =3D"color: rgb(209, 209, 209); background-color: rgb(0, 0, 0);">fn <span cl=
  16. ass=3D"" style=3D"color: rgb(230, 97, 112); font-weight: bold;">main</span>=
  17. <span class=3D"" style=3D"color: rgb(210, 205, 134);">(</span><span class=
  18. =3D"" style=3D"color: rgb(210, 205, 134);">)</span> <span class=3D"" style=
  19. =3D"color: rgb(176, 96, 176);">{</span>
  20. println<span class=3D"" style=3D"color: rgb(210, 205, 134);">!</span><spa=
  21. n class=3D"" style=3D"color: rgb(210, 205, 134);">(</span><span class=3D"" =
  22. style=3D"color: rgb(2, 208, 69);">"</span><span class=3D"" style=3D"color: =
  23. rgb(0, 196, 196);">Hello world!</span><span class=3D"" style=3D"color: rgb(=
  24. 2, 208, 69);">"</span><span class=3D"" style=3D"color: rgb(210, 205, 134);"=
  25. >)</span><span class=3D"" style=3D"color: rgb(176, 96, 176);">;</span>
  26. <span class=3D"" style=3D"color: rgb(176, 96, 176);">}</span></pre><div cla=
  27. ss=3D""><br class=3D""></div><img src=3D"https://u26515437.ct.sendgrid.net/=
  28. wf/open?upn=3DDIOvMy23aag1zrlqvNJSXvalij334tYGiXBPjhGDZmVFp8I6wml2yWuZJN5Gy=
  29. bSje8vz4sPJIshSAHwJ3q0VXXT-2Bc6PQlllUxVtR29EWnCSN5hiChQAIjXAqR6Wybp-2BX4xjr=
  30. 0G6ey9dIx77zxVAowA1r-2FRITFD4Og2jn-2FC3wCWfBUGLplPfTxygFPM8q8w0tCivLExebLwa=
  31. m7q-2Flq-2B4-2FZM1Ekzac-2BOWr4XOH8pFo9-2B4-3D" alt=3D"" width=3D"1" height=
  32. =3D"1" border=3D"0" style=3D"height:1px !important;width:1px !important;bor=
  33. der-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;m=
  34. argin-right:0 !important;margin-left:0 !important;padding-top:0 !important;=
  35. padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !impo=
  36. rtant;"/></body></html>
  37. --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:

确定