Thursday Night

Paul Betts’s personal website / blog / what-have-you

New Program: Merge Thunderbird / Evolution style (mbox) folders

To keep my desktop and my laptop in sync with each other, I use a program called Unison, and it works great for almost everything I throw at it. It’s fast, smart, and has the right amount of control and options to do exactly what I want, except for when it comes to one thing: mail.

Unfortunately, most of the time Unison’s diff-style merge doesn’t work for mbox mail files, which is what both Evolution and Mozilla SeaMonkey / Thunderbird (as well as a lot of other Email clients) use. It’s about impossible to merge two mail folders by hand if you have even more than 50 or so messages, and I didn’t know any way to do it using existing tools. So to fix this, I wrote a small command-line Ruby program that knows how to merge two mail folders without duplicates. Once I finished though, I had no idea if my program wasn’t losing mail messages in the merge, so I wrote another program that displays the differences between two mail folders in terms of messages.

Some examples:

# Merge three mailboxes
mboxmerge.exe Inbox_A Inbox_B Inbox_C > New_Inbox

# Show the differences between two mailboxes
mboxdiff.exe Inbox_A Inbox_B

# Merge all mailboxes that begin with the letter C
cat `find ~/mailboxes | grep ‘^C’` | mboxmerge.rb > C_Mailboxes

That last one was getting a little bit fancy, but you get the idea. The program uses the From, Subject, To, the Date, and the message text to determine if a message is unique, so it’s pretty thorough at telling which messages are identical between the mail files, so it’s safe to use it for synchronization purposes (which is why I wrote it anyways!)

Now, I use this on my own files and I’m reasonably sure that it won’t chew up your mail, but please make sure that you have a backup in case things go wrong or my program randomly freaks out and deletes everything. It will never write to its input files, so there’s no chance of that failing but the standard disclaimer applies.

Update: Also check out the script to automate syncing the local and remote copies of a mail folder

Written by Paul Betts

February 15th, 2007 at 10:26 pm

Posted in Linux,Microsoft,Ruby