【问题标题】:Retrieving Email with PHP in Batches用PHP批量检索邮件
【发布时间】:2012-01-18 10:20:28
【问题描述】:

我正在尝试使用 PHP 的 IMAP 类通过我的邮箱获取电子邮件。因为,消息数以千计,脚本执行时间过长。

有没有一种方法可以分批检索消息,例如每批 100 条消息。

另外,我怎样才能唯一地识别消息。是 *message_id* 还是 UID ?在 imap_fetch_overview()

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {

    /* begin output var */
    $output = '';

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,2);

        /* output the email header information */
        $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
        $output.= '<span class="from">'.$overview[0]->from.'</span>';
        $output.= '<span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';

        /* output the email body */
        $output.= '<div class="body">'.$message.'</div>';
    }

    echo $output;
} 

【问题讨论】:

  • 你能显示你获取这些消息的代码吗?
  • @klennepette 我正在关注这个 davidwalsh.name/gmail-php-imap
  • 好的,你还能发布代码吗,它真的很有帮助。另外是 imap_search() 速度慢还是之后会通过 for 循环?
  • @klennepette 它的代码相同。我现在只是在试验这些东西。

标签: php imap


【解决方案1】:

首先,我认为message_id 是邮件的唯一标识符,UID 仅在邮箱中是唯一的。

至于表演。我最好的猜测是 for 循环是慢的。 for 循环为您的数千封邮件获取整个邮件正文。 您可以将imap_search 限制为使用ON &lt;date&gt;UNSEEN,有关您可以在那里使用的更多信息,请参阅here

或者您可以重写循环,以便使用常规 for 循环一次只处理 100 条消息。

【讨论】:

    猜你喜欢
    • 2011-05-12
    • 2013-01-12
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2015-08-18
    相关资源
    最近更新 更多