Correcting Message Order with Courier
Recently I moved some local messages from my machine (previously downloaded with pop3) onto the server so I could use imap. I used Mail.app 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:
1324697191.M91227P15574V000000000000CA00I0004B07D_556.hostname,S=5622:2,S 1324697192.M322096P15574V000000000000CA00I0004B07F_557.hostname,S=225691:2,RS 1324697196.M144018P15574V000000000000CA00I0004B081_558.hostname,S=7702:2,RS 1324697197.M715598P15574V000000000000CA00I0004B083_559.hostname,S=15741:2,S 1324697199.M327587P15574V000000000000CA00I0004B085_560.hostname,S=8744:2,RS
The received times of these messages, in the same order as listed above, are:
01/15/2010 01/09/2009 07/13/2010 02/21/2010 05/06/2010
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 rename.pl cur/*
When you're convinced it will do the right thing:
perl rename.pl cur/* --output-dir cur_renamed
After ensuring the rename was correct, I swapped the
cur_renamed directories, deleted the
courierimapuiddb file, and restarted courier-imap. I also had to quit Mail.app 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.