MIME (Multipurpose Internet Mail Extensions) is the set of rules that lets an email carry more than plain ASCII text: HTML, non-English characters, images and attachments. Its multipart types are containers. multipart/alternative holds different versions of the same content, such as plain text and HTML, so the client shows the best one it supports. multipart/mixed holds independent parts, such as the body and one or more attachments. multipart/related groups a main document with the resources it uses, such as an HTML body and the images it embeds with cid: links.
Most real messages nest these containers: mixed on the outside for attachments, alternative inside for text and HTML and related around the HTML for inline images. Getting the nesting right is what separates a message that renders correctly everywhere from one that shows attachments as clutter or loses its inline images.
MIME basics in one minute
RFC 2045 to RFC 2049 define MIME. A message declares itself as MIME with a MIME-Version: 1.0 header and describes its content with Content-Type. Each part has its own headers and body. In a multipart message, the Content-Type includes a boundary parameter, a string that separates the parts. The boundary appears in the body as a line starting with two hyphens, and the final boundary ends with two more hyphens. The boundary must not occur anywhere inside the parts, so generators use long random strings.
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="b1_8f3a2c"
--b1_8f3a2c
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Hello,
This is the plain text version.
--b1_8f3a2c
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<p>Hello,</p><p>This is the <b>HTML</b> version.</p>
--b1_8f3a2c--multipart/alternative
Use alternative to offer the same message in different formats. The standard says the parts should be ordered with the simplest first and the richest last, and a client displays the last format it can handle. That is why text/plain comes before text/html. A plain text part matters: it serves clients that cannot show HTML, supports accessibility and, in practice, mail with a missing plain text part is treated with more suspicion by some filters. The two versions should say the same thing. See plain text email.
multipart/mixed
Use mixed when the parts are separate items that should all be presented, in order. The first part is normally the body, and following parts are attachments with Content-Disposition: attachment; filename="report.pdf". When there is no attachment, mixed is not needed. If the body itself has several formats, the first part of the mixed container is a nested alternative.
multipart/related
Use related when an HTML part depends on other parts to display, most commonly images referenced from the HTML with a cid: URL. Each image part carries a Content-ID header, and the HTML refers to it. RFC 2387 defines the type. The first part is the root document unless a start parameter says otherwise.
<img src="cid:[email protected]" alt="Company logo" width="120">
--rel_77de
Content-Type: image/png; name="logo.png"
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>
Content-Disposition: inline; filename="logo.png"
iVBORw0KGgoAAAANSUhEUgAA...Inline images embedded this way travel with the message, so they still appear when remote images are blocked in many clients, but they increase message size. Linking to a hosted image keeps the message small but depends on the client loading remote content. See blocked images.
Choosing the right structure
| Message content | Structure |
|---|---|
| Plain text only | text/plain |
| HTML only (not recommended) | text/html |
| Text and HTML | multipart/alternative: text/plain, then text/html |
| Text and attachments | multipart/mixed: text/plain, then attachments |
| Text, HTML and attachments | multipart/mixed: [multipart/alternative: text/plain, text/html], then attachments |
| Text, HTML and inline images | multipart/alternative: text/plain, then multipart/related: [text/html, images] |
| Text, HTML, inline images and attachments | multipart/mixed: [multipart/alternative: text/plain, multipart/related: [text/html, images]], then attachments |
The full structure as a tree
multipart/mixed
+-- multipart/alternative
| +-- text/plain
| +-- multipart/related
| +-- text/html
| +-- image/png (Content-ID: <[email protected]>)
+-- application/pdf (Content-Disposition: attachment)Content-Transfer-Encoding and character sets
Mail transport was designed for 7-bit text, so MIME defines transfer encodings that make other content safe. base64 encodes binary data such as images and PDFs and grows it by about a third, which affects size limits. quoted-printable encodes mostly-text content with occasional special characters and keeps it readable. 7bit and 8bit declare the content as is, and 8-bit transport depends on the servers on the path supporting it. Declare the character set on text parts, with UTF-8 the usual choice. Non-ASCII characters in header values such as Subject use encoded words instead. See UTF-8 in email, encoded words and base64 overhead.
Building a message in code
Do not assemble boundaries by hand. Use a library that builds the tree for you. The Python standard library produces the structure above in a few lines.
from email.message import EmailMessage
import mimetypes
msg = EmailMessage()
msg['From'] = 'Alerts <[email protected]>'
msg['To'] = '[email protected]'
msg['Subject'] = 'Monthly report'
msg.set_content('Hello,\nThe report is attached.') # text/plain
msg.add_alternative('<p>Hello,</p><p><img src="cid:logo"></p>', subtype='html')
# inline image tied to the HTML part (multipart/related)
html_part = msg.get_payload()[1]
with open('logo.png', 'rb') as f:
html_part.add_related(f.read(), 'image', 'png', cid='<logo>')
# attachment (promotes the outer message to multipart/mixed)
with open('report.pdf', 'rb') as f:
msg.add_attachment(f.read(), maintype='application', subtype='pdf', filename='report.pdf')
print(msg.get_content_type()) # multipart/mixedFrameworks such as Nodemailer, PHPMailer and Jakarta Mail offer the same: set text, html, inline images and attachments, and let the library choose the containers. For attachment size and encoding effects, see size limits and MIME, and for a broader tour of content types see multipart content types explained.
How clients treat the structure
Clients differ in how they interpret edge cases, so build the conventional tree and avoid clever variations. Most desktop and webmail clients show the last alternative they understand, list parts of a mixed container that have an attachment disposition as attachments and treat inline parts as part of the document. Some clients, particularly on mobile, are lenient with nesting mistakes and others are not, which is why a message can look right in one client and broken in another. Mailing list software and some gateways rewrite messages, for example by adding a footer to a text part or by converting HTML to text, and a clean structure survives that better than a tangled one. If your signature is DKIM-signed, remember that the body hash covers the MIME body exactly as sent, so any gateway that modifies it can break verification.
Content-Disposition and filenames
The Content-Disposition header tells the client whether a part is meant to be displayed in place (inline) or offered as a file (attachment), and carries the file name. Keep file names simple. If a name contains characters outside ASCII, use the RFC 2231 parameter encoding (for example filename*=UTF-8''r%C3%A9sum%C3%A9.pdf), which libraries produce for you, because older clients may mishandle raw non-ASCII names. Set an accurate Content-Type for every attachment, such as application/pdf, and fall back to application/octet-stream only when the type is unknown. Mismatched types, for example a script labelled as an image, are a common trigger for security scanners, and some providers block certain executable file types regardless of the label.
Common mistakes
- HTML only, no text part. Add a text alternative.
- Text and HTML in the wrong order. HTML must be last so that it is preferred.
- Putting attachments inside alternative. Alternative is for versions of the same content, not different files.
- Inline images outside related. Clients may show them as attachments or not at all.
- Missing or mismatched Content-ID. The cid in the HTML must match the header exactly, without the angle brackets in the HTML.
- Unencoded 8-bit content sent to servers that do not support it, leading to corrupt characters.
- Boundary collision or malformed boundaries, such as a missing final marker.
Troubleshooting
| Symptom | Likely cause | What to do |
|---|---|---|
| Attachment appears in the body or as odd text | Wrong Content-Type or disposition | Use multipart/mixed and set Content-Disposition: attachment |
| Inline image shows as a paperclip attachment | Image outside multipart/related, or no Content-ID | Wrap HTML and images in related and match the cid |
| Client shows the plain text, not HTML | HTML missing or placed first in alternative | Put text/plain first, text/html last |
| Garbled accents | Wrong charset or encoding | Declare utf-8 and use quoted-printable or base64 |
| Message rejected as too large | Base64 growth and many parts | Reduce attachments or link to hosted files |
| Spam filter penalty | Image-only or HTML-only mail | Add real text and a text part |
A well-formed MIME tree is invisible when it works. Keep text before HTML in alternative, group inline resources with related, put attachments in mixed and let a library generate the boundaries. When something looks wrong, view the raw source of the message and compare its structure with the tree above. See how to view the original message.
Send multipart messages over SMTP or API Submit text, HTML, inline images and attachments, and track delivery with webhooks.
References and further reading
Frequently asked questions
What is multipart/alternative used for?
It holds several versions of the same content, such as plain text and HTML, so the client can show the best one it supports.
When do I need multipart/mixed?
When the message includes attachments or other independent parts in addition to the body.
What is multipart/related for?
It groups an HTML document with resources it uses, such as inline images referenced by cid links.
In what order should text and HTML be placed?
Text/plain first and text/html last, because clients prefer the last part they can display.
Why does my inline image show as an attachment?
It is probably outside multipart/related or its Content-ID does not match the cid reference in the HTML.