我正在为我的网络应用构建邮件模块。在我目前,我正在尝试获取邮件正文并正确解码它。但是,当我在邮件中遇到国际字符时,它无法正确解码它们。
离。我有一个原始的电子邮件正文:
--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
Niels S�nderb�k
应为Niels Sønderbæk
。在处理国际角色时,我只看到过这个问题。有人知道怎么解决吗?我在下面提供了我的解码代码。它来自http://www.sitepoint.com/exploring-phps-imap-library-1/。
<?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);
?>
答案 0 :(得分:0)
是的,标题表示它的iso8859-1字符集编码,但您只撤消了传输编码。您仍然需要从电子邮件到Web应用程序字符集进行字符集转换,大概是utf8。