School of Computing

Using Maildir

Introduction to Maildir

Maildir is a mechanism for storing email on a mail server.

Where the more commonly used mbox format stores email folders (such as your inbox) in a huge flat file for each folder, Maildir stores each mail separately.

Benefits of Maildir include:

  • Nested folders.
  • Less chance of mailbox corruption by delivery agent and mail client competing for a lock on the mail folder.
  • More efficient, providing for faster email storage and retrieval.
  • Less overhead on backups and restores.

Getting started with Maildir

Storing the email

In order to use Maildir you need to create a .forward file in your home directory with instructions to tell the mail system to deliver your email in Maildir format, otherwise the mail system defaults to mbox format mail delivered into a file called .mail.

A filter in its most simple case will look like:

	# Exim filter
	save ${home}/Maildir/

This performs no filtering and just delivers the email to your Maildir inbox.

All email is stored under a folder name called Maildir. This can be changed but we do not recommend that you do so.

Do not be tempted to omit the first line, as the first line of a mail filter must begin with a '#'.

Reading your email

In order to read your email you must use an IMAP enabled mail client (such as Thunderbird or Outlook Express).

Set the mail client to use an SSL IMAP connection on port 996 - NOTE that the port number is important as we have a Maildir enabled IMAP daemon running on a separate port to the more usual mbox enabled daemon.

Most mail clients should automatically get the root folder directory for your email folders from the IMAP daemon. If your mail client does not do this, you should set it to:

	~/Maildir

Filtering out spam

One common use of extra filtering is to filter out recognised spam mail, to do this change the above .forward file example to:

        # Exim filter

        #trap spam and save it into a spam folder
        if (unknown variable message_headers): contains "X-UKC-CSSpamCheck-Flag: YES"
        then
                save ${home}/Maildir/.spam/
                finish
        endif

        # save all other email into my inbox
        save ${home}/Maildir/

Advanced use of Maildir

Directory structure

At a Unix prompt, if you change directory to Maildir and type ls you will see three directories listed:

	$ cd Maildir
	$ ls
	cur new tmp
	$
These directories contain your inbox.
  • cur contains your current mail that has been marked as read by your mail client.
  • new contains new email that has been delivered that has not been marked as read by your email client.
  • tmp is used by the mail system as temporary storage whilst your email is being delivered. Just ignore this directory.

If you perform any mail filtering, to see your filtered mail directories you must do a full directory listing:

	$ cd Maildir
	$ ls -a
	.                         .spam
	..                        .sungrid
	.backups                  .tuhs
	.cert                     .ukc-alert
	.cluster                  .ukmm
	cur                       new
	tmp

The entries beginning with a . are directories, each one of which contains a cur, new and tmp as Maildir requires and are created automatically if your mail is pre-filtered via a .forward file or if you create folders via your email client and move the mail into them.

Filtering

Filtering (or presorting) your email is straightforward, though there are some subtle differences to normail Exim mail filter files.

Take the following example:

        if (unknown variable h_subject): contains "[lego robotics]"
        then
                save ${home}/Maildir/.lego/
                finish
        endif

The example takes all email containing the text '[lego robotics]' in the subject line and saves it in a folder called lego under the Maildir folder. NOTE: that the folder is prefixed by a '.' and suffixed by a '/', the '.' and '/' are REQUIRED in folder names within the mail filter as they tell Exim to deliver the email in Maildir format.

Hierarchical filtering

As mentioned as one of the benefits of Maildir, you can nest mail folders, here is an example of how to do this:

        if (unknown variable h_from): contains "marge@family.com"
        then
                save ${home}/Maildir/.family.auntymarge/
                finish
        endif
        if (unknown variable h_from): contains "homer@family.com"
        then
                save ${home}/Maildir/.family.unclehomer/
                finish
        endif

In your email client, it will appear that you have a folder called family containing two subfolders: auntymarge and unclehomer.

On the Unix side, though it will look like this inside the Maildir directory:

	.                         .family.auntymarge
	..                        .family.homer
	.spam                     cur
	new                       tmp

That is of course assuming that you are filtering for spam as well.

School of Computing, University of Kent, Canterbury, Kent, CT2 7NF

Enquiries: +44 (0)1227 824180 or contact us.

Last Updated: 21/03/2014