带有多个附件的 SMTP 邮件 Mime + html
SMTP Mail Mime with multiple attachments + html
我正在尝试更多地研究原始电子邮件,为此我在 Go 中编写了一些代码,帮助我构建带有附件和 HTML 正文的电子邮件。
但我有一个问题,目前我只能发送一个附件(不适用于多个附件)或 HTML 正文。当我尝试发送 HTML + 1 个附件时,我收到一个内容为空且没有名称
的文件
所以,我不知道我的原始电子邮件有什么问题,如果有人能提供帮助,我将不胜感激。
下面是生成的原始电子邮件,如果有人可以帮助我了解它有什么问题的话。
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
To see this message, use an app with support for 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
A file containing some information
--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
A file containing some information
--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--
您可以尝试使用此 https://github.com/jordan-wright/email 生成具有相同内容和附件的 .eml 文件,然后将其与您的文件进行比较以找出差异。
如果不了解您的期望,就很难看出这里出了什么问题;但是每个附件都有 multipart/alternative
肯定是很可疑的。您基本上是在说“这里有两个等效部分;使用您喜欢的任何一个”,但是没有任何 headers(隐含 text/plain
)的那些似乎无法提供相同的内容或end-user 作为显式文本部分的经验。
明显而直接的问题是在最后两个 multipart/alternative
部分之前没有 MIME 边界。您需要在 headers 之前立即设置 MIME 边界,如下所示:
...
<h1>Your random text is: </h1><b>JU0NZU4ODAX</b>
--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary="hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY"
--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY
A file containing some information
...
但我猜你应该只删除最后两个结构中的 multipart/alternative
层,最后得到一个 top-level multipart/mixed
包含一个 multipart/alternative
(与text/plain
和 text/html
效果图,或者可能只是省略了无用的 text/plain
部分,这对每个人来说都是烦人的,而只是放一个 text/html
部分而不是 multipart/alternative
结构,如果确实这是您可以提供的全部),然后是 Content-Disposition: attachment
.
的两个 text/plain
部分
另外,不要在示例中使用您不知道它们属于谁的地址(更不用说您知道属于无辜第三方的地址)。
From: FooBar
To: some@example.com
Subject: Random Text
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY"
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary="WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY"
--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="UTF-8"
Content-Description: (if you don't have a description, don't add this header)
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
<h1>Your random text is: </h1><b>JU0NZU4ODAX</b>
--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
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=
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
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==
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY--
我正在尝试更多地研究原始电子邮件,为此我在 Go 中编写了一些代码,帮助我构建带有附件和 HTML 正文的电子邮件。
但我有一个问题,目前我只能发送一个附件(不适用于多个附件)或 HTML 正文。当我尝试发送 HTML + 1 个附件时,我收到一个内容为空且没有名称
的文件所以,我不知道我的原始电子邮件有什么问题,如果有人能提供帮助,我将不胜感激。
下面是生成的原始电子邮件,如果有人可以帮助我了解它有什么问题的话。
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
To see this message, use an app with support for 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
A file containing some information
--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
A file containing some information
--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--
您可以尝试使用此 https://github.com/jordan-wright/email 生成具有相同内容和附件的 .eml 文件,然后将其与您的文件进行比较以找出差异。
如果不了解您的期望,就很难看出这里出了什么问题;但是每个附件都有 multipart/alternative
肯定是很可疑的。您基本上是在说“这里有两个等效部分;使用您喜欢的任何一个”,但是没有任何 headers(隐含 text/plain
)的那些似乎无法提供相同的内容或end-user 作为显式文本部分的经验。
明显而直接的问题是在最后两个 multipart/alternative
部分之前没有 MIME 边界。您需要在 headers 之前立即设置 MIME 边界,如下所示:
...
<h1>Your random text is: </h1><b>JU0NZU4ODAX</b>
--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary="hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY"
--hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY
A file containing some information
...
但我猜你应该只删除最后两个结构中的 multipart/alternative
层,最后得到一个 top-level multipart/mixed
包含一个 multipart/alternative
(与text/plain
和 text/html
效果图,或者可能只是省略了无用的 text/plain
部分,这对每个人来说都是烦人的,而只是放一个 text/html
部分而不是 multipart/alternative
结构,如果确实这是您可以提供的全部),然后是 Content-Disposition: attachment
.
text/plain
部分
另外,不要在示例中使用您不知道它们属于谁的地址(更不用说您知道属于无辜第三方的地址)。
From: FooBar
To: some@example.com
Subject: Random Text
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY"
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
Content-Type: multipart/alternative; boundary="WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY"
--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="UTF-8"
Content-Description: (if you don't have a description, don't add this header)
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
<h1>Your random text is: </h1><b>JU0NZU4ODAX</b>
--WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
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=
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
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==
--VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY--