Understanding Maildir and mbox mail store formats

Updated -

Overview

There are several ways to store email on disk. Two of the most common are mbox files and Maildir directories. In the case of mbox files, a mailbox is a single file, with all email messages contained therein appearing one after the other. Maildir directories are slightly more complex, with individual emails stored one per file in a simple directory structure.

In an mbox file, mail messages are separated by their leading envelope From header lines - the word From, first letter capitalized, followed by a space character.

A Maildir folder is composed of an enclosing folder, and three sub-folders: new/, cur/, and tmp/. In general you can disregard tmp/, as it's used in delivery. At rest, you'll note files existing in new/, one for each new email that's been delivered to the mailbox, and files in cur/, one per email that has already been seen by a mail client. From the client's perspective, all of them are in one mailbox.

There are various ways of presenting a hierarchy of mail directories and various naming conventions for them, but we'll focus on the mail content.

mbox format

Here's a simple illustration with two email messages that start off in a mailbox file and transition back and forth to and from Maildir.

[example@rhel77 ~]$ ls
mail
[example@rhel77 ~]$ file mail 
mail: ASCII text
[example@rhel77 ~]$ cat mail 
From example@redhat.com Tue Oct 27 08:54:57 2020
Date: Tue, 27 Oct 2020 08:54:54 -0400
From: Example Engineer <example@redhat.com>
To: example@redhat.com
Subject: test email
Message-ID: <20201027125454.GB7748@redhat.com>

This is a test email.

-- 
Example Engineer - he / him / his
Software Maintenance Engineer
Red Hat, Inc.

From example@redhat.com Tue Oct 27 08:54:59 2020
Date: Tue, 27 Oct 2020 08:54:58 -0400
From: Example Engineer <example@redhat.com>
To: example@redhat.com
Subject: second test email
Message-ID: <20201027125458.AB1234@redhat.com>
Content-Length: 114
Lines: 6

This is a second test email.

-- 
Example Engineer - he / him / his
Software Maintenance Engineer
Red Hat, Inc.

[example@rhel77 ~]$

We can look at this file in Mutt by saying:

$ mutt -f mail

From within Mutt, we can see both emails:

q:Quit  d:Del  u:Undel  s:Save  m:Mail  r:Reply  g:Group  ?:Help
   1 N   Oct 27 Mason Loring Bl (   6) test email
   2 N   Oct 27 Mason Loring Bl (   6) second test email
---Mutt: mail [Msgs:2 New:2 0.7K]---(date/date)-------------------------(all)---

Maildir format

Let's make a Maildir folder:

[example@rhel77 ~]$ mkdir -p Maildir/{new,cur,tmp}
[example@rhel77 ~]$ find Maildir/
Maildir/
Maildir/new
Maildir/cur
Maildir/tmp

Now we can save one of our emails into this. From within Mutt, press s and offer ~/Maildir/ as the destination. Let's do this with the second email. We'll quit Mutt and let it purge the old message out of mail, which it will ask to do automatically. Now let's see what we've got:

[example@rhel77 ~]$ cat mail 
From example@redhat.com Tue Oct 27 08:54:57 2020
Date: Tue, 27 Oct 2020 08:54:54 -0400
From: Example Engineer <example@redhat.com>
To: example@redhat.com
Subject: test email
Message-ID: <20201027125454.GB7748@redhat.com>
Status: O
Content-Length: 107
Lines: 6

This is a test email.

-- 
Example Engineer - she / her / hers
Software Maintenance Engineer
Red Hat, Inc.

[example@rhel77 ~]$ find Maildir/
Maildir/
Maildir/new
Maildir/cur
Maildir/cur/1603803919.1461_1.rhel77:2,
Maildir/tmp
[example@rhel77 ~]$ cat Maildir/cur/1603803919.1461_1.rhel77\:2\, 
Date: Tue, 27 Oct 2020 08:54:58 -0400
From: Example Engineer <example@redhat.com>
To: example@redhat.com
Subject: second test email
Message-ID: <20201027125458.AB1234@redhat.com>
Content-Length: 114
Lines: 6

This is a second test email.

-- 
Example Engineer - she / her / hers
Software Maintenance Engineer
Red Hat, Inc.

Differences between the two formats

Take careful note of how the mbox file does not have the same internal format as each individual message file from within the Maildir.

We can look at the Maildir with Mutt:

$  mutt -f Maildir/

...and see:

q:Quit  d:Del  u:Undel  s:Save  m:Mail  r:Reply  g:Group  ?:Help
   1 O   Oct 27 Mason Loring Bl (   6) second test email
---Mutt: Maildir/ [Msgs:1 Old:1 0.3K]---(date/date)---------------------(all)---

Note that we've pointed Mutt at a proper Maildir, and not a piece extracted from a Maildir. If we said:

$ mutt -f Maildir/cur/1603803919.1461_1.rhel77:2,

...we'd see an error message from our mail client, as that file is not a properly-formatted mailbox file.

Comments