可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to get no. of recent unread mails from a gmail account.For this I have installed IMAP in my Ubuntu system and tried some PHP iMAP functions. Here are what i have tried till now.
/* connect to gmail */ $hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; $username = 'user@gmail.com'; $password = 'user_password'; /* try to connect */ $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
Now I am stating all my attempts. NB - I have tried each attempt by sending new mails to the testing email id
Attempt_1: Using imap_search()
$recent_emails = imap_search($inbox,'RECENT'); if ($recent_emails) echo count($recent_emails); else echo "false return"; imap_close($inbox);
Now Output of Attempt_1 is "false return";
Attempt_2: Using imap_mailboxmsginfo()
$check = imap_mailboxmsginfo($inbox); if ($check) echo "Recent: " . $check->Recent . "
\n" ; else echo "imap_check() failed: " . imap_last_error() . "
\n"; imap_close($inbox);
Here the output is Recent:0 while I have sent 2 new mails to this id
Attempt_3: using imap_status()
$status = imap_status($inbox, $hostname, SA_ALL); if ($status) echo "Recent: " . $status->recent . "
\n"; else echo "imap_status failed: " . imap_last_error() . "\n";
//Output Recent:0
Attempt_4: Using Using imap_search() Again with parameter NEW
$recent_emails = imap_search($inbox,'NEW'); if ($recent_emails) echo count($recent_emails); else echo "false return"; imap_close($inbox);
Output - false return
So Where Am I WRONG? How can I get the recent unread emails count?
回答1:
This function seems to work:
function CountUnreadMail($host, $login, $passwd) { $mbox = imap_open($host, $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; }
Usage:
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; $username = 'user@gmail.com'; $password = 'user_password'; $count = CountUnreadMail($hostname, $username, $password);
I can’t claim full credit for this function. It’s a slightly edited version of sdolgy’s answer to PHP Displaying unread mail count. His version assumed POP mail. This version requires the full $hostname
. I tested it with my own gmail account and it correctly reported the number of unread messages I had in my inbox.
PHP Displaying unread mail count has some pretty good reading material. Check it out.
Hope this helps.
UPDATE
From: Does Gmail support all IMAP features?
Gmail IMAP1 is a fairly complete implementation of IMAP, but the following features are currently unsupported:
\Recent flags on messages.
Verfied at: Gmail's Buggy IMAP Implementation
Gmail doesn't handle standard IMAP flags, such as "\Deleted", "\Answered", and "\Recent".
See also: Jyoti Ranjan's answer (below) for a possible solution.
回答2:
I have solved it using database,This is how I have done it.
1.I made a column in users table, for ex- 'email_max_uid' INT(11) NOT NULL default 0
2.When a user loads this page first, using my sql query I retrieved the value of 'email_max_uid' of that particular users 3.connect to his gmail account automatically and got the unread mails and total mails
$inbox = imap_open('{imap.gmail.com:993/imap/ssl}INBOX','user's email id','user's password') or die('Cannot connect to Gmail: ' . imap_last_error()); $unread_emails = imap_search($inbox,'UNSEEN'); $emails = imap_search($inbox