โ† Back to Blog

How Base64 Is Used in Email Attachments

2026-04-09 ยท 5 min read

โ† Back to Blog

How Base64 Is Used in Email Attachments

ยท 5 min read

Historical Background of the MIME Standard

Early internet email protocols (SMTP) were designed to transmit plain ASCII text, using only 7 bits per byte (values 0โ€“127). This meant binary files (images, documents, executables) couldn't be directly transmitted through email. The MIME (Multipurpose Internet Mail Extensions) standard was proposed in 1992 (RFC 1341), introducing Base64 as a mechanism for safely transmitting binary data in email.

To this day, all mainstream email clients follow the MIME standard. Every time you send an email with an image or file attachment, those attachments are automatically Base64-encoded behind the scenes, then decoded and restored after transmission. This process is completely transparent to users.

MIME Email Structure Analysis

A MIME email with attachments consists of multiple "parts", each with its own headers and content body. The entire email is of type multipart/mixed, the text body is a text/plain or text/html part, and each attachment is a separate part, separated by boundary strings.

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

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

Hello, please find the attachment.

------=_Part_1234
Content-Type: application/pdf; name="report.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="report.pdf"

JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9UeXBlIC9DYXRhbG9nCi9QYWdlcyAy
...๏ผˆBase64 ๆ•ฐๆฎ๏ผŒๆฏ76ๅญ—็ฌฆๆข่กŒ๏ผ‰...

------=_Part_1234--

The Content-Transfer-Encoding Header

The Content-Transfer-Encoding header tells the email client how the data in this part is encoded. Common values include: 7bit (pure ASCII text, max 998 characters per line), 8bit (can contain non-ASCII bytes, but line limits still apply), quoted-printable (suitable for mostly ASCII text with occasional special characters), and base64 (suitable for binary data).

For binary files, base64 encoding should always be used. MIME Base64 requires inserting CRLF (\r\n) after every 76 characters โ€” this is an important difference from using Base64 in other contexts (like JSON APIs).

Sending Email with Attachments in Python

import smtplib
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

def send_email_with_attachment(sender, recipient, subject, body, file_path):
    msg = MIMEMultipart()
    msg['From'] = sender
    msg['To'] = recipient
    msg['Subject'] = subject

    # ๆทปๅŠ ๆญฃๆ–‡ / Add body
    msg.attach(MIMEText(body, 'plain'))

    # ๆทปๅŠ ้™„ไปถ / Add attachment
    with open(file_path, 'rb') as f:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(f.read())

    # Base64 ็ผ–็ ้™„ไปถ / Base64 encode attachment
    encoders.encode_base64(part)
    part.add_header(
        'Content-Disposition',
        f'attachment; filename="{file_path}"'
    )
    msg.attach(part)

    with smtplib.SMTP('smtp.example.com', 587) as server:
        server.starttls()
        server.login(sender, 'password')
        server.sendmail(sender, recipient, msg.as_string())

Inline Images vs Attachments

In HTML emails, images can be embedded in two ways: as attachment references (via CID) or directly as Base64 Data URIs. With CID, the image is attached to the email as an inline attachment, and the HTML body references it via cid:unique-id. This is the preferred approach for desktop clients like Outlook and Apple Mail.

Using Base64 Data URIs directly is simpler, but many major email clients (especially Gmail and Outlook Web) filter out Data URI images for security reasons. Therefore, in production-grade HTML emails, CID embedding or external URLs should be preferred over Data URIs.

Base64 Impact on Email Size

Base64 encoding increases attachment size by approximately 33%, and MIME line breaks (every 76 characters) add another ~1.5% overhead. Therefore, a 1MB PDF attachment actually occupies about 1.37MB in the email. This explains why email providers typically have strict size limits (like Gmail's 25MB cap).

For large files, using cloud storage links (like Google Drive, OneDrive) instead of direct attachments is recommended. This avoids Base64 size inflation, works around email size limits, and allows recipients to always view the latest version of the file.

Debugging Email Attachment Issues

When attachments fail to display properly or appear corrupted, common causes include: incorrect Base64 data line endings (should be CRLF, not just LF); incorrect MIME type in the Content-Type header; missing required newline at the end of Base64 data; boundary string appearing in the email body (causing structure parsing errors).

When debugging, use email analysis tools (like MXToolbox's email header analyzer) to inspect email structure, or use Python's email module to parse raw email content and check the headers and Base64 data integrity of each part.

Try the online tool now โ€” no installation, completely free.

Open Tool โ†’

Try the free tool now

Use Free Tool โ†’