Mark
Mark

Reputation: 153

MIME Multipart in email

I am trying to embed images inside an email.

The email is created by sending data via a socket to a SMTP server on port 25. Here's the MIME bit:

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=mixedsection;

--mixedsection
Content-Type: text/plain;
Content-Transfer-Encoding: 8bit
Content-Disposition: inline

Below should be an inline embedded image

--mixedsection
Content-Type: image/png; file=ts-charts.png
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=ts-charts.png

iVBORw0KGgoAAAANSUhEUgAAAnoAAAHqCAMAAACk+hPgAAABpFBMVEX// (etc etc etc)

--mixedsection--

When the email is received, it appears that the second section, the image, is converted into an attachment and never displayed inline. All of the other parts work fine, the image is correctly encoded as base64 so the attachment can be opened. The text/plain part works correctly both when the Content-Disposition is set to inline or attachment.

I've used other mail clients (outlook, thunderbird) to embed the image, and no problem with the image being displayed inline.

I can't even get the image to display inline even if I strip away everything else. The following is still received as an attachment:

MIME-Version: 1.0
Content-Type: image/png; file=ts-charts.png
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=ts-charts.png

iVBORw0KGgoAAAANSUhEUgAAAnoAAAHqCAMAAAC (etc etc)

So the question is, what am I missing here, and how do I get the image to display inline?

Upvotes: 2

Views: 2564

Answers (1)

Mark
Mark

Reputation: 153

For some reason I just couldn't get it to work in a vanilla, straight from the RFC standard way; if anyone knows of how to do it that way, please do post.

So, I gave up, and used inline HTML/CID to embed the image.

Here's how I did it:

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=mixedsection;

--mixedsection
Content-Type: multipart/related; boundary=relatedsection;

--relatedsection
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 8Bit
Content-Disposition: inline

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body>
    <p><img src="cid:myembeddedimage" alt=""></p>
  </body>
</html>

--relatedsection
Content-Type: image/png; name=ts-charts.png
Content-ID: myembeddedimage
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=ts-charts.png

iVBORw0KGgoAAAANSUhEUgAAAnoAAAHqCAMAAACk+hPgAAABpFBMVEX//3 (etc etc)

--relatedsection--
--mixedsection--

Note I also changed:

Content-Type: image/png; file=ts-charts.png

by removing the file property and replacing with a name property:

Content-Type: image/png; name=ts-charts.png

I'm not incredibly confident that this is either a good or recommended way of doing it, but it does work.

EDIT: .... Just to finish up. I couldn't for the life of me figure out how to mix plain text and HTML embedded images, so opted for simply putting the text into HTML.

Here's the final message:

MIME-Version: 1.0
Content-Type: multipart/related; boundary=relatedsection;

--relatedsection
Content-Type: text/html;
Content-Transfer-Encoding: 8bit
Content-Disposition: inline

<html>
   <head>
      <meta http-equiv="content-type" content="text/html;">
   </head>
   <body>
      <p> Below is an embedded image ...</p>
      <p><img src="cid:myembeddedimage" alt=""></p>
   </body>
</html>

--relatedsection
Content-Type: image/png; name=ts-charts.png
Content-ID: myembeddedimage
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=ts-charts.png
 
AHqCAMAAACk+hPgAAABpFBMVEX///8PDw8jIyPDw8OPj333mOm2ajzGtEbdPpm (etc (etc)
--relatedsection--

I had to remove the outer multipart/mixed since running this message through an exchange server resulted in attachments (att0001, att0002) being appended to the message. No problem with these ghost attachments at all on thunderbird, gmail, or whatever the client is called on i-Phones.

Upvotes: 1

Related Questions