質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top