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:

<?php
    $mbox = imap_open('{myhost.example.com:993/imap/ssl}INBOX', "mymail@example.com", "password");

    imap_close($mbox);
?>

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:

<?php
    $mbox = imap_open('{myhost.example.com:993/imap/ssl}INBOX', "mymail@example.com", "password");

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

    imap_close($mbox);
?>

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: https://support.google.com/a/answer/1071518
So I guess I’ll have to wait a month until I can access it again via PHP.

Answer:

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

openssl s_client -connect myhost.example.com:993

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

* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE AUTH=PLAIN AUTH=LOGIN AUTH=CRAM-MD5] Dovecot ready.

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('{myhost.example.com:993/imap/ssl}INBOX', "mymail@example.com", "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:
http://php.net/manual/en/function.imap-open.php#104701

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.

Answer:

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 *