Zend Framework IMAP connection stops working after fetching 2 Gmail messages - no error messages shown

Hello everyone! I’m building an app that connects to Google Apps. Users can link their Gmail accounts and view their emails through my application. Everything works perfectly when I use Google Apps email addresses, but regular Gmail accounts cause issues where the script just stops running.

Here’s the code I’m using:

$imap_client = new Zend_Mail_Storage_Imap($connection_settings);

$email_list = array();

$current_page = isset($_GET['p'])?$_GET['p']:1;
$per_page = isset($_GET['count'])?$_GET['count']:20;

$start_index = (($current_page-1)*$per_page)+1;

$final_index = ($current_page*$per_page)>$total?$total:($current_page*$per_page);
for ($x=$start_index;$x<=$final_index;$x++){

    $converter = new html2text();
    $converter->set_allowed_tags('<a>');

    if(!$imap_client[$x])
        break;
    else{
        $single_email = $imap_client->getMessage($x);
        $single_email->id = $x;
        $single_email->UID = $imap_client->getUniqueId($x);

        $single_email->message_parts = array();
        $single_email->content = '';
        $part_counter = 1;
        foreach (new RecursiveIteratorIterator($imap_client->getMessage($x)) as $index=>$message_part) {

            try {
                $temp_part = $message_part;
                $single_email->message_parts[$part_counter] = $temp_part;
                $part_counter++;
                
                if (strtok($message_part->contentType, ';') == 'text/html') {
                    $html_content = $message_part->getContent();

                    if($message_part->contentTransferEncoding == 'quoted-printable')
                        $html_content = quoted_printable_decode($html_content);

                    $single_email->html_content = $html_content;
                    $converter->set_html($html_content);
                    $single_email->content = $converter->get_text();
                }

                if (strtok($message_part->contentType, ';') == 'text/plain') {
                    $plain_content = $message_part->getContent();

                    if($message_part->contentTransferEncoding == 'quoted-printable')
                        $plain_content = quoted_printable_decode($plain_content);

                    $single_email->plain_content = $plain_content;
                    $single_email->content = $plain_content;
                }

            } catch (Zend_Mail_Exception $e) {
                continue;
            }

        }

        $email_list[] = $single_email;

    }
}

The weird thing is that it always stops after processing exactly 2 emails from Gmail accounts. Has anyone experienced this before?

Gmail’s probably dropping your IMAP connection after a few messages. Had the exact same issue last year building an email migration tool. Gmail’s way more aggressive about connection management than Google Apps servers. Wrap your getMessage calls with a connection check and add reconnection logic. Test with $imap_client->noop() before each fetch - if it fails, just reinit your IMAP connection. Also double-check how you’re handling multipart messages. Gmail’s got way more complex message structures that’ll make your parser hang without throwing any exceptions.

sounds like gmail’s timing you out or hitting a quota limit. add error_reporting and ini_set for display_errors to see what’s actually happening when it dies. gmail imap is picky about fetch speed - try adding a small sleep between iterations. my guess is it’s either a silent fatal error or gmail cutting the connection after 2 requests.

I’ve encountered a similar issue with Zend Framework when handling Gmail accounts through IMAP. It seems that Gmail can return larger message sizes compared to other email providers, which might lead to hitting PHP’s memory limits or exceeding script execution time. To address this, consider increasing the memory limit to, for example, 256M and setting the maximum execution time to 300 seconds at the beginning of your script. Additionally, adding some error logging within your loop can help identify where the process is failing. Another tip is to introduce a brief delay, such as usleep(100000), between fetches to prevent getting rate-limited by Gmail’s IMAP servers.