IMAP Fetch causing slow loading times


I have a PHP webpage that displays a list of email subject lines and the body from the past month. There are only about 25 emails for it to load and they are reasonably short with no attachment. To achieve this I am using the below code:

$result = imap_fetch_overview($inbox,"1:{$MC->Nmsgs}",0);
foreach ($result as $overview) {


    if (date('m', $timestamp) === date('m')) {
        $timestamp = $overview->udate;
        $realdate = gmdate("Y-m-d", $timestamp);

        $messageBody = imap_fetchbody($inbox,$overview->uid,1);

        echo "<tr><td><button style='background: none; border: none; padding: 0;' onclick=\"showContent('".$overview->uid."')\" class=\"\">" . " {$overview->subject} " . "</button></td><td>" . "{$realdate} " . "</td></tr>";
        echo "<tr><td id='".$overview->uid."' class='w3-container w3-hide '><div><p>$messageBody</p></div></td></tr>";

This does eventually load but it is taking about 30 seconds, my question is is there anyway for me to reduce these loading times?


There is a lot of info on this over the net. Looking around for you, there were a lot of issues that might be the problem. They mentioned port issues, SSL connections and other types of problems. There was a ton of issues with certain email servers.

Which service are we talking about? Google’s Gmail, for example has an API set up just to use for your connection which makes it work much faster. You showed us the email reading code, but, not how you are connecting to the server. (Remember not to show us your passwords or live accounts!)

Please show what connection types you are using.

EDIT: This is one example of the host line: $hostname = ‘{}INBOX’;
Note that the port number and SSL is very important!!


It takes time to get into a mail system and query it.