PHP imap_search 未检测到 gmail 收件箱中的所有邮件

发布于 2024-10-10 07:06:55 字数 877 浏览 0 评论 0原文

当我在 GMail 收件箱上运行非常简单的 imap_search 时,搜索返回的邮件少于应有的邮件。

这是任何拥有 GMail 帐户的人都可以运行的脚本。

$host = '{imap.gmail.com:993/imap/ssl}';
$user = 'foo';
$pass = 'bar';

$imapStream = imap_open($host,$user,$pass) or die(imap_last_error());

$messages = imap_search($imapStream,"ALL");

echo count($messages);

imap_close($imapStream);

这将返回 39 条消息。但是,我的收件箱中有 100 条消息,其中一些是捆绑在对话中的,一些是从另一个帐户 (SquirrelMail) 转发的。

任何人都可以重复这些结果,和/或告诉我发生了什么事吗?


我尝试过的其他服务器字符串都返回相同的结果:

{imap.gmail.com:993/imap/ssl/novalidate-cert}
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX
{imap.gmail.com:993/imap/ssl}INBOX

GMail 的 IMAP 功能支持:http://mail.google.com/support/bin/answer.py?hl=zh-CN&answer=78761

When I run a very simple imap_search on my GMail inbox, the search returns less messages than it should.

Here is the script that anyone with a GMail account can run.

$host = '{imap.gmail.com:993/imap/ssl}';
$user = 'foo';
$pass = 'bar';

$imapStream = imap_open($host,$user,$pass) or die(imap_last_error());

$messages = imap_search($imapStream,"ALL");

echo count($messages);

imap_close($imapStream);

This returns 39 messages. But, I've got 100 messages in my inbox, some bundled in conversations, some forwarded from another account (SquirrelMail).

Can anyone duplicate these results, and/or tell me what's going on?


Other server strings I've tried, all returning the same results:

{imap.gmail.com:993/imap/ssl/novalidate-cert}
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX
{imap.gmail.com:993/imap/ssl}INBOX

GMail's IMAP feature support: http://mail.google.com/support/bin/answer.py?hl=en&answer=78761

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名。 2024-10-17 07:06:55

在严重脱发之后,我找到了答案。这是一个误导性的用户界面。

默认情况下,GMail 将邮件分组为“对话”。这些对话可以包括存档的消息。

例如,Bob 的收件箱看起来有 4 个对话,共 25 条消息,显然应该返回 100 条收件箱消息。实际上,其中 60 封邮件位于存档中(而不是收件箱),因此 imap_search() 返回 40。这些消息会神奇地从存档中提取出来并放入收件箱对话中。

在“设置”->“常规”菜单中,您可以切换对话视图,这会将所有那些顽皮的存档邮件放回到它们所属的位置,并显示您真正的收件箱视图。

After significant hair loss, I've found the answer. It was a misleading UI.

GMail groups one's messages into "Conversations" by default. These conversations can include archived messages.

So, for example, Bob's inbox looks like there's 4 conversations of 25 messages, which should apparently return 100 inbox messages. In reality, 60 of the messages are in the archive (not the inbox), so the imap_search() returns 40. These messages are magically pulled out of the archive and placed into inbox conversations.

In the Settings->General menu, you can toggle conversation view, which will put all of those naughty archived messages back where they belong, and show your true inbox view.

离旧人 2024-10-17 07:06:55

imap_search criteria ALL - 返回与其余条件匹配的所有消息,所以我问你其余条件在哪里?

您可以使用 imap_sort($imapStream, 'SORTDATE', 0); ( imap_sort - 按给定参数获取消息编号并对其进行排序imap_sort ) 。


编辑,这里有一些代码可以遍历您收件箱中的所有邮件,而不是 imap_num_msg ,您可以使用前面所述的 imap_sort ,这样您就可以根据需要对收件箱进行排序。

<?php
    $imap = imap_open("{mail.yourserver.com:143}INBOX", "username", "password");
    $message_count = imap_num_msg($imap);

    for ($i = 1; $i <= $message_count; ++$i) {
        $header = imap_header($imap, $i);
        $body = trim(substr(imap_body($imap, $i), 0, 100));
        $prettydate = date("jS F Y", $header->udate);

        if (isset($header->from[0]->personal)) {
            $personal = $header->from[0]->personal;
        } else {
            $personal = $header->from[0]->mailbox;
        }

        $email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
        echo "On $prettydate, $email said \"$body\".\n";
    }

    imap_close($imap);
?>

imap_search criteria ALL - return all messages matching the rest of the criteria , so i ask you where is the rest of the criteria ?

You could use imap_sort($imapStream, 'SORTDATE', 0); ( imap_sort - Gets and sorts message numbers by the given parameters imap_sort ) .


Edit , here is some code that goes thru all messages in you're inbox , instead of imap_num_msg , you could use imap_sort as stated before , so you get you're inbox sorted if you like .

<?php
    $imap = imap_open("{mail.yourserver.com:143}INBOX", "username", "password");
    $message_count = imap_num_msg($imap);

    for ($i = 1; $i <= $message_count; ++$i) {
        $header = imap_header($imap, $i);
        $body = trim(substr(imap_body($imap, $i), 0, 100));
        $prettydate = date("jS F Y", $header->udate);

        if (isset($header->from[0]->personal)) {
            $personal = $header->from[0]->personal;
        } else {
            $personal = $header->from[0]->mailbox;
        }

        $email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
        echo "On $prettydate, $email said \"$body\".\n";
    }

    imap_close($imap);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文