Question

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.

Was it helpful?

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());

OTHER TIPS

I might be a bit late, but pagination with the Zend_Mail_Storage classes is straight forward. They all implement iterator interfaces and can be combined with a LimitIterator for pagination. The only gotcha is that they start with 1 instead of 0, because that's what all mail interfaces do.

So that's what you do:

$mail = new Zend_Mail_Storage_Pop3(/* ... */); 
foreach (new LimitIterator($mail, 1 + $offset, $limit + $offset) as $message) {
    // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top