PHP Displaying unread mail count

有些话、适合烂在心里 提交于 2019-11-27 20:57:36

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 me@example.com, you could do this:

$result = imap_search($connection, 'UNSEEN FROM "me@example.com"');

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/

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 me@example.com, you could do this: view sourceprint?

 $result = imap_search($connection, 'UNSEEN FROM "me@example.com"');

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;
 }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!