簡體   English   中英

如何使用 PHP 真正解碼 7 位 email 消息?

[英]How to really decode a 7Bit email message using PHP?

我有代碼從服務器讀取電子郵件,然后在將數據插入數據庫之前解析它們。

我正在使用 PHP 中的IMAP 擴展來幫助我解決這個問題。

這是我正在做的讀取數據的事情

//read new messages
private function _getNewMessages(){

    // Checks the inbox
    if ($messages = imap_search($this->conn,'ALL'))
    {
        // Sorts the messages newest first
        sort($messages);

        // Loops through the messages
        foreach ($messages as $id)
        {
            // Grabs the overview and body
            //$overview = imap_fetch_overview($this->conn, $id, 0);
            $struct = imap_fetchstructure($this->conn, $id, 0);
            $header = imap_headerinfo($this->conn, $id);
            $message = imap_fetchbody($this->conn, $id, 1);

            //decode the message
            if(isset($struct->encoding)){
                $message = $this->_decodeMessage($message, $struct->encoding);
            }


            $from = $header->from[0]->mailbox . '@' . $header->from[0]->host;
            $subject = $header->subject;    
            echo  $message;
        }
    }
    else
    {
        exit('No messages to process');
    }
}

我看到的問題是,當消息以值 0“7 位”編碼時,它返回黑色。 解碼似乎無法正確解碼消息。

我正在使用這個 function 來解碼 7 位

    // function to decode 7BIT encoded message
    private function _decode7Bit($text) {
        // If there are no spaces on the first line, assume that the body is
        // actually base64-encoded, and decode it.
        $lines = explode("\r\n", $text);
        $first_line_words = explode(' ', $lines[0]);
        if ($first_line_words[0] == $lines[0]) {
            $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;
    }

我也試過這種方法來解碼消息

/**
 * decoding 7bit strings to ASCII
 * @param string $text
 * @return string
 */
function decode7bit($text){
        $ret = '';
        $data = str_split(pack('H*', $text));

        $mask = 0xFF;
        $shift = 0;
        $carry = 0;
        foreach ($data as $char) {
                if ($shift == 7) {
                        $ret .= chr($carry);
                        $carry = 0;
                        $shift = 0;
                }

                $a      =       ($mask >> ($shift+1)) & 0xFF;
                $b      =       $a ^ 0xFF;

                $digit = ($carry) | ((ord($char) & $a) << ($shift)) & 0xFF;
                $carry = (ord($char) & $b) >> (7-$shift);
                $ret .= chr($digit);

                $shift++;
        }
        if ($carry) $ret .= chr($carry);
        return $ret;
}

但消息是空白的。

我在這里做錯了什么? 我能做些什么來確保消息被正確解碼?

謝謝

我解決了這個問題。 我檢查郵件是否為base64編碼的方式不好。 所以我改變了這個功能

private function _isEncodedBase64($date){

        if ( base64_encode(base64_decode($data)) === $data){
            return true;
        }

        return false;
    }

    // function to decode 7BIT encoded message
    private function _decode7Bit($text) {
        // If there are no spaces on the first line, assume that the body is
        // 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;
    }

根據您描述的字符編碼,看起來您的文本是使用帶引號的printable編碼的。

嘗試使用以下方法解碼:

echo quoted_printable_decode($text);

不必要的代碼。 只需在 php 中使用 quoted_printable_decode 即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM