php imap how to search unread and also sort them by date?

你说的曾经没有我的故事 提交于 2020-08-10 13:56:12

问题


Is it possible to get the unread messages and also sort them messages by date ? I have

$messages = imap_search($imap,"UNSEEN");
  imap_sort($imap, SORTDATE, 1);

but I'm wondering if it shouldn't be

  imap_sort($imap, SORTDATE, 1);
$messages = imap_search($imap,"UNSEEN");

or something else ?


回答1:


You may use

$messages = imap_search($imap,"UNSEEN");
$sorted = array_reverse($messages);



回答2:


Here is a code which will help you:

$host = '{imap.gmail.com:993/imap/ssl}INBOX';

// Connect to the pop3 email inbox belonging to $user
$con = imap_open("$host", $user, $pass) or die("Can't connect: " . imap_last_error());

// after some sleeping
if (!imap_ping($con)) {
    // do some stuff to reconnect
die("The user is no longer logged in.");
} else {
    echo ('Connection Successful !');
}

$MC = imap_check($con);

// Get the number of emails in inbox
$range = "1:".$MC->Nmsgs; 

// Retrieve the email details of all emails from inbox
$response = imap_fetch_overview($con,$range); 
$response = array_reverse($response);

// displays basic email info such as from, to, date, subject etc...
foreach ($response as $msg) {
    // extra filters to show records which are Unread/Not seen  
    if($msg->seen == "0"  && $msg->recent == "0"){
        echo '<pre>';
        var_dump($msg);
        echo '</pre><br>-----------------------------------------------------<br>';
    }
}

Eventually, you can put condition for date wise sorting or key wise sorting with ksort function




回答3:


If you use imap_sort you don't need to use imap_search, as imap_sort admits a parameter $search_criteria as well as imap_search. Let's say imap_sort is like imap_search, but you can also get the results ordered.

$messages = imap_sort($imap, SORTDATE, 1, SE_UID, 'UNSEEN');



回答4:


U r performing two different actions on the original set of data independently. One possible way is to take all the data from first command(search), store it in some variable and then implement your own sort functionality.



来源:https://stackoverflow.com/questions/15035454/php-imap-how-to-search-unread-and-also-sort-them-by-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!