Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

email - PHP Displaying unread mail count

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
"; 
} else { exit ("Can't connect: " . imap_last_error() ."
");  echo "FAIL!
";  }; 

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...