Home » How To » Convert MailDir Emails To Eml or Mbox Format

Convert MailDir Emails To Eml or Mbox Format

The Problem:

I have a client who is trying to find emails that have gone missing. The mail server they have their emails on stores emails in an MailDir Email format, the format commonly used by Amivs / Postfix.

My first thought was “How can I full text index MailDir emails”. Other than install new functions on the mail server -there is no third party, open source tool to do this I have seen.

Option One : Grep.

The problem with this is you get one line back for each match found nd have to use Vim or similar to look at each file that matches your search. That also doesn’t let you search attachments.

Option two : Import them into a mail client

(Outlook, Thunderbird, Ximian Evolution, MailStore Home)

Use the mail client to index the emails and make them searchable using multiple search criteria.  ( As a side note – I mourn the loss of Lotus Notes that actually full text indexed all data, and allowed you to do true full text searching, including attachments, using AND / OR and NOT statements that it actually obeyed).

The problem is mail clients don’t read emails in MailDir email format. they need it in PST, .EML or MBOX format. So how do you export MailDir email formatted emails into MBox or EML formats?

There is a tool mb2md that converts from MailBox to MailDir format but no md2mb tool . So – I wrote one.

The Solution:

How to convert all your emails from an Amavis / PostFix / MailDir format into one that can be used by a mail client.

This code below requires Grep and procmail to be present. Procmail provides formail, which can export email into different formats.

If you have Windows you can do this using WSL. To install Run:

sudo apt-get update && sudo apt-get install procmail -y

If you have a flavour of Linux, then the Command Not Found site has instructions here.

The bash shell app.


# Enable dotglob option to include hidden directories
shopt -s dotglob

# Define the path to the Maildir directory

echo "Mail dir = $1"
sleep 1

# Iterate over each subdirectory (mail folder) within the Maildir directory
for folder in "$MAILDIR"/*/; do
    echo "Folder $folder"

    # Extract the folder name
    folder_name=$(basename "$folder")

    # Create a new MBOX file for the current folder

    # Convert emails in the current folder to MBOX format
    find "$folder" -type f -exec sh -c 'formail -s < "$1"' _ {} \; > "$mbox_file"
    echo "MBOX file created for folder: $folder_name"
echo "***** Completed *********"

exit 0

I have named this md2mb.sh

It is called by md2mb.sh /path/to/maildir


DON’T ADD A TRAILING / AT THE END OF YOUR /PATH/TO/MAILDIR    it mucks up file names nad I’m too lazy to be bothered checking if there is a trailing slash and remove it.

End Notes:

I used MailStore Home , which is both free, can be installed portably and is very good at spitting files into various formats, in order to search the emails.   Thunderbord, Outlook, and eM Client  are all able to work with eml or mbox emails. Outlook is probably the least cpaable of dealing with mbox but does handle eml files.