I'm using PHP's built in IMAP functions to build a bare-bones webmail client (will be primarily used for accessing gmail accounts). I've currently hit a road block in the mailbox list views, where I am displaying a paginated list of messages in that mailbox sorted by date (ascending or descending).
My initial implementation retrieved ALL messages from the mailbox with imap_headers() and sorted that array based on the date, then returned the segment of the array that corresponded to the current page of list the user wanted to view. This worked ok for mailboxes with a small number of messages but the performance decreased greatly as the size of the mailbox grew (for a mailbox with ~600 messages, the execution time was on average around 10 seconds). And for some of the users of this client, 600 messages is a small number for a mailbox, with some easily between 5 and 10 thousand messages in their inbox.
So my second stab at the problem was to instead of retrieving the headers for all messages in the mailbox, I got the total number of messages with imap_num_msg() and using that number i constructed a for loop, where the loop counter is used as the message number. and for each iteration I call imap_headerinfo() with that message number.
This works better, however I was under the impression that the message number corresponded directly to when that message was recieved, so message no. 1 was the oldest message, and the number returned by imap_num_msg() was the message number of the newest message. So using that I could still provide sorting by date within my paginated list. But after testing it seems that the message number does not correspond to the date recieved, and really I have no clue how they are assigned.
Any help or direction would be greatly appreciated.