title: php imap retrieve only the oldest email
body: <p>Goodmorning,</p>
  
  <p>I'm trying to retrieve only the oldest email that matches my imap_search.
  So I need to get rid of the foreach loop.
  I've already tried just removing it, but that didn't work.</p>
  
  <p>code:</p>
  
  <pre><code>&lt;?php
  /* connect to gmail */
  $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
  $username = '**';
  $password = '**';
  
  /* try to connect */
  $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
  
  /* grab emails, voeg hier extra search opties in bijv: SUBJECT "wat er in het onderwerp moet staan" From "email.van.herkomst@example.com" */
  $emails = imap_search($inbox,'UNSEEN SUBJECT "Task has succeeded"');
  
  /* if emails are returned, cycle through each... */
  if($emails) {
  
      /* begin output var */
      $output = '';
  
      /* for every email... */
      if($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;
  }
  
  /* close the connection */
  imap_close($inbox);
  ?&gt;
  </code></pre>
  
  <p>Kind regards,
  Lars Kaptein</p>