rich
rich

Reputation: 1227

PHP IMAP Loop getting progressivly slower

I'm having a bit of a problem with IMAP. Basically, I am connecting to the server to get the folder names, re-connecting for each folder, looping through each message and doing what I need.

It's working except for the fact that things just get progressivly slower and slower. Any ideas with this? I have a funny feeling it is to do with imap_body as it seems to be this bit that is causing the issue. Do I need to clear this out for each loop somehow?

My code looks something similar to this (however with a load of bits removed)...

    $Mailbox = imap_open("$Hostname", "$Username", "$Password") or die('Error: ' . imap_last_error());
    $List = imap_list($Mailbox, "$Hostname", "*");

    if(is_array($List))
    {
            foreach ($List as $Folder)
            {
                    $FolderName = imap_utf7_decode($Folder);
                    $FolderStream = imap_reopen($Mailbox, "$Folder") or die('Error: ' . imap_last_error());
                    $FolderCount = imap_num_msg($Mailbox);
                    for($i=1;$i<=$FolderCount;$i++)
                    {
                            $EmailBody = imap_body($Mailbox, $i);
                            // Do whatever I need here
                    }
            }
    }
    imap_close($Mailbox);

Update I have sped it up a little by using the ip address of the server rather than the hostname (this is in addition to specifying the port), though the issue still stands with things getting slower as it goes.

Upvotes: 1

Views: 597

Answers (2)

user2077511
user2077511

Reputation:

try reconnecting to mail server after every ay 20/25 mails or when 50/100mb of data is downloaded. I have seen IMAP servers reducing the speed after this point...

Upvotes: 1

Anshul
Anshul

Reputation: 1446

This solely depends on your network and the connecting IMAP server's capability to cater client requests. imap_body is actually fetching the mail body and the mail size could vary from few kbs to 20-25 mbs.

Nothing much can be done from clients end.

Sometimes the servers try to throttle the connections in case the data transfer is too high in that case you can try reconnecting the connection and continue from previous state.

Updating to IP is not going to help you considerably instead i would recommend you to use host name only.

Upvotes: 1

Related Questions