Search code examples
emailgosmtpmimemime-message

SMTP Mail Mime with multiple attachments + html


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: [email protected]
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--

Solution

  • 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:

    ...
    <h1>Your random text is: </h1><b>JU0NZU4ODAX</b>
    
    --WMaSWQ0Q205bd0Ly000W5p00N5ohAMoUpXu050nC0d0a0r0y050B0O0UkN0D0ARY--
    --VH5S0k0C2sKbOiiC0CQW0H0gdwo0A00wLHuY0Knk0dzaTr5y55VB0O5UqN0D5ARY
    Content-Type: multipart/alternative; boundary="hcx0dnS50VSCeMb5mSyM05W00yQT0odL7050u0XMn5UdUa0rdy050B0OgUjNDARY"
    
    --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: [email protected]
    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--