简体   繁体   English

如何从imap_body结果中仅提取HTML

[英]How to extract only HTML from imap_body result

I want to extract only the HTML content from a imap_body result. 我想只从imap_body结果中提取HTML内容。 The imap_body give a verbatim copy of the mail. imap_body给出了邮件的逐字副本。

I found a solution: 我找到了解决方案:

function getBody($uid, $imap)
{
    $body = $this->get_part($imap, $uid, "TEXT/HTML");
    // if HTML body is empty, try getting text body
    if ($body == "") {
        $body = $this->get_part($imap, $uid, "TEXT/PLAIN");
    }
    return $body;
}

function get_part($imap, $uid, $mimetype, $structure = false, $partNumber = false)
{
    if (!$structure) {
        $structure = imap_fetchstructure($imap, $uid, FT_UID);
    }
    if ($structure) {
        if ($mimetype == $this->get_mime_type($structure)) {
            if (!$partNumber) {
                $partNumber = 1;
            }
            $text = imap_fetchbody($imap, $uid, $partNumber, FT_UID);
            switch ($structure->encoding) {
                case 3:
                    return imap_base64($text);
                case 4:
                    return imap_qprint($text);
                default:
                    return $text;
            }
        }

        // multipart
        if ($structure->type == 1) {
            foreach ($structure->parts as $index => $subStruct) {
                $prefix = "";
                if ($partNumber) {
                    $prefix = $partNumber . ".";
                }
                $data = $this->get_part($imap, $uid, $mimetype, $subStruct, $prefix . ($index + 1));
                if ($data) {
                    return $data;
                }
            }
        }
    }
    return false;
}

function get_mime_type($structure)
{
    $primaryMimetype = ["TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"];

    if ($structure->subtype) {
        return $primaryMimetype[(int)$structure->type] . "/" . $structure->subtype;
    }
    return "TEXT/PLAIN";
}

http://php.net/manual/en/function.imap-fetchbody.php http://php.net/manual/en/function.imap-fetchbody.php

Parameter 3, "the section" is as follows: 参数3,“该部分”如下:

The part number. 零件号。 It is a string of integers delimited by period which index into a body part list as per the IMAP4 specification 它是一个由句点分隔的整数字符串,它根据IMAP4规范索引到正文部分列表中

(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - file.ext

Therefore, to grab the HTML part of the mail, you would have to use the 1.2 option as the third parameter. 因此,要获取邮件的HTML部分,您必须使用1.2选项作为第三个参数。 Like so: 像这样:

$message = imap_fetchbody($inbox, $number, 1.2);

I don't have enough reputation to add a comment, but I just wanted to clarify in @GunniH's answer that your call to the function should look like this: 我没有足够的声誉来添加评论,但我只是想在@ GunniH的答案中澄清你对该函数的调用应如下所示:

$message = imap_fetchbody($inbox, $number, '1.2');

instead of this 而不是这个

$message = imap_fetchbody($inbox, $number, 1.2);

That final argument should be a string , not an int . 最后一个参数应该是一个string ,而不是一个int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM