【问题标题】:PHP Imap Body Decode IssuePHP Imap 正文解码问题
【发布时间】:2017-04-22 02:24:27
【问题描述】:

我从PHP IMAP decoding messages 复制了以下代码,但我仍在努力实现。我是这样实现的:

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];
    }
}

您能否指出我做错的一些事情?

【问题讨论】:

  • 对不起,愚蠢的变量名称来自我的高中时代。我已经更新了我的答案,请再看一遍。

标签: php imap


【解决方案1】:

使用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;

?>

【讨论】:

  • 谢谢,您的代码确实转换了内容,但没有保留格式,似乎只是将其转换为文本。
  • 可能不在 1.2 部分 :)
  • @LeRoux 尽量不包含mb_convert_encoding 部分?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 2013-03-26
  • 1970-01-01
  • 2013-03-10
  • 2011-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多