Solved

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
8
131 Views
Last Modified: 2014-12-02
The code I've been using (snippet):

while(true) {

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

	if($emails) {

		rsort($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

	sleep(5);

}

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
0
Comment
Question by:Mark
  • 5
  • 3
8 Comments
 
LVL 62

Expert Comment

by:gheist
ID: 40475275
You never check if connection is alive.
0
 

Author Comment

by:Mark
ID: 40475296
Could you supply the correct code then that checks as you mention and reconnects if necessary
0
 
LVL 62

Expert Comment

by:gheist
ID: 40475443
Add imap_open/imap_close in the loop
They are outside code presented, so I hold no chance to fix it.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Mark
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)
0
 
LVL 62

Expert Comment

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

Author Comment

by:Mark
ID: 40475873
I, what about imap_ping .. Any use with that in this instance
0
 
LVL 62

Accepted Solution

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

Expert Comment

by:gheist
ID: 40477532
if !(imap_check()) then
 reconnect
fi
doyour loop
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
This Micro Tutorial demonstrates how to create custom reports and the secrets of determine the metrics and dimensions for your data that works best with your needs.
This Micro Tutorial will demonstrate how to use comments in Google Docs. You will learn how to create, edit, and delete a comment.

825 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