Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3228270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:40:21+00:00 2026-05-17T16:40:21+00:00

I’m using PHP’s built in IMAP functions to build a bare-bones webmail client (will

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:40:22+00:00Added an answer on May 17, 2026 at 4:40 pm

    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;
    

    ….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.