Monday, November 9, 2009

How Attachments are Included in Email Messages

·

In the past I discussed how email messages are created by an email client and how the mail server receives and accepts messages. In this post I wanted to provide you with some information on how an attachment is included with email messages.

The standard for attaching files or including different content types in your email message is defined using MIME version 1.0. MIME is a very simple method for separating message body content into parts so it can later be viewed by an email client.

Discussion:

  1. MIME Messages
  2. MIME boundaries
  3. Types of content - HTML, Text, Images, Files
  4. MIME end boundary
MIME Messages

Most email clients use MIME when creating email messages, however, there are older technologies that may still be used today, for example, uuencoding was big back in the day for attaching files and for Newsgroups. Basically it consisted of tags (begin / end) that told the reader where the beginning and the ending parts of the attachment were. This worked great but didn't provide the flexibility needed to decode modern email messages.

The method used by MIME to divide the message body into parts is fairly simple and knowing how to read MIME may help you in troubleshooting email issues.

Most email clients will not allow you to view the MIME boundaries or MIME content of an email message so keep in mind that you will need to read the original message file saved by your mail server to see MIME content/boundaries.

MIME Boundaries

MIME Boundaries are tags defined by the MIME Boundary header. The email client reads the boundary header and looks in the message body for the boundary to determine the start of an attachment or special message body content, such as HTML.

Here's an example of a message showing MIME boundaries, this is standard header configuration and should appear on all MIME messages (other than the boundary tags which are always unique).

---------------------------------------------
Received: from remote_domain.com (127.0.0.1 [127.0.0.1])
by danj (IceWarp Server v9.4.3) with ESMTP id QZS43813
for
danj@domain.com; Mon, 09 Nov 2009 09:35:13 -0500
Date: Mon, 9 Nov 2009 09:35:13 -0500
Return-Path: danj@remote_domain.com
To: Dan
From: Fred
Subject: Test message
Message-ID: 6ae9475a5a749119298f7d5a1ec823b4@remote_domain.com
X-Mailer: IceWarp Mailer 9.4.3 (2009-06-22)
X-Priority: 3
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="b1_6ae9475a5a749119298f7d5a1ec823b4"

--b1_6ae9475a5a749119298f7d5a1ec823b4
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is a test message

--b1_6ae9475a5a749119298f7d5a1ec823b4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<b>This is a test message</b>

--b1_6ae9475a5a749119298f7d5a1ec823b4--
---------------------------------------------


References to MIME boundary locations

Content-Type Header

The Content-Type header defines the type of message and the boundary used to define different parts of the body of the message.

The boundary in this message example is:

b1_6ae9475a5a749119298f7d5a1ec823b4

The first starting boundary is defined using '--' followed by the boundary, example,

--b1_6ae9475a5a749119298f7d5a1ec823b4

Each time a new part is defined the boundary will separate that section and define the type of content. In this example, the content type is plain/text and HTML. The first starting boundary is plain text and the second is HTML

The type of content defined between the MIME boundaries is what tells the email client how to read the content. If you attach an image or document the content type will change for that type of content. Here's an example, using an image attachment

--b1_6ae9475a5a749119298f7d5a1ec823b4
Content-Type: image/jpeg; name="mime-example.JPG"; charset="UTF-8"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="mime-example.JPG"

wAARCAH5Ai8DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS
0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVW

--b1_6ae9475a5a749119298f7d5a1ec823b4--


Notice the Content-Type: is now 'image/jpeg' and the file name is 'mime-example.JPG'.

End Boundary

The ending boundary is the same as the starting boundary with the addition of '--' at the end of the boundary, example:

--b1_6ae9475a5a749119298f7d5a1ec823b4--

The ending boundary may appear more than once in the message, usually defining the ending of attachments other than Plain Text and HTML boundaries.

For more information on MIME please refer to the following links and I hope this was helpful.

Wiki

MIME RFC

Archive

Tweets