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?