2016-12-07 96 views
0

我已經從PHP IMAP decoding messages中複製了以下代碼,但我仍在爲實現而苦惱。這就是我已經實現了這一點:PHP Imap正文解碼問題

public function readMessage($imap,$email_number){ 

    $message['header'] = imap_headerinfo($imap,$email_number,0); 
    $message['overview'] = imap_fetch_overview($imap,$email_number,0); 
    //$message['body'] = imap_fetchbody($imap,$email_number,2); 

    // Get the message body. 
    $body = imap_fetchbody($imap, $email_number, 1.2); 
    if (!strlen($body) > 0) { 
     $body = imap_fetchbody($imap, $email_number, 1); 
    } 

    // Get the message body encoding. 
    $encoding = $this->getEncodingType($imap,$email_number); 

    // Decode body into plaintext (8bit, 7bit, and binary are exempt). 
    if ($encoding == 'BASE64') { 
     $message['body'] = $this->decodeBase64($body); 
    } 
    elseif ($encoding == 'QUOTED-PRINTABLE') { 
     $message['body'] = $this->decodeQuotedPrintable($body); 
    } 
    elseif ($encoding == '8BIT') { 
     $message['body'] = $this->decode8Bit($body); 
    } 
    elseif ($encoding == '7BIT') { 
     $message['body'] = $this->decode7Bit($body); 
    } 

    return $message; 
} 

出於某種原因,getEncodingType調用總是返回格式7位顯示文本時出現問題的原因,並表示垃圾。這裏是這個功能:

public function getEncodingType($imap, $messageId, $numeric = false) { 
    // See imap_fetchstructure() documentation for explanation. 
    $encodings = array(
     0 => '7BIT', 
     1 => '8BIT', 
     2 => 'BINARY', 
     3 => 'BASE64', 
     4 => 'QUOTED-PRINTABLE', 
     5 => 'OTHER', 
    ); 
    // Get the structure of the message. 
    $structure = $this->getStructure($imap, $messageId); 
    // Return a number or a string, depending on the $numeric value. 
    if ($numeric) { 
     return $structure->encoding; 
    } else { 
     return $encodings[$structure->encoding]; 
    } 
} 

你能指出我做錯了什麼嗎?

+0

對不起,愚蠢的變量名是從我讀高中。我已經更新了我的答案,請再看一遍。 – mertyildiran

回答

0

使用imap_qprint

<?php 

$header = imap_header($imap, $email_number); 
$from = $header->from; 
$print_sender = $from[0]->personal; 
$print_check = $print_sender[0]->charset; 

$bodymsg = imap_qprint(imap_fetchbody($imap, $email_number, 1.2)); 

if (empty($bodymsg)) { 
    $bodymsg = imap_qprint(imap_fetchbody($imap, $email_number, 1)); 
} 

if($print_check == "ISO-8859-9") { 
    $print_sender = mb_convert_encoding($print_sender, "UTF-8", "ISO-8859-9");//Encoding process 
    $bodymsg = mb_convert_encoding($bodymsg, "UTF-8", "ISO-8859-9");//Encoding process 
} 

echo $print_sender; 
echo $bodymsg; 

?> 
+0

謝謝,您的代碼確實會轉換內容,但它不保留格式,它似乎只將它轉換爲文本。 – LeRoux

+0

也許它不在1.2 :) – Max

+0

@LeRoux儘量不要包含'mb_convert_encoding'部分? – mertyildiran