tags:

views:

54

answers:

1

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.

A: 

i have been playing with this and here is some snips of what im doing, pagination is working nice getting only a few mails per page. i wont paste all the code, just the main parts. hope this helps :)

// main method to get the mails __getFormattedBasics() just calls imap_hearderinfo() loop runs backwards by default to get newest first
private function __getMails($Model, $query) {
            $pagination = $this->_figurePagination($query);

            $mails = array();
            for ($i = $pagination['start']; $i > $pagination['end']; $i--) {
                $mails[] = $this->__getFormattedBasics($Model, $i);
            }

            unset($mail);

            return $mails;
        }

// this just uses the current page number, limit per page and figures the start/end for the loop above you can sort in the other direction passing asc/desc
protected function _figurePagination($query) {
            $count = $this->_mailCount($query); // total mails
            $pages = ceil($count / $query['limit']); // total pages
            $query['page'] = $query['page'] <= $pages ? $query['page'] : $pages; // dont let the page be more than available pages

            $return = array(
                'start' => $query['page'] == 1
                    ? $count    // start at the end
                    : ($pages - $query['page'] + 1) * $query['limit'], // start at the end - x pages
            );

            $return['end'] = $query['limit'] >= $count
                ? 0
                : $return['start'] - $query['limit'];

            $return['end'] = $return['end'] >= 0 ? $return['end'] : 0;

            if (isset($query['order']['date']) && $query['order']['date'] == 'asc') {
                return array(
                    'start' => $return['end'],
                    'end' => $return['start'],
                );
            }

            return $return;
        }

    private function __getFormattedBasics($Model, $message_id) {
        $mail = imap_headerinfo($this->MailServer, $message_id);
        $structure = imap_fetchstructure($this->MailServer, $mail->Msgno);

        $toName = isset($mail->to[0]->personal) ? $mail->to[0]->personal : $mail->to[0]->mailbox;
        $fromName = isset($mail->from[0]->personal) ? $mail->from[0]->personal : $mail->from[0]->mailbox;
        $replyToName = isset($mail->reply_to[0]->personal) ? $mail->reply_to[0]->personal : $mail->reply_to[0]->mailbox;

....

dogmatic69