Search code examples
phpzend-mail

How to do batch retreival of email using Zend_Mail_Protocol_Imap or Zend_Mail_Storage_Imap


I am using Zend_Mail_Storage_Imap to access email but using following code

$storage = new Zend_Mail_Storage_Imap($imap);
$allIds = $storage->getUniqueId(); // i get all key value pair of meesageid and uniqueid
foreach ($allIds as $k => $v) 
{
    echo '<li>' . htmlentities($storage->getMessage($v)->subject) . "</li>\n";
}

My problem is that it loops and get one email at a time which is slow like getting two emails per second which is very slow . I am looking for batch retreival method of these mails but could not find any . Did anybody do it before


Solution

  • Finally got it.

    Where $imap is an instantiated and connected version of Zend_Mail_Protocol_Imap:

    1. $imap->select(); // or $imap->select('FOLDER_NAME');

    2. $imap->requestAndResponse('SEARCH UNSEEN', $imap->escapeString('*'))//all unread email ids

    3. $imap->requestAndResponse('SEARCH UNSEEN FROM "[email protected]"', $imap->escapeString('*'))//all unread email id's from [email protected]

    4. $imap->requestAndResponse('SEARCH UNSEEN SUBJECT "test" FROM "[email protected]"', $imap->escapeString('*'))//all unread email id's from [email protected] with the subject of test

    *You must do #1 above first or else you'll get something similar to: "TAG# BAD SEARCH not allowed now."

    All of the above will return an array similar to the below array:

    //C: TAG3 SEARCH UNSEEN
    //S: * SEARCH 321 323 362 371 377 384 386 387 388 389 416 417 418 
    //S: TAG3 OK SEARCH completed (Success) 
    //The above lines are in the format of RFC 1730 to show what was actually sent/received.
    
    array (size=1)
      0 => 
        array (size=14)
          0 => string 'SEARCH' (length=6)
          1 => string '321' (length=3)
          2 => string '323' (length=3)
          3 => string '362' (length=3)
          4 => string '371' (length=3)
          5 => string '377' (length=3)
          6 => string '384' (length=3)
          7 => string '386' (length=3)
          8 => string '387' (length=3)
          9 => string '388' (length=3)
          10 => string '389' (length=3)
          11 => string '416' (length=3)
          12 => string '417' (length=3)
          13 => string '418' (length=3)