我只是使用以下代码对7bit编码的消息进行解码。它正在处理某些消息,但有些消息显示仍然显示加密的encrypted.some消息。如何正确解码此消息?有人请帮帮我。
public function decode7Bit($text) {
// actually base64-encoded, and decode it.
if($this->_isEncodedBase64($text)){
$text = base64_decode($text);
}
// Manually convert common encoded characters into their UTF-8 equivalents.
$characters = array(
'=20' => ' ', // space.
'=E2=80=99' => "'", // single quote.
'=0A' => "\r\n", // line break.
'=A0' => ' ', // non-breaking space.
'=C2=A0' => ' ', // non-breaking space.
"=\r\n" => '', // joined line.
'=E2=80=A6' => '…', // ellipsis.
'=E2=80=A2' => '•', // bullet.
);
// Loop through the encoded characters and replace any that are found.
foreach ($characters as $key => $value) {
$text = str_replace($key, $value, $text);
}
return $text;
}
private function _isEncodedBase64($data){
if ( base64_encode(base64_decode($data)) === $data){
return true;
}
return false;
}
$body = imap_fetchbody($inbox, $messageId, 1.2);
if (!strlen($body) > 0) {
$body = imap_fetchbody($inbox, $messageId, 1);
}
// Get the raw headers.
$raw_header = imap_fetchheader($inbox, $messageId);
// Get the message body.
$body = imap_fetchbody($inbox, $messageId, 1.2);
if (!strlen($body) > 0) {
$body = imap_fetchbody($inbox, $messageId, 1);
}
// print_r($body);
// Get the message body encoding.
$encodings = array(
0 => '7BIT',
1 => '8BIT',
2 => 'BINARY',
3 => 'BASE64',
4 => 'QUOTED-PRINTABLE',
5 => 'OTHER',
);
// Get the structure of the message.
$structure = imap_fetchstructure($inbox, $messageId);
// Return a number or a string, depending on the $numeric value.
if ($numeric) {
$encoding =$structure->encoding;
} else {
$encoding= $encodings[$structure->encoding];
}
// Decode body into plaintext (8bit, 7bit, and binary are exempt).
if ($encoding == 'BASE64') {
$body = imap_base64($body);
}
elseif ($encoding == 'QUOTED-PRINTABLE') {
$body = quoted_printable_decode($body);
}
elseif ($encoding == '8BIT') {
$body = quoted_printable_decode(imap_8bit($body));
}
elseif ($encoding == '7BIT') {
$body = $this->decode7Bit($body);
}
else{
$body = $body;
}