【问题标题】:imap_search limit the number of messages returned using $emails = imap_search($inbox, "SINCE \"$since_date\"");?imap_search 使用 $emails = imap_search($inbox, "SINCE \"$since_date\""); 限制返回的邮件数量?
【发布时间】:2014-05-01 08:39:17
【问题描述】:

我有 PHP 脚本 可以从邮箱中获取消息。我使用 imap_search 功能:

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

我可以将限制应用于imap_search();function 以上,并且小时数也适用于 $since_date

表示 $since_date=24 march 2014 12:33:14这边。

【问题讨论】:

    标签: php cakephp search limit imap


    【解决方案1】:

    恐怕这两个问题的答案是否定的。但如果您的服务器有WITHIN extension,您可以按小时搜索。几乎没有服务器这样做。

    【讨论】:

    • 请回答它的银色
    • 紧迫感不会改变答案。不,您不能应用限制,不,您不能使用小时数。
    • 搜索只返回消息 ID。您可以在它们返回后通过截断列表来应用自己的限制。
    【解决方案2】:

    由于 imap_search 仅允许您按 日期 进行搜索,为了进一步减少 时间 的搜索结果,您可以循环浏览过去 2 天的电子邮件并检查它们标题。这是一个例子:

    // 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;
    

    我会返回 2 天的原因是因为 Gmail 的时区可能与您的服务器不同。如果您不想在额外的一天中循环,您可以在 Gmail 的设置中更改您的时区。

    【讨论】:

      猜你喜欢
      • 2011-01-11
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多