Problems with PHP System_Daemon and IMAP connection
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 = 开发者_JAVA百科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?
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.
精彩评论