Search code examples
phpzend-frameworkpop3mail-serverzend-mail

Getting some and not all mails using Zend_Mail_Storage


I use Zend_Mail_Storage_Pop3 to fetch mails from a given mail server. Now, the issue is that the framework provides a way to fetch all the mails from the storage (Zend_Mail_Storage_Pop3::getMessages()) but that obviously could be overwhelming when talking about a few thousands of mails in a box. Thus the question, how do I fetch some and not all of the mails in a box (more like the SQL LIMIT statement) so I could probably paginate the "resultset".

Thank You.


Solution

  • Such functionality is not available. However, all messages have a number starting with 1. If you are just reading emails, then you can cache last opened message number, and next time start to retrieve emails starting from this (cached) number. Another solution is to cache all opened messages (read more) but the trick is how to invalidate the cache in this case.

    Quick Example:

    $mail = new Zend_Mail_Storage_Imap(array('host'     => 'localhost',
                                             'user'     => 'root',
                                             'password' => '******'));
    
    $cachedId = (apc_exists('email_id') ? apc_fetch('email_id') + 1 : 1);
    
    for ($id = $cachedId ; $id <= $mail->countMessages() ; $id++) {
        echo sprintf('%d, %s <br/>', $id, $mail->getMessage($id)->subject);
    }
    
    apc_store('email_id', $mail->countMessages());