Requesting the headers for about 300,000 IMAP mails will off course require a signifcant amount of time.
My suggestions:
Why do you call
imap_search()
?
You want to process all messages. Then there is no need to do so. You even don't use the returned array anymore besides calling
rsort()
which is useless.
I would try (untested):
for ($i = $n_msgs - 1; i >= 0; $i--)
{
$header = imap_header($inbox ,$i);
}
Note that I have changed the loop to start with the highest message number (which you might have tried using
rsort
()) and that it includes index zero while your code starts at index one.
You might also try to use
PHP: imap_headers - Manual[
^] instead using a
foreach
loop and a regular expression to extract the Reply-to header line. This should be faster because it retrieves all headers at once while your solution fetches the headers of each message.