dtra49684 2014-01-04 12:58
浏览 76
已采纳

imap_search()对于看不见的消息不会返回任何内容

I am currently trying to get the UNSEEN/UNREAD messages from my server. Currently, I have this:

$openmail = imap_open($dns, $email, $password) or die("Cannot Connect " . imap_last_error());
if ($openmail) {
    /* grab emails */
    $emails = imap_search($openmail, 'UNSEEN');
    if ($emails) {
        //For every e-mail.
        $tot = imap_num_msg($openmail);
        for ($i = $tot; $i > 0; $i--) {
            $structure = imap_fetchstructure($openmail, $i);
            if (isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
                $part = $structure->parts[1];
                $message = imap_fetchbody($openmail, $i, 2, FT_PEEK);

                if ($part->encoding == 3) {
                    $message = imap_base64($message);
                } else if ($part->encoding == 1) {
                    $message = imap_8bit($message);
                } else {
                    $message = imap_qprint($message);
                }
            }
            $header = imap_header($openmail, $i);
            $from = imap_utf8($header->fromaddress);
            $subject = $header->Subject;
            $subject = substr($subject, 0, 150);
            $date = $header->Date;
        }
        /* Print out the Unseen messages in here! */
    } else {
        /* No unseen messages */
        echo "No unseen";
    }
}

I've tried sending multiply emails to my mailserver, refreshed the page with the above script. But I keep getting the "No unseen".

I've tried to output the $emails but it's empty. It can't find anything.

If I try to just get ALL the messages (no unseen filter), I can see the emails I've sent to the mailbox, although, they're all marked as read.

  • 写回答

1条回答 默认 最新

  • dongxian3574 2014-01-06 19:09
    关注

    Your code $message = imap_fetchbody($openmail, $i, 2, FT_PEEK); uses hardcoded part offsets, i.e. it assumes that a message is always multipart/alternative with text/plain and text/html. That's a very wrong assumption.

    You have to look at the output of the BODYSTRUCTURE to see what the MIME structure of your mail is.

    With that out of the way, be sure that none of your commands use the BODY fetch operation; you have to use BODY.PEEK. I have no idea how this is accessible within the PHP c-client bindings.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入