Migrating from Thunderbird to Outlook email

My company provided a deal with Microsoft that allowed me to purchase Office 2010 Professional for less the 10 pounds. Hard to ignore. This version of Office includes Outlook, so I was able to use it as my email client that then Thunderbird.

The only problem was, how to migrate my old email messages from Thunderbird to Outlook? There's plenty of help to go in the other direction, but not so much for what I wanted to do.

However, it wasn't that difficult. I found a VB program, written by Ricardo Drizin, available here, that would import sets of .eml files into Outlook, so all I needed to do was write something to convert Thunderbird mbox files to a set of .eml files, suitable for Ricardo's program.

Here's the python program:

#!/usr/local/bin/python
"""
 NAME
    split.py

 SYNOPSIS
    python split.py [-r root_dir]  [-s regex] file [file] ...

 DESCRIPTION 

    split.py converts a Thunderbird mbox file into a set of .eml
    files.  Multiple mbox files may be specified on the command line.
    The resulting eml files are written to a directory with the same
    name as the mbox file.  Each eml directory is created in the
    directory named by the -r argument, or /tmp, if not specified.

    split.py supports the following command arguments:

    -f  suffix    sets suffix of output file.  Default is .eml.
                  A value of .eml causse split.py to strip white space
                  from the regex string (or Outlook isn't happy)
    -r root_dir   sets root directory for the output .eml directories and 
                  files
    -s regex      defines regular expression at which to split the input 
                  files.  Default (r'\n\nFrom -') works for Thunderbird 
                  mbox format.

 MODIFICATION HISTORY
 Mnemonic        Rel  Date     Who
 split.py        1.0  20120617 mpw
    Created
    
"""

import os
import re
import sys
import getopt


#+++++++++++++++++++++++++++++++++++++++++++++++++
# start of program
#+++++++++++++++++++++++++++++++++++++++++++++++++

# defaults
root_dir ="/tmp"
split_regex = r'\n\nFrom -'
suffix = ".eml"

# read command line arguments, if any
try:
    opts,args = getopt.getopt(sys.argv[1:],'f:r:s:')
    for o,v in opts:
        if o == '-r': root_dir = v
        elif o == '-f': suffix = v
        elif o == '-s': split_regex = v
except getopt.GetoptError:
    print("%s: illegal argument: %s"% (sys.argv[0],o))
    sys.exit(0)

sr = re.compile(split_regex)

for file in args:
    with open(file) as fp:
        content = fp.read()
    content = open(file).read()
    chunks = sr.split(content)
    headers = sr.findall(content)
    target_dir = root_dir+"/"+file
    if not os.path.exists(target_dir):
        os.makedirs(target_dir,mode=0777)
    fno = 0
    for chunk,header in zip(chunks,headers):
        if len(chunk.strip()) == 0: continue
        fn = "%s/%04d%s"%(target_dir,fno,suffix)
        h = open(fn,mode="w")
        if suffix == ".eml": 
            h.write(header.strip())
        else: 
            h.write(header)
        h.write(chunk)
        h.close()
        fno = fno+1
    print "Wrote %5d eml files to %s"%(fno,target_dir)