Recently I moved some local messages from my machine (previously downloaded with pop3) onto the server so I could use imap. I used on Mac OS X 10.5.8 to do this.

For some reason, the messages in this example were uploaded in a way that caused them to be loaded in the wrong order on iOS 5.0.1. Since I run the mailserver myself, I took a look at the message files to see if I could deduce the cause of the issue. I noticed a series of messages on the mail server that looked like this:


The received times of these messages, in the same order as listed above, are:


It turns out that due to the messages in this example having a modification date that doesn't match their receive timestamp the messages appear in the wrong order on an iOS device, where the mail application assumes the order of the messages it gets from the imap server is already sorted by date. This isn't a problem with desktop mail clients -- they simply fetch all the messages, sort them, and everything is fine. Several google searches on this subject yielded threads where people were trying to get their messages in the correct order and the suggested remedy was "sort them on the client side."

To fix this, I wrote a perl script script to read the message headers, determine the receive time, and update the file name and modification date to match.

Use the script like this:

perl cur/*

When you're convinced it will do the right thing:

perl cur/* --output-dir cur_renamed

After ensuring the rename was correct, I swapped the cur and cur_renamed directories, deleted the courierimapuiddb file, and restarted courier-imap. I also had to quit on OS X and delete and re-add the mail account on the iOS device to get the messages to come in in the correct order.