Migrate from Microsoft Exchange

From openSUSE

Most times, you only need to set up two IMAP accounts in your mail client, and then copy everything from the Microsoft Exchange account to the GNU/Linux account. However, subfolders and a great number of messages on the Microsoft Exchange account can trip up several popular mail clients. WARNING: DO NOT USE MOVE, USE COPY!

We want to copy all mail - read or not read - from a Microsoft Exchange account to a Maildir-directory on a GNU/Linux account. You only need to be logged into a shell on the GNU/Linux machine to do this. Please read the entire example before proceeding at your own risk! We will not delete any email, but you should do this without haste.

First, make sure no new email arrives in the old Microsoft Exchange account by adding forwarding to the new GNU/Linux account in Microsoft Active Directory. Remember to fix mailing-lists also.

Microsoft Exchange supports email retrieval using the Internet Message Access Protocol (IMAP). A special folder called INBOX exists in your account on the Microsoft Exchange server. Subfolders of INBOX use the forward slash character - / - as delimiter in text mode, but we will check to be sure.

telnet yourexchangeserver.com imap
A LOGIN username password
A LIST "" *
A LOGOUT

A list of all folders on the Microsoft Exchange server flies by, something like this:

* LIST (\HasChildren) "/" INBOX
* LIST (\HasChildren) "/" INBOX/mailing-lists
* LIST (\Marked \HasNoChildren) "/" INBOX/mailing-lists/openSuSE

INBOX has children, meaning a subfolder like mailing-lists. mailing-lists has children too, the subfolder openSuSE. There are no subfolders in openSuSE, so it says \HasNoChildren. The character in the "", here /, shows the delimiting character is the forward slash.

Mark everything and copy/paste into a text file called folders and save it.

IMPORTANT: CHECK THE FILE'S CONTENTS! Here be dragons, or more mundane things that might trip up copying. You can type

cat folders

to see the file's contents. You only want to keep the folder names. Folders with spaces in their names will appear quoted, while folders without spaces will not be quoted. Like this:

"Junk E-mail"
INBOX/mailing-lists

If you are migrating a lot of folders for many users, you probably want a good regular expression to remove everything but the folder names. If you are moving just your own folders, you probably can get by with a text editor. You must end up with a file where each folder is on it's own line, and keep the quotes if there are spaces in the folder name. To follow this example, call the cleaned up file folders_cleaned.

You need fetchmail and maildrop installed. Procmail can be used as a substitute for maildrop. This will not be covered here.

Create or edit a file called .fetchmailrc in your home folder, make it look like this:

# See man fetchmail for options
poll    yourexchangeserver.com
               proto imap
               user your_username_on_the_Microsoft_Exchange_server_goes_here
               pass your_password_goes_here
               mda     /usr/bin/maildrop

Use maildirmake to create a subfolder for mail from the Microsoft Exchange account, like this:

maildirmake -f Exchange Maildir

Create or edit a file called .mailfilter in your home folder, make it look like this:

MAIL=$HOME/Maildir/.Exchange/

Maildrop will now send all mail into this folder, so it might be a good idea ask your administrator to hold all new mail or do this at a time of day when you don't get a lot of mail.

Now, do a dry run to check for problems

for x in `cat folders_cleaned`; do; fetchmail -c -v -r $x; done

If all goes well, you can start copying by changing the above line to

for x in `cat folders_cleaned`; do; fetchmail -a -k -r $x; done

WARNING: DO NOT FORGET THE -k OPTION! This keeps the messages on the Microsoft Exchange server.

When the copying has completed, clean up by changing .mailfilter back to

MAIL=$HOME/Maildir/