imap_search limit the number of messages returned using $emails = imap_search($inbox, "SINCE \"$since_date\"");?

StackOverflow https://stackoverflow.com/questions/22601794

質問

I have PHP script that fetch messages from a mailbox. I use the imap_search function:

$emails = imap_search($inbox, "SINCE \"$since_date\"");

Can I apply Limit to above imap_search();function and also hours apply in $since_date

means $since_date=24 march 2014 12:33:14 this way.

役に立ちましたか?

解決 2

The answer to both questions is no, I'm afraid. But if your server has the WITHIN extension you can search by hour. Hardly any servers do.

他のヒント

Since imap_search only lets you search by date, to further reduce your results down by time you can loop through the emails from the last 2 days and check their headers. Here's an example:

// Get a count of all the emails sent in the last 24hrs 
// (perhaps to see if you've reached your gmail sending limit)

$targetCount = 0;

// Get last 2 days of emails in the SENT box
$date = date("j-M-Y", strtotime("-1 day")); 
$imap = imap_open('{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail', 'your-email-address-here', 'your-password-here');
$emails = imap_search($imap, 'SINCE ' . $date);

if($emails) {
    $count = count($emails);

    // Loop through each email and only count those within the 24 hour period
    for($i = 0; $i < $count; $i++){
        $header = imap_header($imap, $emails[$i]);
        // Note: I'm using the package Carbon (to work with dates easier) 
        $date = Carbon::createFromTimeStamp(strtotime(substr($header->date, 0, -6)),'America/Chicago');
        $date->addHours(5); // <-- Adjust for your timezone
        if($date->diffInHours(Carbon::now('America/Chicago'), false) <= 24){
            $targetCount++;
        }
    }
}

imap_close($imap);
echo $targetCount;

The reason I would go back 2 days is because Gmail's timezone may be different from your server. You can change your timezone in Gmail's settings if you want to avoid having to cycle through that additional day.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top