douyi5157 2015-03-13 19:20
浏览 97

慢速php imap电子邮件提取(> 10秒)

I am building my own webmail client. Like Roundcube or Squirrelmail, for example. The problem is that my version is very slow, while Roundcube is fast and I cannot understand why is that (Roundcube's source ir very big and I am unable to dive in it..)

The goal - fetch last 50 messages from mailbox. My strategy:

  1. Get number of messages in mailbox by imap_num_msg()

  2. Make array of sequence numbers from max to (max-50)

  3. For each sequence number I ran functions imap_header() and imap_fetchstructure()

It takes 10-15 seconds. It allows me to get each messages title, date, whether is has attachments or not, from, to and other information.

However, Roundcube displays the same info, but load time is only 3 seconds or so. My strategy seems to be very wrong. How can I do it faster? I'm pretty sure that it must be slow to ran imap_header and imap_fetchstructure for each sequence number, but I think there is no other way to get that information.. I'm doing something like this:

function getMessageBySequenceNumber($imapStream, $sequence_number){

    $header = imap_header($imapStream, $sequence_number);
    $structure = imap_fetchstructure($imapStream, $sequence_number);

    /* 
    ... some code parsing $structure to find out whether this emails has any attachments or not 
    */

    return [
        'uid' => imap_uid($imapStream, $i),
        'subject' => $header->subject,
        'timestamp' => $header->udate,
        'unseen' => $header->Unseen,
        'star' => $header->Flagged,
        'draft' => $header->Draft,
        'size' => $header->size,
        'has_attachments_bool' => $has_attachments_bool,
    ];
}

$imapStream = imap_open();
$first_sequence_number = imap_num_msg(); // lets imagine it returns 100
$last_sequence_number = $first_sequence_number-50;
$sequence_numbers = [100,99,88 ..., 51, 50];

$messages = [];
foreach($sequence_numbers as $sequence_number){
    $messages[] = getMessageBySequenceNumber($imapStream, $sequence_number);
}

return $messages;
  • 写回答

1条回答 默认 最新

  • duanbei1709 2015-03-17 15:47
    关注

    You are fetching the messages one-by-one. This means that your PHP code has to wait for the remote IMAP server to answer you, then your PHP code is going to process the (partial) response, send the data back to the server, etc.

    Use an IMAP library which allows batched operations, and read RFC 3501 to understand how to use it.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题