如何获取Gmail未读数量

时间:2011-08-09 21:19:46

标签: php gmail imap gmail-imap

我使用以下代码来获取Gmail中未读电子邮件的数量。但是,它返回错误:

can't connect: Too many login failures

这里有什么我想念的吗?

(我正在测试的Gmail帐户中启用了IMAP和POP。)


注意:看起来它正在运行(至少对于大多数请求而言)。然而,它花了太长时间 - 也许2到3分钟回来一个数字。有没有办法加快速度?


谢谢!

<?php

$mbox = imap_open ("{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox", 
"username", "password", OP_READONLY) 
or die("can't connect: " . imap_last_error()); 
$check = imap_mailboxmsginfo($mbox); 
if ($check) { 
print $check->Unread; //. "/" . $check->Nmsgs; 
} else { 
print "Failed"; 
}

?>

4 个答案:

答案 0 :(得分:3)

您还可以使用Gmail Inbox Feed获取未读数。 只需向https://mail.google.com/mail/feed/atom发送经过身份验证的GET请求,然后检查fullcount元素的值。

答案 1 :(得分:0)

尝试输出可能遇到的所有错误:

$mbox = imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox", "username", "password", OP_READONLY) or die('Cannot connect to Gmail: ' . print_r(imap_errors()));

答案 2 :(得分:0)

我有同样的问题而且非常简单。

使用您用于imap连接的帐户登录,并在页面顶部谷歌改变您关于多帐户访问您的帐户的权利,以便Goolge能够完成该流程并使其能够使用您的帐户

您的问题将得到解决。

答案 3 :(得分:0)

最简单的方法是向gmail api发出经过身份验证的GET请求。 url :: https://www.googleapis.com/gmail/v1/users/me/labels/UNREAD

它将返回一个带有未读消息数n个线程的json。 countUnread = response [&#34; messagesTotal&#34;]。 有关更多详细信息,请参阅oauth 2 playground。 https://developers.google.com/oauthplayground/?code=4/-49VJwh28-eJG7xiK3UoFBchIQrCYRllnOt1TY-w0h4#

相关问题