Converters

How to Extract Attachments From an MBOX File on Mac

There is nothing to unzip. Attachments live inside the mail as encoded text and a short script or a purpose built tool pulls them out.

4 min read

Quick AnswerExtract attachments from an MBOX file on Mac in 2 ways. Use a Python script or bulk software. Scripts walk every message and save named attachments. Bulk software suits large mailboxes and many MBOX files. Work on a copy of the mailbox. Count the saved files afterwards.

Double clicking an MBOX file shows you nothing and no mail app gives you a Save All button for an entire file. Two routes exist and both are covered below. Each one explains what it saves and when it stops being practical.

Why Extract MBOX Attachments?

People extract MBOX attachments from mailbox exports to recover documents, photos and invoices without a mail app. Others extract attachments before they archive a mailbox so the files remain open for search and sharing.

Mail apps rarely offer a Save All button for an entire mailbox. You extract attachments in bulk instead, with a script or with bulk software. Both routes save the files into a folder you choose.

Where Do Attachments Live in an MBOX File?

MBOX files are plain text with messages joined end to end. Every attachment is a MIME part inside its message, encoded as base64 so binary data survives as printable characters. RFC 2045 defines that encoding.

Base64 turns every three bytes into four characters so attachments inflate the file by about a third. That is why a mailbox full of photographs produces an MBOX file far larger than the mail suggests. Our article on the MBOX format explains the rest of the structure.

How Do You Extract Attachments From an MBOX File on Mac With Python?

Who it suits. Technical users who work in Terminal. Source file size limit. None in the script. Extra application. Python 3 installed on your Mac.

Python’s standard mailbox module opens an MBOX file and the email package decodes every part. You need Python 3 installed and nothing else.

import mailbox, pathlib, sys
src = sys.argv[1]
out = pathlib.Path(sys.argv[2])
out.mkdir(exist_ok=True)
count = 0
for n, msg in enumerate(mailbox.mbox(src)):
    for part in msg.walk():
        name = part.get_filename()
        if name:
            data = part.get_payload(decode=True)
            safe = pathlib.Path(name).name
            (out / f"{n:05d}_{safe}").write_bytes(data)
            count += 1
print(count, "attachments saved")
Every part with a file name is decoded and saved. Message numbers in front of every name stop two files called report.pdf from overwriting one another.

Save the script as extract.py, then enter python3 extract.py mail.mbox attachments in Terminal. Files go into a folder named attachments and the script prints how many it found. Test it on a copy of the mailbox first. Compare the number the script prints with the number of files in the folder.

Why Does the Script Number Every File?

Two messages can contain attachments with the same name, for example report.pdf. The script adds the message number in front of every file name so one file never overwrites another. Sort the output folder by name and files from the same message sit together.

How Big Is the Output Folder?

Attachment text inside an MBOX file is about a third larger than the files it describes. The extracted files therefore total about three quarters of that text. Check the free space on your Mac before you start a large mailbox.

Which Attachments Does the Script Ignore?

Parts without a file name are skipped so unnamed inline images do not appear. Attachments inside forwarded messages are found because the walk descends into them.

Large files need patience. Mailboxes of many gigabytes take time to scan and the script shows no progress.

When Does Software Beat the Script?

MacXTools MBOX Attachment Extractor is built for the same task without a script. Use it when the mailbox is large, when you have many MBOX files or when other people need to do the work.

MacXTools reads .mbox, .mbx and .mbs files and files with no extension. Date, file size and file type filters limit which attachments are saved. A report lists the attachment count and fail count of every file. You can extract MBOX attachments and check the result without counting by hand.

The table compares the three routes. A mail app opens single messages but has no Save All button for an entire mailbox.

RouteWhat it doesBest for
Python scriptSaves every named attachment of one MBOX file per commandTechnical users with one or a few mailboxes
Mail appOpens single messages and saves their attachments one by oneA handful of known messages
Bulk softwareProcesses a folder of MBOX files in one sessionLarge mailboxes and many files

Which Route Handles a Folder of MBOX Files?

Extract attachments from several mailboxes by entering the command again for every file. One command processes one MBOX file. Use a separate output folder for every mailbox so file names never mix. Run the same settings on every file so you extract MBOX attachments in a comparable way. Bulk software handles a folder of MBOX files in one session.

What Should You Do With the Extracted Files?

After you extract attachments, sort the output folder by name so files from the same message sit together. Open some of the largest files and some of the oldest before you rely on the folder. Store a copy on an external disk and retain the MBOX file until the checks pass. Write down the folder name and the date so you can find the files later. Large mailboxes can contain thousands of files so a clear folder name for every mailbox saves time later.

Is It Safe to Open Extracted Attachments?

Extraction saves each file without starting it. Risk begins when you open one. Treat attachments from unknown senders as you would an unexpected email attachment. Check the file type first and avoid executables and scripts. Open documents in an app that you update regularly. If a mailbox export unexpectedly holds an executable, ask the sender before you open it.

How Do You Confirm Every Attachment Was Saved?

Count the files you extracted and compare the number with what you expect. Open a few of the largest ones and a few of the oldest. Our article on checking a converted mailbox shows the same method for mail. For attachments inside PDF files, see our guide on extracting attachments from a PDF on Mac.

Common Questions

Are Attachments Stored Inside an MBOX File?

Yes. Every attachment is a MIME part inside its message, encoded as base64 text. No separate folder stores them.

Why Is My MBOX File Larger Than the Mail?

Base64 turns three bytes into four characters so attachments grow by about a third inside the file.

Can Python Open a Large MBOX File?

Yes. Python’s mailbox module opens the file and the email package decodes every part. Mailboxes of many gigabytes take time to scan and the script shows no progress.

Which Python Version Does the Script Need?

Python 3. Both the mailbox module and the pathlib module ship with Python.

Does the Script Alter the MBOX File?

Reading is all it does but work from a copy anyway so the original remains safe.

Why Are Some Attachments Missing From the Output?

Only parts with a file name are saved so unnamed inline images do not appear.

Sources

Every version, option and date above is taken from these pages and not from memory.

  1. mailbox, manipulate mailboxes in various formats, Python documentation Python Software Foundation
  2. RFC 2045, MIME Part One, on the base64 transfer encoding IETF
  3. Mbox, on the file format Wikipedia