Speed up your Gmail IMAP downloading

Photo by IslandBoy

Now I’m getting deeper into using the IMAP API to pull email from Google, I’m hitting a lot of performance issues. Most of them are on the parsing and database loading side, but while profiling I did discover a few ways I was using IMAP inefficiently. I’ve updated my original PHP/Gmail example with some optimizations. The main speed boost was switching from grabbing all the email headers using imap_headers() just to get the total number of messages in the mailbox. That’s very inefficient, especially on large mailboxes. Instead I just call imap_num_msg() to get the count directly, and that’s much faster. Another wrinkle was asking for the INBOX mailbox to get all the messages. It’s better to look for [Gmail]/All Mail if you want the complete set of non-spam email in case the user has organized their mail into different folders, though you do also get the sent mail as part of that.

Here’s the source code as a zip, or you can give it a try online. Big thanks to Rob and Josh at EventVue for trying some of this out on their mailboxes too, they’ve been a fantastic help.

One response

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: