How to decode a 7BIT coded message using PHP?

601 Views Asked by At

I am using the imap extension that is built in PHP to read messages from an imap email and parse out the message and add it to a database of another system.

The problem is that the message come encoded. so I will have to first decode it and then add it to the database.

I can use the imap_fetchstructure() to find out what type of encoding is the message encoded with and from them I should be able to decode it.

so here is what I have done

$struct = imap_fetchstructure($this->conn, $id, 0);

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

    private function _decodeMessage($msg, $type){

        if($type == 0){
            return mb_convert_encoding($msg, "UTF-8", "auto");
        } elseif($type == 1){
            return imap_8bit($msg); //imap_utf8
        } elseif($type == 2){
            return imap_base64(imap_binary($msg));
        } elseif($type == 3){
            return imap_base64($msg);
        } elseif($type == 4){
            return imap_qprint($msg);
            //return quoted_printable_decode($msg);
        } else {
            return $msg;
        }
    }

The problem that I am having is some messages that are encoded with 7BIT are not being decoded correctly and I still see the messages encrypted when I print the $message

How can I correctly decode the messages when it's type is 7BIT?

0

There are 0 best solutions below