首页 > 后端开发 > Golang > 正文

使用 net/mail 解析电子邮件

WBOY
发布: 2024-02-06 09:40:07
转载
982人浏览过

使用 net/mail 解析电子邮件

问题内容

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

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))
}
登录后复制

这对于纯文本电子邮件来说效果很好。但是,当我使用包含 html 的 apple mail 应用程序发送电子邮件时,返回了以下正文:

--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--
登录后复制

当使用 sendgrid 将此正文发送给我自己时,我收到以下电子邮件:

附件也会发生类似的情况。如何正确解析此电子邮件以便我可以将其再次发送到另一个电子邮件地址?


正确答案


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

 3.14.2 中文版LimeSurvey
3.14.2 中文版LimeSurvey

LimeSurvey是一款问卷调查管理系统,具有问卷的设计、修改、发布、回收和统计等多项功能,集成了调查程序开发、调查问卷的发布以及数据收集等功能,使用它,用户不必了解这些功能的编程细节。 LimeSurvey 3.14.2 中文版 更新日志:2018-08-07 -修正问题#13878:向用户组发送电子邮件-显示问题; -修正问题#13902:LimeSurvey尝试在编辑问题时更新响

 3.14.2 中文版LimeSurvey 154
查看详情  3.14.2 中文版LimeSurvey

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

对于非多部分正文,您还需要包含原始 Content-Transfer-Encoding 标头,该标头确定正文如何编码以进行传输,即 base64、quoted-printable、7bit,...

以上就是使用 net/mail 解析电子邮件的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号