Reputation: 424
system info: Running on CentOS release 6.2 (Final), PHP 5.3.3, Dovecot + postfix setup, Apache/2.2.15 (Unix)
connection line:
$mbox = imap_open({'{localhost:143/imap/novalidate-cert}INBOX','un','pw');
Changing the port and/or using the ip and domain name have no impact...
Consistently takes between 5.5 and 7 seconds to run the imap_open command...
any idea how I can get this to run faster?
Upvotes: 0
Views: 3123
Reputation: 4494
Here are two steps to get you started. You want to isolate the cause - is it PHP or is it the local IMAP server. These steps use telnet
and strace
. You might need to install them first:
yum install telnet
yum install strace
First: Test the connection locally using telnet
$ telnet localhost 143
...
a LOGIN username password
Is there still a delay? If so - when: at the initial connection or after the LOGIN
command? If there is a delay, the problem is related to IMAP on your server and not PHP.
Second: If connecting through telnet is quick, make a minimal PHP-script and debug it using strace
<?php
// save this as imap_test.php
$mbox = imap_open({'{localhost:143/imap/novalidate-cert}INBOX','un','pw');
?>
$ strace -t -s 200 php ./imap_test.php
To save the output from strace to a file strace.log
:
$ strace -t -s 200 php ./imap_test.php 2>&1 > strace.log
strace writes all its output to stderr.
2>&1 means to combine stderr with stdout, then > to write to the logfile.
If you want to see the output AND write it to a file, use tee:
$ strace -t -s 200 php ./imap_test.php 2>&1 | tee strace.log
You will get lots of output. Try to see if and where it hangs, waiting for something. It is often useful to quickly press ENTER several times when you see it hanging - that way it's easier to locate by scrolling back in the output after the command has finished.
With a little patience (and luck) you should be able to detect where PHP is waiting, and for what.
Edit: (or what happened next)
One issue was with SSL/TLS which caused delays. Adding the /notls
option solved it:
// This should be ok for internal connections to localhost, but you really
// dont want to disable TLS on an open network
$mbox = imap_open('{localhost:143/imap/notls}INBOX','un','pw');
Disabling TLS causes PHP warnings. Here is a discussion about handling them.
One further issue was authentication methods. Basically php's imap_open will try all protocols. People complained for years and it was finally fixed by adding a new parameter in php 5.3:[3]
$mbox = imap_open('{localhost:143/imap/notls}INBOX','un','pw',NULL,1,array('DISABLE_AUTHENTICATOR' => array('GSSAPI','NTLM)));
Long story short: from 6 seconds to maybe 6 miliseconds.
[3] https://bugs.php.net/bug.php?id=33500
Upvotes: 7