PHP 显示未读邮件数

发布于 2024-11-23 20:12:05 字数 596 浏览 1 评论 0 原文

我正在使用 php imap 类。 我的邮箱里有很多邮件,但使用这个脚本我只能检索未读的邮件。 我该怎么做呢?

if ($mbox=imap_open( "{" . $mailserver . ":" . $port . "}INBOX", $user, $pass )) 
{
  echo "Connected\n"; 
} else { exit ("Can't connect: " . imap_last_error() ."\n");  echo "FAIL!\n";  }; 

if ($hdr = imap_check($mbox)) {
  $msgCount = $hdr->Nmsgs;
  echo "Ci sono ".$msgCount." mail";
} else {
  echo "Failed to get mail";
}

如果我将

$overview=imap_fetch_overview($mbox,"1:$msgCount",0);

脚本加载到无限时间。

imap_search UNSEEN 解决方案不好,因为 pop3 不使用此标志。 那我该怎么办???? 多谢。

I am using php imap class.
In my box I have a lot of mail, but with this script I would retrieve only the unreaded mail.
How can I do it?

if ($mbox=imap_open( "{" . $mailserver . ":" . $port . "}INBOX", $user, $pass )) 
{
  echo "Connected\n"; 
} else { exit ("Can't connect: " . imap_last_error() ."\n");  echo "FAIL!\n";  }; 

if ($hdr = imap_check($mbox)) {
  $msgCount = $hdr->Nmsgs;
  echo "Ci sono ".$msgCount." mail";
} else {
  echo "Failed to get mail";
}

If I do

$overview=imap_fetch_overview($mbox,"1:$msgCount",0);

the script load to an infinity time.

The imap_search UNSEEN solution is not good because pop3 don't use this flag.
So how can I do??????
Thanks a lot.

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

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

发布评论

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

评论(2

萌能量女王 2024-11-30 20:12:05

您可以采用两种方法:

1.循环浏览消息

$count = imap_num_msg($connection);
for($msgno = 1; $msgno <= $count; $msgno++) {

    $headers = imap_headerinfo($connection, $msgno);
    if($headers->Unseen == 'U') {
       ... do something ... 
    }

}

2.使用 imap_search

有一个名为 UNSEEN 的标志,您可以使用它来搜索未读电子邮件。您可以使用 UNSEEN 标志调用 imap_search 函数,如下所示:

$result = imap_search($connection, 'UNSEEN');

如果您需要将其与更多搜索标志结合起来,例如搜索来自 [电子邮件受保护],您可以执行以下操作

$result = imap_search($connection, 'UNSEEN FROM "[email protected]"');

:可用标志列表,请参阅 PHP 网站 (www.php.net/imap_search) 上 imap_search 手册页的条件部分

来源:http://www.electrictoolbox.com/php-imap-unread-messages/

There is two way you can follow:

1. Looping through the messages

$count = imap_num_msg($connection);
for($msgno = 1; $msgno <= $count; $msgno++) {

    $headers = imap_headerinfo($connection, $msgno);
    if($headers->Unseen == 'U') {
       ... do something ... 
    }

}

2. Using imap_search

There's a flag called UNSEEN which you can use to search for the unread emails. You would call the imap_search function with the UNSEEN flag like so:

$result = imap_search($connection, 'UNSEEN');

If you need to combine this with more search flags, for example searching for messages from [email protected], you could do this:

$result = imap_search($connection, 'UNSEEN FROM "[email protected]"');

For a complete list of the available flags, refer to the criteria section of the imap_search manual page on the PHP website (www.php.net/imap_search)

Source: http://www.electrictoolbox.com/php-imap-unread-messages/

狼性发作 2024-11-30 20:12:05

这是 Google 上的一个难题:php imap unread

第一个结果:

有一个名为 UNSEEN 的标志,您可以使用它来搜索未读电子邮件。您可以使用 UNSEEN 标志调用 imap_search 函数,如下所示:
查看源代码?

 $result = imap_search($connection, 'UNSEEN');

如果您需要将其与更多搜索标志结合起来,例如搜索来自 [电子邮件受保护],您可以这样做:
查看源代码?

 $result = imap_search($connection, 'UNSEEN FROM "[email protected]"');

有关可用标志的完整列表,请参阅 PHP 网站 (www.php.net/imap_search) 上 imap_search 手册页的条件部分

编辑 我最初将其作为 IMAP 阅读。 #失败。

Google:php pop3 未读

第二个链接:

 function CountUnreadMails($host, $login, $passwd) {
      $mbox = imap_open("{{$host}/pop3:110}", $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;
 }

This was a tough one on Google: php imap unread

The first result:

There's a flag called UNSEEN which you can use to search for the unread emails. You would call the imap_search function with the UNSEEN flag like so:
view sourceprint?

 $result = imap_search($connection, 'UNSEEN');

If you need to combine this with more search flags, for example searching for messages from [email protected], you could do this:
view sourceprint?

 $result = imap_search($connection, 'UNSEEN FROM "[email protected]"');

For a complete list of the available flags, refer to the criteria section of the imap_search manual page on the PHP website (www.php.net/imap_search)

Edit I had read this originally as IMAP. #fail.

Google: php pop3 unread

2nd link:

 function CountUnreadMails($host, $login, $passwd) {
      $mbox = imap_open("{{$host}/pop3:110}", $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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文