
我目前正在 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 将此正文发送给我自己时,我收到以下电子邮件:
附件也会发生类似的情况。如何正确解析此电子邮件以便我可以将其再次发送到另一个电子邮件地址?
如果您想在另一封邮件中重用邮件内容(例如重定向),仅包含正文是不够的,还需要包含邮件标头。
LimeSurvey是一款问卷调查管理系统,具有问卷的设计、修改、发布、回收和统计等多项功能,集成了调查程序开发、调查问卷的发布以及数据收集等功能,使用它,用户不必了解这些功能的编程细节。 LimeSurvey 3.14.2 中文版 更新日志:2018-08-07 -修正问题#13878:向用户组发送电子邮件-显示问题; -修正问题#13902:LimeSurvey尝试在编辑问题时更新响
154
具体来说,您至少需要包含原始 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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号