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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?