Base64 and MIME: How Email Transport Works

When you attach a PDF document, an image, or a zip file to an email and hit send, that binary file must travel across the internet through the Simple Mail Transfer Protocol (SMTP). The architectural problem is that SMTP was designed in the 1980s to strictly transport 7-bit ASCII text. It has absolutely no native capacity to handle raw 8-bit binary data.

If a client attempted to stream raw binary bytes into an SMTP server, the server would misinterpret the data, corrupt the file, and likely terminate the connection. The solution to this problem is MIME, powered by Base64 encoding.

What is MIME?

MIME stands for Multipurpose Internet Mail Extensions. It is an internet standard that extends the format of email to support text in character sets other than ASCII, as well as attachments of audio, video, images, and application programs. MIME acts as an envelope wrapper that tells the receiving email client exactly how the data inside the email is formatted.

How Base64 Enables Attachments

When you attach a file to an email, the email client performs the following operations behind the scenes:

  1. It reads the raw binary data of the file from your hard drive.
  2. It passes that binary data through a Base64 Encoder, transforming the binary file into a massive, safe block of ASCII text.
  3. It injects this text block into the body of the email.
  4. It adds MIME headers immediately above the text block to instruct the receiving client on how to handle it.

The Structure of a MIME Payload

A typical MIME attachment inside an email looks like this:

Content-Type: image/jpeg; name="photo.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="photo.jpg"

/9j/4AAQSkZJRgABAQEASABIAAD/4QAwRXhpZgAATU0AKgAAAAgAA...
... (thousands of lines of Base64 text) ...

The crucial header here is Content-Transfer-Encoding: base64. This tells the receiving email client (like Gmail or Outlook) not to display the block of text to the user, but rather to pass it through an internal Base64 Decoder. The decoder reconstructs the original binary JPEG file, and the client displays it as a downloadable attachment.

Line Length Restrictions in MIME

Unlike modern JSON APIs which usually transmit Base64 as one massive, unbroken string, MIME specifications enforce strict line length limits. According to RFC 2045, Base64 encoded lines within a MIME payload must be limited to a maximum of 76 characters. Every 76 characters must be separated by a carriage return and line feed (\r\n).

This limitation was designed to prevent buffer overflows in legacy SMTP servers, which were prone to crashing if they received text lines that were too long. Modern decoders are designed to ignore these newline characters when extracting the binary data.

The Bandwidth Cost

Because Base64 translates 3 bytes of raw data into 4 characters of text, attaching files to an email inflates their size by approximately 33%. A 15MB video file becomes a 20MB email payload. This is the primary reason email providers enforce strict attachment size limits (typically 25MB). The sheer volume of inflated text traversing the SMTP network requires massive bandwidth and server processing power.

Despite the overhead, the marriage of MIME and Base64 remains the foundational infrastructure that allows the modern, media-rich email system to function.