html - PHP - imap count unseen emails gives always '1' as result -


trying count unseen emails in email box, script have counting however, when there no unseen emails result 1 , no 0. idea why?

here code have far.

php:

    $hostname = '{imap.example.com:993/imap/ssl}inbox';     $username = 'myemail@example.co.uk';     $password = 'mypass';     $imap = imap_open($hostname, $username, $password) or die("imap connection error");     $result = imap_search($imap, 'unseen');     $new_inbox_msg = count($result);     echo $new_inbox_msg 

imap_search() returns array, not number, according documentation.

so instead need:

$result = imap_search($imap, 'unseen'); echo count($result); 

ok, sorry, miss interpreted documentation myself. here explanation of issue: function indeed return array, but array holding 1 result (count) per search attribute handed over. since specify single attribute ('unseen') always single element in array. that elements value number of messages matching search criteria.

therefore correct usage should be:

$result = imap_search($imap, 'unseen'); if (is_array($result) && isset($result[0])) {     echo count($result[0]); } else {     echo "failed query number of messages\n"; } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -