3

I am trying to fetch email using imap function which return very old email. Following script i am trying

$imap = @imap_open("{mail.******.com:143/novalidate-cert}", $sEmail, $sPwd);
    $message_count = @imap_num_msg($imap);
    for ($m = 1; $m <= $message_count; ++$m){
     $header = @imap_rfc822_parse_headers(imap_fetchheader($imap, $m));
     $email[$m]['from'] = $header->from[0]->mailbox.'@'.$header->from[0]->host;
    }

Currently above function return email in ascending order, so that's why very old emails (2015's Email) are coming first. So is there any way to get recent email first.

13
  • 1
    Reverse the loop, start with $message_count and count down to 1. Commented Mar 27, 2018 at 12:06
  • 1
    you count up, here: for ($m = 1; $m <= $message_count; ++$m) - reverse this. Commented Mar 27, 2018 at 12:08
  • 1
    What is the behavior of your code in the block I have pointed out? What happens to $m in each iteration? Commented Mar 27, 2018 at 12:10
  • 1
    In this part of the code: for ($m = 1; $m <= $message_count; ++$m), you start by setting $m to 1; then, if it's below $message_count, you increment it (++$m). Commented Mar 27, 2018 at 12:14
  • 2
    Writing a for loop is hard? Okay, look at this answer - the first option they use there: stackoverflow.com/a/2089282/19746 Commented Mar 27, 2018 at 12:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.