title: Retrieving Email with PHP in Batches
body: <p>I am trying to fetch emails through my mailbox using IMAP class of PHP. Because, the number of messages are in thousands the script is taking too long to execute.</p>
  
  <p>Is there a way I can retrieve messages in batches of say 100 messages each.</p>
  
  <p>Also, how can I uniquely Identify the message. is it <strong>*message_id*</strong> or <strong><em>UID</em></strong> ? in <strong>imap_fetch_overview()</strong></p>
  
  <pre><code>/* grab emails */
  $emails = imap_search($inbox,'ALL');
  
  /* if emails are returned, cycle through each... */
  if($emails) {
  
      /* begin output var */
      $output = '';
  
      /* put the newest emails on top */
      rsort($emails);
  
      /* for every email... */
      foreach($emails as $email_number) {
  
          /* get information specific to this email */
          $overview = imap_fetch_overview($inbox,$email_number,0);
          $message = imap_fetchbody($inbox,$email_number,2);
  
          /* output the email header information */
          $output.= '&lt;div class="toggler '.($overview[0]-&gt;seen ? 'read' : 'unread').'"&gt;';
          $output.= '&lt;span class="subject"&gt;'.$overview[0]-&gt;subject.'&lt;/span&gt; ';
          $output.= '&lt;span class="from"&gt;'.$overview[0]-&gt;from.'&lt;/span&gt;';
          $output.= '&lt;span class="date"&gt;on '.$overview[0]-&gt;date.'&lt;/span&gt;';
          $output.= '&lt;/div&gt;';
  
          /* output the email body */
          $output.= '&lt;div class="body"&gt;'.$message.'&lt;/div&gt;';
      }
  
      echo $output;
  } 
  </code></pre>