douke6881 2010-06-02 14:46
浏览 55

PHP System_Daemon和IMAP连接的问题

I'm trying to create a PHP daemon that connects to an IMAP server and processes emails as they come in. I have it close to working, but the daemon keeps grabbing the original emails that it finds the first time the daemon is loaded. I believe the reason is because I'm opening the IMAP connection in the parent process. Example below:

if ($imapConnection=imap_open($authhost,$user,$pass) or die())
{
  //start daemon
  while()
  {
    //Grab email headers 
    $imapHeaders = imap_headers($imapConnection);
    $count = sizeof($imapHeaders)

    //loop the emails
    for($i = 1; $i <= $count, $i++)
    {
      //process the email
      //delete the email
    }

    System_Daemon::iterate(15);
  }
}   
imap_close($imapConnection);

I'd like to stay away from putting the IMAP connection within the loop. How can I keep the connection to the IMAP server outside of the loop and still get new emails?

  • 写回答

1条回答 默认 最新

  • doujia2090 2010-06-02 15:03
    关注

    In IMAP, mails stay on the server. So each time you come, if you have not explicitly removed them, old emails are still there. To prevent processing these emails, you could have a var that keeps the amount of mails you treated before, so you could go from $i = 0 (supposed the last arrived) to $i < $var where $var stands for the number of mails already treated.

    EDIT :

    Since you delete the mail by imap_delete, do an imap_expunge at each loop.

    EDIT 2 :

    Use imap_reopen, I tried you script on my server using imap_reopen($imapConnection, "{domain.tld}INBOX"); after each loop and it sees the new mail now. Does not do a new authentication, just move your stream.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算