# Archiving old mail with formail

I’ve been trying to clean up my email at work lately. When a new project comes in, I create an email folder for it in MailMate and then save the incoming and outgoing messages associated with that project in the folder. When the project is finished, I’m faced with four options:

1. Delete the project folder and its messages. This gets rid of the messages on both the server and my local computer. I don’t like doing this because you never know when a client is going to come back asking for information that’s squirreled away in an email.
2. Move the project folder into an “Old projects” folder. This preserves the messages on both the server and my local computer, but keeps the MailMate interface cleaner by hiding folders I don’t expect to access. The problem with this is that messages accumulate on my local computer—not a concern with for the iMac with a 3 TB hard drive, but a bit of a squeeze on my old MacBook Air with only a 128 GB SSD.
3. Go into and change the subscription settings for the project folder.

This keeps the messages on the server but allows my local computer to reclaim some disk space. And I can always change the setting back temporarily if I need to get at the old messages.

4. Archive the messages in mbox format, save the archive with the rest of the old project files, and delete the project folder and its messages. This gets rid of the messages from both the server and my local computer, but preserves them along with the rest of the file materials in an offline archive.

Choice 4 is my preference, with Choice 3 coming in a close second. Unfortunately, MailMate—which I switched to when Mail.app on Mavericks refused to collect mail from the GMail servers on a regular basis—has no Export command, so I’ve been forced to go with Choice 3 since early last year. But today I found the formail command, and it looks like I can return to archiving and deleting.

I learned about formail from this Stack Exchange page, in which the questioner wanted to be able to open an email stored in eml format in Mutt. The top answer used formail to convert the eml into mbox so Mutt could open it. MailMate stores its messages in eml format, so it took only a little messing around to adapt the answer to handle a folder full of messages in eml format.

Here’s what I do to make an mbox archive:

• In the Finder, navigate to the folder on my local disk where the eml files for the project are stored. This is deep within ~/Library/Application Support/MailMate/, but it’s pretty easy to follow the trail to the correct folder.
• Open a Terminal window and change the working directory to the folder found in the previous step. I use a Keyboard Maestro macro to do this, but you could also drag the Finder window’s proxy icon fill in the argument to the cd command.
• Issue the following command:

for f in *.eml; do formail < "\$f" >> ~/Desktop/test.mbox; done


This converts all the messages in turn and strings them together into a single mbox archive file. Obviously, the archive doesn’t have to be named test.mbox and it doesn’t have to be saved on the Desktop, but that’s what the form of the command should be.

I’ve tested this command several times on different mail folders, and it’s always worked well. The easiest way to test is to open the mbox file in Mutt:

mutt -f test.mbox


which, if it works, leads to something like this:

Mutt isn’t shipped with OS X, but it’s easy to install through Homebrew. I’m sure there are other ways to test an archive, but I was a happy Mutt user back in my Linux days, so that’s what I thought of first.

formail is part of the old Unix procmail system. I was a procmail user in my Linux days, too, but definitely not a happy one. Only a masochist could enjoy editing a procmail recipe.

Update 9/4/15 4:07 PM