问题
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