在PHP中使用IMAP()获取最近的未读电子邮件计数 [英] Using IMAP () in PHP to get recent unread emails count
问题描述
我试图得到拒绝.来自gmail帐户的最新未读邮件.为此,我在Ubuntu系统中安装了IMAP,并尝试了一些PHP iMAP函数. 这是我到目前为止尝试过的.
/* 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());
现在我要说明我的所有尝试. 注意:我已经尝试过通过将新邮件发送到测试电子邮件ID来尝试每次尝试
Attempt_1:使用imap_search()
$recent_emails = imap_search($inbox,'RECENT');
if ($recent_emails)
echo count($recent_emails);
else
echo "false return";
imap_close($inbox);
现在Attempt_1的输出为"false return";
Attempt_2:使用imap_mailboxmsginfo()
$check = imap_mailboxmsginfo($inbox);
if ($check)
echo "Recent: " . $check->Recent . "<br />\n" ;
else
echo "imap_check() failed: " . imap_last_error() . "<br />\n";
imap_close($inbox);
当我向该ID发送了2封新邮件时,输出为最近:0"
Attempt_3:使用imap_status()
$status = imap_status($inbox, $hostname, SA_ALL);
if ($status)
echo "Recent: " . $status->recent . "<br />\n";
else
echo "imap_status failed: " . imap_last_error() . "\n";
//最近输出:0
Attempt_4:再次将imap_search()与参数NEW配合使用
$recent_emails = imap_search($inbox,'NEW');
if ($recent_emails)
echo count($recent_emails);
else
echo "false return";
imap_close($inbox);
输出-错误的返回
那我错在哪里? 如何获得最近的未读电子邮件计数?
此功能似乎有效:
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;
}
用法:
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'user@gmail.com';
$password = 'user_password';
$count = CountUnreadMail($hostname, $username, $password);
我无法为此功能索取全部功劳.它是对 PHP显示未读邮件数的回答比较呆滞的略加修改的版本.他的版本假定使用POP邮件.此版本需要完整的$hostname
.我用自己的gmail帐户进行了测试,并正确报告了收件箱中的未读邮件数量.
PHP显示未读邮件计数具有一些相当不错的阅读材料.签出来.
希望这会有所帮助.
更新
发件人: Gmail是否支持所有IMAP功能?
Gmail IMAP1是IMAP的相当完整的实现,但是 当前不支持以下功能:
\Recent flags on messages.
经过验证: Gmail的Buggy IMAP实现
Gmail不处理标准的IMAP标志,例如"\ Deleted", "\ Answered"和"\ Recent".
另请参见: Jyoti Ranjan的答案(如下),以寻求可能的解决方案.>
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 . "<br />\n" ;
else
echo "imap_check() failed: " . imap_last_error() . "<br />\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 . "<br />\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?
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.
这篇关于在PHP中使用IMAP()获取最近的未读电子邮件计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!