Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php imap邮件正文编码_Php_Encoding_Imap - Fatal编程技术网

php imap邮件正文编码

php imap邮件正文编码,php,encoding,imap,Php,Encoding,Imap,我正在为我的web应用程序构建邮件模块。目前,我正在尝试获取邮件正文并正确解码。然而,当我在邮件中遇到国际字符时,它无法正确解码 例如,我有一个原始的电子邮件正文: --001a11c126f6bd3aa804f575bd85 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable ss -- s Niels S=F8nderb=E6k --001a11c126f6bd3a

我正在为我的web应用程序构建邮件模块。目前,我正在尝试获取邮件正文并正确解码。然而,当我在邮件中遇到国际字符时,它无法正确解码

例如,我有一个原始的电子邮件正文:

--001a11c126f6bd3aa804f575bd85 Content-Type: text/plain; charset=ISO-8859-1 
Content-Transfer-Encoding: quoted-printable ss -- s Niels S=F8nderb=E6k --001a11c126f6bd3aa804f575bd85 
Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

ss
-- s
Ni= els S=F8nderb=E6k
--001a11c126f6bd3aa804f575bd85--
这是解码后的结果:

ss
-- s
Niels S�nderb�k
尼尔斯夫妇�nderb�k应该是Niels Sønderbæk。我只在处理国际角色时看到过这个问题。有人知道怎么修吗?我在下面包含了我的解码代码。它是从一个地方拿走的


是的,标题上说是iso8859-1字符集编码,但您只撤消了传输编码。您仍然需要执行从电子邮件到Web应用程序字符集的字符集转换,大概是utf8

<?php

$imap = imap_open(...);

$uid = ...

function getBody($uid, $imap) {
    $body = get_part($imap, $uid, "TEXT/HTML");
    // if HTML body is empty, try getting text body
    if ($body == "") {
        $body = 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 == 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 imap_utf8($text);
           }
       }

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

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

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

echo getBody($uid,$imap);
?>