tags:

views:

47

answers:

1

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  A: 

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.

Serty Oan
I actually delete the emails within the loop after I'm done processing them. Do I still need to do what you are suggesting if I'm deleting the emails?
mike
Also, once the daemon is started, it does not appear to find new emails that came in afterwards. That is why I think it has something to do with having the connection outside of the loop. It doesn't even know that I deleted emails and continues to loop over the same ones.
mike
Ok. Maybe you have a cache problem and you could try [imap_open](http://fr.php.net/manual/en/function.imap-open.php) with option OP_SHORTCACHE ?
Serty Oan
Another question. Do you only do a imap_delete ? or do you do and imap_expunge after ?
Serty Oan
I'm not sure what the shortcache is. I'll have to look in to that a bit.I do a imap_delete($imapConnection, $msgNum) Which, seems to work correctly.
mike
From PHP.net : imap_delete marks messages listed in msg_number for deletion. Messages marked for deletion will stay in the mailbox until either imap_expunge() is called or imap_close() is called with the optional parameter CL_EXPUNGE.Maybe here is your problem, if no expunge is done, you current connection will still see the messages in the inbox.
Serty Oan
hmmm I'll give it a try, but that wouldn't explain why new emails aren't being found. I still think it has something to do with the imap_open() not begin within the loop. It doesn't seem to be getting the new messages every 15 seconds, which it should.
mike
I succeeded with imap_reopen(), just edited my answer
Serty Oan