Using PHP's IMAP module with Gmail, how do I keep a script alive checking for emails every 5 seconds

The code I've been using (snippet):

while(true) {

	$emails = imap_search($connection,'ALL');

	if($emails) {

		foreach($emails as $email_number) {
			$overview = imap_fetch_overview($connection, "{$email_number}:{$email_number}");
			$headers = imap_header($connection, $email_number);
			$replyto = $headers->reply_toaddress;
			$subject = $headers->Subject;

                       #........ code to delete email and a bunch more lines

	else echo "No New Emails\n";
	imap_gc($connection, IMAP_GC_ELT); # Clear Cache



Open in new window

The code works when left on for a few hours however I left it on this weekend and when I came back, it wasn't obtaining new emails (it just kept outputting "No New Emails"). When I restarted the script, it started working property...

Please advise, thanks
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You never check if connection is alive.
MarkProgrammerAuthor Commented:
Could you supply the correct code then that checks as you mention and reconnects if necessary
Add imap_open/imap_close in the loop
They are outside code presented, so I hold no chance to fix it.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

MarkProgrammerAuthor Commented:
I already have imap_open but at the beginning of the script before the infinite loop begins. I don't have an imap_close tag anywhere because I want to connection to remain alive.

I don't want the script to have to reconnect to gmail's servers every 5 seconds as it will slow its performance down. The way I have it now, it works well however after a few hours it stops working (I was even going to add a while ($loopCount<x) but that doesn't seem like the right way togo.

I assumed when building the script that imap_search would throw an error of it's no longer connected to a the server but that doesn't seem to be the case (or gmail has some sort of limits)
if imap_check()
 ... do your loop
else imap_close/imap_open
MarkProgrammerAuthor Commented:
I, what about imap_ping .. Any use with that in this instance
imap_check has same return semantics and updates mailbox status at the same time.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
if !(imap_check()) then
doyour loop
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.