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

Posted on 2014-12-01
Last Modified: 2014-12-02
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
Question by:Mark
  • 5
  • 3
LVL 61

Expert Comment

ID: 40475275
You never check if connection is alive.

Author Comment

ID: 40475296
Could you supply the correct code then that checks as you mention and reconnects if necessary
LVL 61

Expert Comment

ID: 40475443
Add imap_open/imap_close in the loop
They are outside code presented, so I hold no chance to fix it.

Author Comment

ID: 40475792
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)
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

LVL 61

Expert Comment

ID: 40475832
if imap_check()
 ... do your loop
else imap_close/imap_open

Author Comment

ID: 40475873
I, what about imap_ping .. Any use with that in this instance
LVL 61

Accepted Solution

gheist earned 500 total points
ID: 40476761
imap_check has same return semantics and updates mailbox status at the same time.
LVL 61

Expert Comment

ID: 40477532
if !(imap_check()) then
doyour loop

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

With voice direction tools, various map viewing styles, the ability to get directions for different means of transportation and great service, Google Maps is simply one of the best navigations app in the market.    However, over the past year, an …
Whether you’re looking to gather data for research or gather feedback on an idea, being able to build and distribute your own online survey is not only cost-effective, but allows you to reach a larger audience and receive results in real-time. Googl…
This Micro Tutorial  demonstrates whether your site uses one subdomain or multiple subdomains, how to create full URLs from Google Analytics content reports. This procedure is called concatenation and can also be done with the CONCATENTATE function.…
This Micro Tutorial demonstrates in Google Analytics how to create a custom report that shows you traffic over time using the month of year dimensions. There are also instructions on how to fix Google's odd month of year formatting, which Microsoft …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now