mb2md: A very useful linux/unix email utility


The linux/unix tool mb2md does one thing and it does it well: It takes and "mbox" mailbox file and converts it into Maildir files.

In "mbox" mailbox format, your entire mailbox is one file. Each message is delimited with a blank line and the next line starting with "From ". When you get a new email, it is appended to the end of the mbox file. It's not horrible, it works just fine in most cases and most email clients can read it just fine.

In "Maildir" mailbox format, each new inbound email messages received is stored in a new file. That file contains only your email message. The file is placed in the "new" subdirectory, inside of the "Maildir" directory, inside of the user's home directory. 

Maildir makes life a lot easier if you want to feed email messages to a script. Sure, you could fiddle with pipes and dot-forwards, but if you use Maildir, messages are just dropped into that directory as individual files and it's super easy to have your script scan that directory periodically, to look for messages that merit processing.

Well, if you're like me and your linux box's postfix mail server configuration got overwritten yesterday, you might have ended up with about 75,000 inbound email messages in an mbox file that you want to process with a script. I started to write a shell script that would just loop through the messages and try to pull them apart, but it was pretty slow and fussy. So I looked around to see what utilities exist that can do that, and that's how I found mb2md!

And it's even simpler than I realized. Here's a not-too-bad tutorial, but I can even simplify that guide. Here's really all you have to do:
  • As root or with sudo, install mb2md, using your system's package manager. That might mean you run "apt-get install mb2md" or "yum install mb2md" or something else.
  • As the affected user, run mb2md -m
  • Boom, what was once in your mbox file is now inside your Maildir directory. In my case, everything went into the "cut" subdirectory, instead of the "new" subdirectory.
  • The filenames weren't created in the expected format used by postfix. If that matters to you, write a little script that renames and moves them to the "new" subdirectory. The output file name is not exactly what postfix uses, but was close enough for my purposes. Be sure to test.
cd ~/Maildir/cur
ls | grep m | (
while read FILE
do
NEWFILE=`date "+%y%m%d%H%M%S$$$RANDOM.$HOSTNAME"`
mv $FILE ../new/$NEWFILE
done
)

And finally, here are two things you need to keep in mind:
  • Make sure you've adjusted your postfix config to use Maildir format before you run this. Otherwise you could be receiving new mail in your "mbox" mailbox even after extracting what's there.
  • And note that the "mbox" mailbox is NOT emptied after you run mb2md. You need to zero it out. In my case I did that with "cat /dev/null > /var/mail/(username)".
  • There is no third thing.
1 Comments

Comments

  1. Al, Regarding your original problem, losing the Postfix mail server configuration on your Linux server. I recommend installing and setting up etckeeper to store the /etc directory tree structure in source code control. - Matt

    ReplyDelete

Comments policy: Al is always right. Kidding, mostly. Be polite, please and thank you.