Asked  2 Years ago    Answers:  5   Viewed   151 times

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 "Connectedn"; 
} 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


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.



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 (


Monday, September 19, 2022

$mb = imap_open("{host:port/imap}","username", "password" );

$messageCount = imap_num_msg($mb);
for( $MID = 1; $MID <= $messageCount; $MID++ )
   $EmailHeaders = imap_headerinfo( $mb, $MID );
   $Body = imap_fetchbody( $mb, $MID, 1 );
   doSomething( $EmailHeaders, $Body );
Saturday, December 17, 2022

I'd suggest 4'th:

Once new message has been sent to a user, you update counter in memcache. You create simple ajax application on client side sending a request every X seconds. At server side, you just check is there unread messages. At page refresh, you don't need to query the database since you get count from memcache extremely fast.

That's what I'd done if I had bottleneck in DB (in 90% cases, DB is the weekest part of any database-driven application).

That's what we usually do at highloaded web sites: we trying to avoid any COUNTER queries. If not, we denormalize the database to store counters right in the appropriate table as yet another column e.g. if you can not use memcache, you would store the unread messages counter as a column for Users table.

Monday, August 22, 2022

Unfortunately, e-mail clients can essentially do whatever they want with your message, and there is no reliable standard for determining how a received message originated at the client. In addition, IMAP doesn't really have anything to do with it. E-mails can be sent a number of different ways, including webmail.

The best you can do is look for an ID number in the subject line (assuming folks don't change it, which they rarely do). You can also do what Google does... fuzzy match the reply text to e-mail you sent to that address. If it matches, consider it part of the reply. This takes great effort though.

Tuesday, December 13, 2022

This bug was related to configuration mbstring.func_overload in php.ini. You can use workaround to avoid this bug - just add at begin of your script:

if (function_exists('mb_internal_encoding')) {

and at the end:

if (function_exists('mb_internal_encoding'))

It helped me, so, I believe it'll help you as well.

Friday, November 25, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages