2014-03-25 53 views
1

我正在爲我的web應用程序構建郵件模塊。在我目前的情況下,我試圖抓取郵件正文並正確解碼。但是,當我在郵件中遇到國際字符時,它不會正確解碼它們。php imap郵件正文編碼

ex。我有一個原始電子郵件正文:

--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

是的,頭文件說它是iso8859-1字符集編碼,但你只是撤消了傳輸編碼。您仍然需要從電子郵件到您的Web應用程序字符集進行字符集轉換,大概是utf8。

+0

我該怎麼做? –