SMTP邮件MIME带有多个附件+HTML

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

SMTP Mail Mime with multiple attachments + html

问题

我正在尝试学习有关原始电子邮件的更多知识,为此我用Go编写了一些代码,帮助我构建带有附件和HTML正文的电子邮件。

但是我遇到了一个问题,目前我只能发送一个附件(无法处理多个附件)或者一个HTML正文。当我尝试发送一个包含HTML和一个附件的邮件时,我收到一个内容为空且没有名称的文件。

所以,我不知道我的原始邮件有什么问题,如果有人能帮助我,我会非常感激。

以下是生成的原始邮件,如果有人能帮助我理解问题所在,我将不胜感激。

From: FooBar
To: some@mail.com
Subject: Random Text
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY"

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary="WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY"

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY
要查看此消息,请使用支持HTML的应用程序。

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY
Content-Type: text/html; charset="UTF-8"
Content-Description: 
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

<h1>Your random text is: </h1><b>JU0NZU4ODAX</b>

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--

Content-Type: multipart/alternative; boundary="hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY"

--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY
包含一些信息的文件

--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY
Content-Type: text/plain; charset="utf-8"
Content-Description: random-text.txt
Content-Disposition: attachment; filename="random-text.txt";
Content-Transfer-Encoding: base64
Content-ID: <random-text.txt>

SlUwTlpVNE9EQVg=

--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY--

Content-Type: multipart/alternative; boundary="0PZiT5S5IpaWM5bEOA0Uw5W0000TDo5X0y50u00Ln50dIa7r0yS50B5O5UZNDARY"

--0PZiT5S5IpaWM5bEOA0Uw5W0000TDo5X0y50u00Ln50dIa7r0yS50B5O5UZNDARY
包含一些信息的文件

--0PZiT5S5IpaWM5bEOA0Uw5W0000TDo5X0y50u00Ln50dIa7r0yS50B5O5UZNDARY
Content-Type: text/plain; charset="utf-8"
Content-Description: no-matter.txt
Content-Disposition: attachment; filename="no-matter.txt";
Content-Transfer-Encoding: base64
Content-ID: <no-matter.txt>

w4kgQklSTCBNRVNNTw==

--0PZiT5S5IpaWM5bEOA0Uw5W0000TDo5X0y50u00Ln50dIa7r0yS50B5O5UZNDARY--

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY--
英文:

I'm trying to study a little more about raw emails and for that I wrote some code in Go that helps me building an Email with attachments and HTML body.

But I have a problem, currently I can only send either one attachment (don't work with multiple attachments) or a HTML body. And when I try to send a HTML + 1 attachment, I receive a file with empty content and noname

So, I don't know whats the problem with my raw email, if anyone can help I really appreciate it.

Below is the generated Raw email if anyone can help me understanding what's the problem with it.

From: FooBar
To: some@mail.com
Subject: Random Text
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=&quot;VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY&quot;

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary=&quot;WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY&quot;

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY
To see this message, use an app with support for HTML.

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY
Content-Type: text/html; charset=&quot;UTF-8&quot;
Content-Description: 
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

&lt;h1&gt;Your random text is: &lt;/h1&gt;&lt;b&gt;JU0NZU4ODAX&lt;/b&gt;

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--

Content-Type: multipart/alternative; boundary=&quot;hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY&quot;

--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY
A file containing some information

--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY
Content-Type: text/plain; charset=&quot;utf-8&quot;
Content-Description: random-text.txt
Content-Disposition: attachment; filename=&quot;random-text.txt&quot;;
Content-Transfer-Encoding: base64
Content-ID: &lt;random-text.txt&gt;

SlUwTlpVNE9EQVg=

--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY--

Content-Type: multipart/alternative; boundary=&quot;0PZiT5S5IpaWM5bEOA0Uw5W0000TDo5X0y50u00Ln50dIa7r0yS50B5O5UZNDARY&quot;

--0PZiT5S5IpaWM5bEOA0Uw5W0000TDo5X0y50u00Ln50dIa7r0yS50B5O5UZNDARY
A file containing some information

--0PZiT5S5IpaWM5bEOA0Uw5W0000TDo5X0y50u00Ln50dIa7r0yS50B5O5UZNDARY
Content-Type: text/plain; charset=&quot;utf-8&quot;
Content-Description: no-matter.txt
Content-Disposition: attachment; filename=&quot;no-matter.txt&quot;;
Content-Transfer-Encoding: base64
Content-ID: &lt;no-matter.txt&gt;

w4kgQklSTCBNRVNNTw==

--0PZiT5S5IpaWM5bEOA0Uw5W0000TDo5X0y50u00Ln50dIa7r0yS50B5O5UZNDARY--

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY--

答案1

得分: 1

在没有了解你的期望之前,很难看出这里有什么问题;但是每个附件都有multipart/alternative确实是相当可疑的。你基本上是在说“这里有两个等效的部分;使用你喜欢的那个”,但是没有任何头部的部分(隐含为text/plain)似乎无法提供与显式文本部分相同的内容或最终用户体验。

明显和直接的问题是,在最后两个multipart/alternative部分之前,你没有MIME边界。你需要在头部之前立即放置MIME边界,就像这样:

...
&lt;h1&gt;Your random text is: &lt;/h1&gt;&lt;b&gt;JU0NZU4ODAX&lt;/b&gt;

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary=&quot;hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY&quot;

--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY

A file containing some information
...

但我猜你应该只删除最后两个结构中的multipart/alternative层,并最终得到一个顶级的multipart/mixed,其中包含一个multipart/alternative(带有text/plaintext/html的渲染,或者只是省略无用的text/plain部分,因为它只会让每个人都感到烦恼,而是用text/html部分替代multipart/alternative结构,如果确实是你能提供的全部内容) ,然后是两个带有Content-Disposition: attachmenttext/plain部分。

另外,不要在示例中使用你不知道属于谁的地址(更不用说你知道属于无辜第三方的地址了)。

From: FooBar
To: some@example.com
Subject: Random Text
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=&quot;VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY&quot;

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary=&quot;WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY&quot;

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY

我们认为在HTML查看器中显示一个令人讨厌的建议会很有帮助,而不是提供只在HTML部分可见的信息。嘿嘿。

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY
Content-Type: text/html; charset=&quot;UTF-8&quot;
Content-Description: (如果你没有描述,请不要添加此头部)
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

&lt;h1&gt;Your random text is: &lt;/h1&gt;&lt;b&gt;JU0NZU4ODAX&lt;/b&gt;

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: text/plain; charset=&quot;utf-8&quot;
Content-Description: random-text.txt
Content-Disposition: attachment; filename=&quot;random-text.txt&quot;;
Content-Transfer-Encoding: base64
Content-ID: &lt;random-text.txt&gt;

SlUwTlpVNE9EQVg=

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: text/plain; charset=&quot;utf-8&quot;
Content-Description: no-matter.txt
Content-Disposition: attachment; filename=&quot;no-matter.txt&quot;;
Content-Transfer-Encoding: base64
Content-ID: &lt;no-matter.txt&gt;

w4kgQklSTCBNRVNNTw==

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY--
英文:

Without knowledge of what you expect, it's hard to see what's wrong here; but having multipart/alternative for each attachment is certainly quite dubious. You are basically saying "here are two equivalent parts; use whichever one you like" but the ones without any headers (which are implicitly text/plain) don't seem to be able to provide the same contents or end-user experience as the explicit text parts.

The obvious and immediate problem is that you have no MIME boundary before the two last multipart/alternative parts. You need to have the MIME boundary immediately before the headers, like this:

...
&lt;h1&gt;Your random text is: &lt;/h1&gt;&lt;b&gt;JU0NZU4ODAX&lt;/b&gt;

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary=&quot;hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY&quot;

--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY

A file containing some information
...

But I'm guessing you should just remove the multipart/alternative layers in the last two structures and end up with a top-level multipart/mixed containing one multipart/alternative (with text/plain and text/html renderings, or maybe just omit the useless text/plain part which is just annoying to everyone anyway, and just put a text/html part instead of the multipart/alternative structure if indeed that is all you can usefully provide) followed by two text/plain parts with Content-Disposition: attachment.

As an aside, don't use addresses you don't know who they belong to (let alone then addresses you know belong to innocent third parties) in examples.

From: FooBar
To: some@example.com
Subject: Random Text
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=&quot;VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY&quot;

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary=&quot;WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY&quot;

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY

We thought it would be helpful to display an annoying suggestion
to look at this in an HTML viewer instead of provide you with
the information which is only visible in the HTML part.
Nyah, nyah.

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY
Content-Type: text/html; charset=&quot;UTF-8&quot;
Content-Description: (if you don&#39;t have a description, don&#39;t add this header)
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

&lt;h1&gt;Your random text is: &lt;/h1&gt;&lt;b&gt;JU0NZU4ODAX&lt;/b&gt;

--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: text/plain; charset=&quot;utf-8&quot;
Content-Description: random-text.txt
Content-Disposition: attachment; filename=&quot;random-text.txt&quot;;
Content-Transfer-Encoding: base64
Content-ID: &lt;random-text.txt&gt;

SlUwTlpVNE9EQVg=

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: text/plain; charset=&quot;utf-8&quot;
Content-Description: no-matter.txt
Content-Disposition: attachment; filename=&quot;no-matter.txt&quot;;
Content-Transfer-Encoding: base64
Content-ID: &lt;no-matter.txt&gt;

w4kgQklSTCBNRVNNTw==

--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY--

答案2

得分: 0

你可以尝试使用这个链接https://github.com/jordan-wright/email来生成一个具有相同内容和附件的.eml文件,然后将其与你的文件进行比较,找出它们之间的差异。

英文:

You may try using this https://github.com/jordan-wright/email to generate a .eml file with same content and attachments then compare it with your one to find out the differences.

huangapple
  • 本文由 发表于 2021年9月9日 14:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/69113120.html
匿名

发表评论

匿名网友

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

确定