如何通过IMAP php在特定日期和时间之后获取邮件

时间:2014-10-08 05:27:05

标签: php imap

我正在编写IMAP邮件提取功能。想要展示

`$now = time(); // current time

$mailbox = '{192.168.150.11:143/imap/novalidate-cert}'; 
$mbox = imap_open($mailbox, 'username', 'password'); // log in to mail server

if (!$mbox)
echo ('Failed opening mailbox<br>' . print_r(imap_errors(), true)); // remove the  print_r for production use
else
{
$box = imap_check($mbox); // get the inbox

for ($imap_idx = 1; $imap_idx <= $box->Nmsgs; $imap_idx++) // loop through the messages
{
$headers = imap_headerinfo($mbox, $imap_idx); // 
$raw_headers = imap_fetchheader($mbox, $imap_idx); //
$selected_headers = '';
$text_part = '';
$html_part = '';
$original_message = imap_body($mbox, $imap_idx); // save the copy of the entire thing, attachments and all

// build selected headers string
for ($ii = 0; $ii < count($headers->from); $ii++)
  $selected_headers .= 'From: ' . $headers->from[$ii]->mailbox . '@' . $headers->from[$ii]->host . "\n";
for ($ii = 0; $ii < count($headers->to); $ii++)
  $selected_headers .= 'To: ' . $headers->to[$ii]->mailbox . '@' . $headers->to[$ii]->host . "\n";
for ($ii = 0; $ii < count($headers->cc); $ii++)
  $selected_headers .= 'Cc: ' . $headers->cc[$ii]->mailbox . '@' . $headers->cc[$ii]->host . "\n";
for ($ii = 0; $ii < count($headers->bcc); $ii++)
  $selected_headers .= 'Bcc: ' . $headers->bcc[$ii]->mailbox . '@' . $headers->bcc[$ii]->host . "\n";
if (!empty($headers->date))
  $selected_headers .= 'Date: ' . $headers->date . "\n";
if (!empty($headers->subject))
  $selected_headers .= 'Subject: ' . $headers->subject . "\n";

现在我的问题是我需要在特定日期和时间之后收到邮件。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我认为这里存在语言障碍所以我会尝试两次回答你,提出两个明确的问题并回答它们 - 这有望解决你的问题。

  1. &#34;我希望我的PHP脚本能够在固定日期检查新电子邮件 - 即2015年7月5日和#34;
  2. 为此,您需要使用UNIX时间戳。这是一个自1970年以来每秒增加一次的数字。这意味着自1970年以来已经过了1412746422秒,从中可以推断出日期和时间。我将在PHP脚本中将您转到read up on how to implement this - 这就是日期函数的工作方式。

    1. &#34;我希望我的PHP脚本定期检查新电子邮件 - 即每小时一次&#34;。
    2. 实现此目的最实用的方法是使用CRON(如果您使用的是* nix服务器)。在Google中搜索CRON作业。简单地说,您将创建一个由您的服务器按您设置的时间间隔运行的PHP脚本(例如每小时) - 这不是在PHP脚本中定义的,而是在服务器上定义的。设置CRON有很多资源,我们不了解您的环境,因此我无法为您提供链接。

      将来我可能会建议花一点时间在您认为相关的答案中提供更多信息。也许如果英语不是你的第一语言,尝试用几种不同的方式写出这个概念 - 或者更好的是,提供你为什么需要某种方式以某种方式工作的原因,我们将能够提供帮助。在那。

相关问题