【发布时间】: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 它的代码相同。我现在只是在试验这些东西。