MIME headers and how we add an attachment in an e-mail

The idea of this article is very simple. I will try to explain how it should be done, to be able to attach “manually” an attachment to an email . To those who have red and have asked themselves which is the purpose of some action, I strongly recommend not to continue with the lecture, because this article is not for them.

The applications for adding an attachment are enough, I will mention only one: creating those “corporate” footers with pictures and others wonders.

So, lets assume that we want to create a different kind of footer, in HTML, with a nice picture which should be representative for the emails send through the server we want to configure.

Strictly for the disclaimer / footer part, altermime can be used without any problem. The problem is that, if we want also, to add  an image (which will appear in footer), things will get complicated.

The solution is to modify the messages body and to add our attachment the way we wanted. Easy to say, but what happens, when we run across “Content-Type: multipart/mixed; boundary=”_004_A8EC770875E1AE4A84851C05B6F730038D98AEFF4A_””…?

Well, we have just meet MIME. I won’t get into unnecessary details, but I will sum up the essential. We are interested in that “boundary=”.  All that is between quotation marks after the equal sign represents an separator (unique for a message) which breaks the various attachments/ parts of the message. So “_004_A8EC770875E1AE4A84851C05B6F730038D98AEFF4A_” is that boundary which we should look for in the body of the message, to be able to add our attachment.

A MIME content area starts with two signs “-” and is succeeded by that boundary, like so

“–_004_A8EC770875E1AE4A84851C05B6F730038D98AEFF4A_” (of course without “).

The area lasts until the meeting  point with another same marking line or until the meeting point with the final line, formed of boundary succeeded by to signs “-“, like so:  “_004_A8EC770875E1AE4A84851C05B6F730038D98AEFF4A_–”

That’s pretty much about the structure of an email, at least that’s what interests us.

If you wonder how to add attachments however, now comes the answer:

Our attachment can be added just before the final mark we talked about. We will have an example at the end. What you should know is that an attachment from an email is encoded base64. So, if we want to add an JPEG file, we must converted first in base64 (uuencode -m, for example) and after that to add it, using a sequence like:

Content-Type: image/jpeg; name=”sign.jpg”
Content-Description: sign.jpg
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=”sign.jpg”
Content-ID: <sign.jpg@00000000>

After all of the above, next is base64 encoding for the sign.jpg file.

All of this achieved, we have a message to which we added the attachment and it could be very nicely displayed form footers HTML .