简体   繁体   中英

Imap Mail sort with imap class

I have a little trouble for sorting mij mails on Arrival,

i have this code to get my mail :

$mailbox = new ImapMailbox($hostname,$row["user_email"],$row["user_mail_password"]) 

/* Count mails in $mbox */
$mail_count = $mailbox->countMails();

/* put the newest emails on top */
$mails_sort = $mailbox->sortMails();
var_dump($mails_sort);
/* overview mails in $mbox */
$mailsIds = $mailbox->searchMailBox('ALL');
 var_dump($mailsIds);

/* get data of mails  */
$mails = $mailbox->getMailsInfo($mails_sort);

/* for every email... */
foreach ($mails as $mail) {
    /* Put date result at DateTime constructor*/
    $message_date=new DateTime($mail->date);

    /* output the email header information */
    $output.= '<tr class="'.($mail->seen ? 'read' : 'unread').'">';
    $output.= '<td class="small-col"><input type="checkbox" /></td>';
    $output.= '<td class="small-col"><i class="clickable fa fa-'.($mail->flagged ? 'star' : 'star-o').'"></i></td>';
    $output.= '<td class="name"><a href="?page=read-mail&folder='.$mbox.'&uid=' . $mail->uid . '" >' . $mail->from . '</a></td>';
    $output.= '<td class="subject"><a href="?page=read-mail&folder='.$mbox.'&uid=' . $mail->uid . '" >' . $mail->subject . '</a></td>';
    $output.= '<td class="time">'.$message_date->format("d-m-Y H:i").'</td>';
    $output.= '</tr>';
}

when i run this code my mail is showing from old to new, i want to show it from new to old,

i am using imap class from : https://github.com/barbushin/php-imap

there is a function :

    public function sortMails($criteria = SORTARRIVAL, $reverse = true) {
        return imap_sort($this->getImapStream(), $criteria, $reverse, SE_UID);
    }

The output from : $mails_sort is the correct output, all UID's from new to old.

The output from : $mailsIds is when al the UID's are from old to new.

But it wont show my mails in the correct order, i think i am missing something here.

just use one line before foreach loop rsort($mails);

$inbox = imap_open($hostname,$username,$password,NULL,1) or die('Cannot connect to Mail Server: ' . print_r(imap_errors())); /* grab emails */

    $mails = imap_search($inbox,'ALL');    // UNSEEN = for unread mail,  SEEN = READ mail , ALL = all mail READ as well as UNREAD mail

    /* if emails are returned, cycle through each... */

    rsort($mails);


    foreach($mails as $key => $value) {

                         // body
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM