How to Read Only limited E-Mails by IMAP in PHP?

1.9k Views Asked by At

I use IMAP to read mails from my mail server.

But, I have huge number of mails in my Inbox, every time i try to test, it take a minutes to load.

I only want New, Unread, First 10 E-mails only.

For Reading Email :

 // open IMAP connection
    $dns = "{imap.smtp.domain:993/imap/ssl}INBOX";
    $email = "[email protected]";
    $password = "**********";


    $mbox = imap_open($dns, $email, $password);
    $MC = imap_check($mbox);
    if (!$mbox)
        die("COULD NOT OPEN MAILBOX!\r\n");
    $result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);

    echo "<table>";
    $i=1;
    foreach ($result as $overview) {
    if($i == 10) break;
        echo "<tr>"
            ."<td>".$overview->msgno."</td>"
            ."<td>".$overview->uid."</td>"
            ."<td>".$overview->date."</td>"
            ."<td>".$overview->udate."</td>"
            ."<td>".$overview->from."</td>"
            ."<td>".$overview->to."</td>"
            ."<td>".$overview->size."</td>"
            ."<td>".$overview->subject."</td>"
            ."</tr>";
    $i++;
    }
    echo "</table>";

It returns only 10, but it takes huge time.

I need simple and quick read of email.

Is it Possible ?

Or any other solutions ?

1

There are 1 best solutions below

3
On BEST ANSWER
// this will select top 10 emails
$result = imap_fetch_overview($mbox,"1:10",0);
//for recent emals 
$mailbox = imap_search($mbox,'RECENT');
// implode gives you id fo the messages
messages = implode(",", $mailbox);
// list of recent emails and you can pass your message ids in string with comma seperated values like(1,2,5,6) in imap_fetch_overview as below 
$messages = imap_fetch_overview($mbox,"$messages",0);
// for unseen 
$mailbox = imap_search($mbox,'UNSEEN');