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.

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

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置