I have a customer support system which creates emails when an email is received. I used to postfix and a special configuration to get a hold of the emails to add extra features.
For example I want to include attachments that were sent from an email. The system doesnt do this , but creates an email with the subject , so I can include the attachments by matching the subjects.
I used ImapMailBox.php to read through the email contents. It all works fine but I am getting an issue fetching the last email, so I am gettign contents from any other email with the same subject , so I need to fetch the latest email.
$mailboxP = new ImapMailbox('{127.0.0.1:143/novalidate-cert}',POSTFIX_EMAIL,POSTFIX_PASSWORD,ATTACHMENT_DIR, 'utf-8'); foreach($mailbox->searchMails('ALL') as $mailId) $mail = $mailbox->getMail($mailId); $mailx=(array)$mail; $att=$mailx['attachments'];
I have tried using usort to the object $mail , with a function like this
function mysort($a,$b) { return strtotime($a->date)-strtotime($b->date); }
and to the array with a function like this
function mysort($a,$b) { return strtotime($a['date'])-strtotime($b['date']); }
I have also tried using imap_sort to $mail and $mailx , but none of this works. errors I am getting
imap_sort() expects parameter 1 to be resource, array given imap_sort() expects parameter 1 to be resource, object given usort() expects parameter 1 to be array, object given when passing an array I get undefined index date but it defined ..
Can anyone please be kind enough to point me in the right direction.
Advertisement
Answer
You can add a function like this on ImapMailbox.php :
public function searchMailsSorted($imapCriteria = 'ALL') { $this->checkConnection(); $mailsIds =imap_sort($this->mbox,SORTDATE,1,SE_UID,$imapCriteria,$this->serverEncoding); return $mailsIds ? $mailsIds : array(); }
And then use it in your code like this:
foreach($mailbox->searchMailsSorted('ALL') as $mailId) { ///insert code here }