joe-jeff
joe-jeff

Reputation: 336

Imap_fetch_overview and newest / oldest mail

I am struggling with sequence for imap_fetch_overview. Is the mail 1 the oldest or newest mail? If a new mail arrives does it get a higher id or not?

Solution:

   $check = imap_check($connection);
   $result = array();   

   if($check->Nmsgs > 0){

        $sort = imap_sort($connection, SORTARRIVAL, 0);

        $msgs = '';

        $i = 1;

        foreach($sort as $id)
        {
            if($i > 200) break;
            $msgs = $msgs . $id . ',';
            $i++;
        }

        $response = imap_fetch_overview($connection, rtrim($msgs,','));
        foreach ($response as $msg){
            $result[] = $msg;
        } 
    }

Upvotes: 1

Views: 1954

Answers (1)

Masum Nishat
Masum Nishat

Reputation: 167

For the second part of the question,

see this RFC .

Each mail will have different ID but there is no higher or lower. It is just rendomly selected but unique.

For the first part of your question,

If you want to find oldest or newest mail, you have to use date method like:

$overview = imap_fetch_overview($connection,$email_number,0);
echo $overview[0]->date;

then compare this mails using returned dates.

Upvotes: 1

Related Questions