Jump to content


This topic is now archived and is closed to further replies.


IMAP and speed issues

Recommended Posts

Hi all,

I'm having a few issues with IMAP functions taking a long time to retrieve information from the server. I've noticed that there seem to be a few IMAP functions that have a cross over in terms of what they return and was wondering if anyone had any suggestions as to which is best for my case (in terms of speed).

All I want to do is contact an IMAP server and get back the FROM, DATE, and SUBJECT of the unread emails (without changing their status flag on the server). At the moment I'm doing that using the following (simplified) code:

$server = gethostbyname("imap.example.com");
$imap_server_str = "\{$server:143/imap/ssl}INBOX";
$conn = imap_open($imap_server_str, "username", "password");
$overview = imap_fetch_overview($conn);

Once I have $overview I run through the array and pickout the unread emails by checking for seen=="0" && recent=="0". Now this works fine and gets me what I want. However it takes a very long time if the imap inbox is large (on the order of a 1000 or so emails). It seems to me that there must be a better way of doing this - currently I'm retrieving the overview details of 1000+ emails simply to find the <10 emails that are new. Note that in the above code, I'm converting the name address of the server to an IP address as read on the php functions page for imap_open() that doing this conversion myself saves the imap_open() a few seconds of time.

Incidentally - I did a time check to see which part of the code was taking all the time and found the following:

imap_open() takes : ~0.01 seconds on average

imap_fetch_overview() takes : between 30 and 50 seconds

loop to extract the relevant information from the $overview array takes : ~0.04 seconds.

I'd be grateful if anyone could offer advice on this. I've tried using imap_headers() instead of imap_fetch_overview() but its just as long (sometimes longer in fact!). I know that a simple solution would be to reduce the number of emails in the inbox... thats all very well for me to do, but I can't really stipulate that every user of my script should do the same!

Thanks in advance,


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.