Я скопировал следующий код от PHP IMAP decoding messages, но я все еще боюсь реализации. Это, как я реализовал это:PHP Imap Body Decode Issue
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 всегда возвращает формат, 7bit, который вызывает проблемы при отображении текста и показывает мусор. Вот эта функция:
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];
}
}
Возможно, вы можете указать на некоторые вещи, которые я делаю неправильно?
Извините за глупые имена переменных это было от моих лет средней школы. Я обновил свой ответ, пожалуйста, взгляните еще раз. – mertyildiran