PHP method for imap_search by date

I am trying to show only emails received in the last 5 minutes, for example:

$since = date('d-M-Y G\:i', time() - 300); // current time - 5 min
$mb = imap_search ($m_mail, 'UNSEEN SINCE ' . $since . '');

      

Is there an easy way to do this? I found a way: take all invisible emails for the current day (ex: May 9, 2010), skip these emails, and then check if they were sent in the last X minutes. If so, echo (using imap_headerinfo-> udate).

However, when I looked at gmail.com, one email was received at 5:08 pm and it turned out on my server that the email was received at 2:08 pm

UPDATE: I solved the problem where it showed me that the email was sent at 17:04 on gmail.com and my app shows 14:04. I changed the global server timezone to GMT +0 and set the application timezone. Now it shows me the time correctly, but still I don't know how to get the emails received in the last X-period in short.

+2


a source to share


2 answers


Since PHP uses IMAP2 search services that don't allow time-based searches, this is probably the only solution. Or you can look at the RECENT flag, but it still requires scrolling through all the messages of the day.



+4


a source


I am using the search above:



$mailbox = imap_open($str_conexao, $login, $senha) or die("Can't connect: " . imap_last_error());

echo $since = date("D, d M Y", strtotime("-1 days"));

$search = imap_search($mailbox, 'FROM "somebody@exemple.com" SINCE "'.$since.' 00:00:00 -0700 (PDT)"', SE_UID);
echo "<PRE>";
  var_dump($search);
echo "</PRE>";
foreach ($search as $msg_id)
{
    //Pega informações da mensagem
    $overview = imap_fetch_overview($mailbox, $msg_id, FT_UID);
    echo "<PRE>";
     var_dump($overview);
    echo "</PRE>";
}

      

-2


a source







All Articles