Convert emails from Outlook MSG to Thunderbird EML format using MSGConvert

Outlook MSG to Thunderbird EML conversion picture

Recently, I’ve had the need to convert a bunch of outlook MSG files to a format that Thunderbird can read (EML).

There are various ways of doing this according to this article on kb.mozillazine.org, but I have chosen the MSGConvert method so I can script the conversion process.

To use MSGConvert, you’ll need the MSGConvert script along with its Perl dependencies (you’ll need to compile one if you are using Ubuntu/Xubuntu).

Installation

#!/bin/bash

## Install all dependencies for "Email::Outlook::Message" - "Email-Outlook-Message-0.909"
aptitude install 
libemail-mime-contenttype-perl 
libemail-mime-creator-perl 
libemail-mime-modifier-perl 
libemail-simple-perl 
libio-all-perl 
libole-storage-lite-perl 
libio-string-perl    # Needed to pass "make test"

## Compile "Email::Outlook::Message"
wget http://search.cpan.org/CPAN/authors/id/M/MV/MVZ/Email-Outlook-Message-0.909.tar.gz    # From http://search.cpan.org/~mvz/Email-Outlook-Message-0.909/lib/Email/Outlook/Message.pm
tar -zxf "Email-Outlook-Message"*".tar.gz"
cd "Email-Outlook-Message"*
perl Makefile.PL
make
make test
sudo make install

## Note: To uninstall
# cd "Email-Outlook-Message"*
# sudo make uninstall
# aptitude purge .....


## Install dependency "Email::LocalDelivery"
aptitude install libemail-localdelivery-perl


## Install msgconvert.pl
wget 'http://www.matijs.net/software/msgconv/msgconvert.pl' -O msgconvert.pl
sudo mv msgconvert.pl /usr/bin

MSGConvert usage

Once installed, you can convert a single message by doing:

msgconvert.pl some_message.msg

# Please note the script chucks out a file with a .mime extension, it's an easy fix:
mv some_message.msg.mime some_message.eml

Automation

Alright, let’s automate it a little bit. Here is what I used to convert a bunch of msg files to eml:

#!/bin/bash

MSGCONVERT="/usr/bin/msgconvert.pl"

if (( $(echo "$1"|grep -icE "^-[-]?(help|h)$")==1 || $#==0 )); then
	echo 
"Summary:
	Wrapper script for msgconvert.pl.
	msgconvert.pl throws out the converted eml files in current directory and with the wrong extension...
	
	I suppose I ^could^ just edit msgconvert.pl...  Meh...

Usage:
	$(basename "$0") msgFile1.msg [msgFile2.msg] [msgFile3.msg]...

Example:
	$(basename "$0") messages/message1.msg messages/message2.msg messages/message3.msg
	find "emails/2009" -iname "*.msg" -print0|xargs -0 $(basename "$0")
"
	exit 0
fi

## Convert each message specified
for message in "$@"; do
	# Test to ensure it has a .msg extension (I ^could^ do a "hd|grep" test or add a "file" magicfile I suppose...  Again, meh...)
	if (( $(echo "$message"|grep -icE ".msg$")== 1 )) && [ -f "$message" ]; then
		# Convert - we can only do this one at a time
		echo "INFO: Converting: "$message""
		"$MSGCONVERT" "$message"
		if (( $? != 0 )); then echo "EXCEPTION: Encountered problem(s) at "$message"."; fi
	
		# Move the converted message to its original directory, changing extension at the same time
		#echo "DEBUG: mv "$(basename "$message".mime)"" ""$(echo "$message"|sed -e 's/.msg$/.eml/i')""
		mv "$(basename "$message").mime" "$(echo "$message"|sed -e 's/.msg$/.eml/i')"
	fi
done

Happy converting!

About these ads

2 Responses to “Convert emails from Outlook MSG to Thunderbird EML format using MSGConvert”

  1. E Hansen Says:

    Thanks a lot for your great explanation on how to install MSGConvert. The script works great after following your installation. If only I have discovered it earlier, it would have saved me many wasted hours…

    I’m still trying to figure out how to customize the automation script, though. I need to convert approx 15000 .msg files that are scattered down a directory tree. The automation script would need to traverse through the directory tree, convert all .msg files it finds to .eml files – and delete the original .msg files as it goes……

  2. norb ebeis Says:

    Here is bash script which traverses a directory tree and converts all *.msg files to *.eml. files ( The *.mime files which are created by MsgConvert.pl are renamed to *.eml and moved to the directory of the original msg file).

    #!/bin/bash
    function convertMsg {
    echo Directory “$1″
    find “$1″ -iname ‘*.msg’ | while read -r msgFile
    do
    echo Handling “$msgFile”
    # parse found name in path and filename with and without extentsion
    dirName=`dirname “$msgFile”`
    baseName=`basename “$msgFile”`
    fileName=”${baseName%.*}”
    # run convert script
    msgconvert.pl “$dirName/$baseName”
    # move created mime file to source directory
    mv “$baseName”.mime “$dirName/$fileName”.eml
    done
    }

    for param in “$@”; do
    convertMsg “$param”
    done


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: