Reputation: 73241
I'm writing a little imap class and want to fetch mails which have been received during the last hour, but can't find a solution for this.
I've found the possibility of imap_search()
which is working, but only for dates:
$date = date ( "d M Y", strToTime ( "- 1 days" ) );
$this->date = imap_search ( $connection, "SINCE \"$date\"");
When doing
$date = date ( "d M Y h:i", strToTime ( "- 1 hours" ) );
$this->date = imap_search ( $connection, "SINCE \"$date\"");
it gives me the examt same result as with the first approach, but not only messages received during the last hour.
Same result for
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
Does somebody know what I could try?
Upvotes: 0
Views: 3351
Reputation: 427
there is no option to get emails using hours. but you can use it based on days. i wrote a solution for getting hourly emails here i am sharing that with you
$mailbox = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$imapResource = imap_open($mailbox, $username, $password);
$search = 'SINCE "' . date("j F Y", strtotime("0 days")) . '"';
$emails = imap_search($imapResource, $search);
$emails = array_reverse($emails);
if(!empty($emails)){
//Loop through the emails.
foreach($emails as $email){
//Fetch an overview of the email.
$overview = imap_fetch_overview($imapResource, $email);
$overview = $overview[0];
//Print out the subject of the email.
if(isset($overview->subject))
{
$emailsub = htmlentities($overview->subject);
}
//Print out the sender's email address / from email address.
$emailfrm = $overview->from;
$string = $emailfrm;
$pattern = '/<(.*?)>/i';
preg_match_all($pattern, $string, $matches);
$emailidfrom = $matches[1][0] ?? $string;
//Get the body of the email.
$message = imap_fetchbody($imapResource, $email, 1, FT_PEEK);
$unixTimestamp=strtotime($overview->date);
$mailtime = date("Y-m-d H:i:s", $unixTimestamp);
$start = date('Y-m-d H:i:s');
//set time here
$fromtime = date('Y-m-d H:i:s',strtotime('-60 minutes',strtotime($start)));
if (preg_match('/^([a-zA-Z0-9]{76} )+[a-zA-Z0-9]{76}$/', $message)) {
$message = base64_decode($message);
}
echo "<div class='faq-tile'>$message</div>";
if($mailtime>$fromtime)
{
// store to database
$message = nl2br($message);
$message = trim($message);
echo $message;
// $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
//save to database
}
}
}
}
Upvotes: 1
Reputation: 10985
IMAP Protocol does not support a time based search, only days.
You could do a date based search, then fetch the INTERNALDATE for those, and select the ones you want.
Alternatively if this is a process you are doing every hour, just track the UID of the newest message you have, and fetch any messages with higher UIDs.
Upvotes: 3