I recently wanted to do something about the hundreds of thousands of emails I currently have stored on multiple email accounts. It would be nice to fetch all of them at the same place, find and remove duplicates, then store them with proper classification in a nice new maildir. I could then make sure all messages are fetched and stored in this new place.

The first step was to export messages. Google Mail can export messages as a mbox file. Mbox is a very old format that actually has several variants. Google Mail uses the mboxrd format.

I decided to use Go; I found two packages dealing with mbox files: github.com/aerth/mbox only supports writing them, while github.com/blabber/mbox does not indicate which format it supports and does not handle the from_ encoding used in mboxrd files. So I ended up writing a new package, github.com/galdor/go-mbox. It’s designed to support multiple mbox formats, but currently only supports mboxrd.

For example, the following code opens a mbox file and iterates through its messages:

mbox, _ := mbox.Open(path, mbox.Mboxrd)

for {
    msg, _ := mbox.Read()
    if msg == nil {
        break
    }

    fmt.Printf("%s\n", msg.Id)
}

mbox.Close()

I also wrote a small tool to work with mbox files; for the time being, it can be used to list messages in a mbox (with a custom template for each entry), and can extract messages to a maildir.

In the future, I might add new features such as support for other mbox formats, filtering, or the ability to add new messages.