2

I am using php imap class. In my box I have a lot of mail, but with this script I would retrieve only the unreaded mail. How can I do it?

if ($mbox=imap_open( "{" . $mailserver . ":" . $port . "}INBOX", $user, $pass )) 
{
  echo "Connected\n"; 
} else { exit ("Can't connect: " . imap_last_error() ."\n");  echo "FAIL!\n";  }; 

if ($hdr = imap_check($mbox)) {
  $msgCount = $hdr->Nmsgs;
  echo "Ci sono ".$msgCount." mail";
} else {
  echo "Failed to get mail";
}

If I do

$overview=imap_fetch_overview($mbox,"1:$msgCount",0);

the script load to an infinity time.

The imap_search UNSEEN solution is not good because pop3 don't use this flag. So how can I do?????? Thanks a lot.

1

2 Answers 2

6

There is two way you can follow:

1. Looping through the messages

$count = imap_num_msg($connection);
for($msgno = 1; $msgno <= $count; $msgno++) {

    $headers = imap_headerinfo($connection, $msgno);
    if($headers->Unseen == 'U') {
       ... do something ... 
    }

}

2. Using imap_search

There's a flag called UNSEEN which you can use to search for the unread emails. You would call the imap_search function with the UNSEEN flag like so:

$result = imap_search($connection, 'UNSEEN');

If you need to combine this with more search flags, for example searching for messages from [email protected], you could do this:

$result = imap_search($connection, 'UNSEEN FROM "[email protected]"');

For a complete list of the available flags, refer to the criteria section of the imap_search manual page on the PHP website (www.php.net/imap_search)

Source: http://www.electrictoolbox.com/php-imap-unread-messages/

Sign up to request clarification or add additional context in comments.

2 Comments

But after imap_search how can I print the mails?
Because UNSEEN means UNREADED?
3

This was a tough one on Google: php imap unread

The first result:

There's a flag called UNSEEN which you can use to search for the unread emails. You would call the imap_search function with the UNSEEN flag like so: view sourceprint?

 $result = imap_search($connection, 'UNSEEN');

If you need to combine this with more search flags, for example searching for messages from [email protected], you could do this: view sourceprint?

 $result = imap_search($connection, 'UNSEEN FROM "[email protected]"');

For a complete list of the available flags, refer to the criteria section of the imap_search manual page on the PHP website (www.php.net/imap_search)

Edit I had read this originally as IMAP. #fail.

Google: php pop3 unread

2nd link:

 function CountUnreadMails($host, $login, $passwd) {
      $mbox = imap_open("{{$host}/pop3:110}", $login, $passwd);
      $count = 0;
      if (!$mbox) {
           echo "Error";
      } else {
           $headers = imap_headers($mbox);
           foreach ($headers as $mail) {
                $flags = substr($mail, 0, 4);
                $isunr = (strpos($flags, "U") !== false);
                if ($isunr)
                $count++;
           }
      }

 imap_close($mbox);
 return $count;
 }

4 Comments

No it is no a good solution....the script is still loading and the result is: Maximum execution time of 30 seconds exceeded
which solution? 1st? 2nd? both?
Ok after $result = imap_search($connection, 'UNSEEN'); how can I print the mails?
this question is about unread count : ) if you need help with reading / printing the emails, select an answer here and open a new question ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.