email – PHP imap_open() too slow? Any alternatives?-ThrowExceptions

Exception or error:

Am I doing something wrong with IMAP mail connection? Because just this takes at least 40 seconds:

    $mbox = imap_open('{}INBOX', "", "password");


It works, but it’s too slow. As you can see – a basic open and close. Okay, 40 seconds might be acceptable, but this is without any operations in between! I tried connecting to gmail IMAP and to my personal host IMAP servers and the results were similar.

Now I tried this:

    $mbox = imap_open('{}INBOX', "", "password");

    $unreadMails = imap_search($mbox, 'UNSEEN');


By connecting to a freshly created mailbox which has only 3 emails (1 unread) and this took about 80 seconds when I executed it from my PC. I then uploaded this PHP script to my server and tried to execute it there, but it was even slower – it didn’t even manage to execute, it timed out.

Am I doing something wrong?

How to solve:

I just had the same problem. Changing domainname to IP address, or disabling SSL didn’t help. imap_open took about 30 seconds, the listing of mailboxes worked, but downloading of the messages caused an Exception.

It turns out that I have exceeded the bandwidth limit of GMail, because I had downloaded my whole 5 GB mailbox:
So I guess I’ll have to wait a month until I can access it again via PHP.


Check which authentication methods are enabled on your mailserver by connecting to it via openssl:

openssl s_client -connect

Look for the “AUTH=…” entries, Example:


Find out which authentication method is the one you are actually using and disable the other on in your imap_connect with DISABLE_AUTHENTICATOR. In my case CRAM-MD5 took much time, in another case

$mbox = imap_open('{}INBOX', "", "password", NULL, 0, array('DISABLE_AUTHENTICATOR' => 'CRAM-MD5') );

Alternatively: Disable the unneeded authentication methods in dovecot for example. If you do that, make sure it’s not used by anyone anymore or they will run into problems accessing their mailbox.

This solution is based on this posting related to GSSAPI authentication:

Another solution, as pointed out in several Stackoverflow answers, is to use the IP address and a port number in your imap_open statement, instead of a domain name. Not doing this obviously adds delay for resolving the DNS entry and finding the right port in some cases.


This is an old one, but I’ve found that using imap4rev1 speeds up your connection time to Gmail significantly.

Leave a Reply

Your email address will not be published. Required fields are marked *