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.

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

报告相同问题?