gk1 / php-mime-mail-parser

Automatically exported from code.google.com/p/php-mime-mail-parser
0 stars 0 forks source link

The problem with base64 encoded text #20

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
Try to parse a message like:
MIME-Version: 1.0
Received: by 10.231.28.9 with HTTP; Wed, 23 Feb 2011 09:23:40 -0800 (PST)
Date: Wed, 23 Feb 2011 22:23:40 +0500
Delivered-To: removed@removed.com
Message-ID: <AANLkTimWMLnNrmfEOS91TKxx6xytSBw-EeeiGJ3a_=vx@mail.gmail.com>
Subject: =?KOI8-R?B?9MXT1A==?=
From: =?KOI8-R?B?6czY0SDrz87P18HMxc7Lzw==?= <removed@removed.com>
To: removed@removed.com
Content-Type: text/plain; charset=KOI8-R
Content-Transfer-Encoding: base64

8NLJ18XJCj4+IMvByyDU2T86CiIiCicnCgouCi4KCi0tIApUaGFuayB5b3UsCklseWEuCg==

What is the expected output? What do you see instead?
Warning: fread() [function.fread]: Length parameter must be greater than 0 in 
/home/***/MimeMailParser.class.php on line 369

Fatal error: Call to private method MimeMailParser::decode() from context '' in 
/home/****.php on line 47

What version of the product are you using? On what operating system?
I guess r22, CentOS

This is an example of the email sent by Google. This particular example can not 
process your class. I think this is due to base64.

Original issue reported on code.google.com by ilyakono...@gmail.com on 23 Feb 2011 at 6:28

GoogleCodeExporter commented 8 years ago
Any updates?

Original comment by ilyakono...@gmail.com on 5 Mar 2011 at 11:42

GoogleCodeExporter commented 8 years ago
I too face same kind of some problem. But not sure whether it is BASE64 
encoding or not. I receive garbage characters as Subject 
"J֠y�����nW�y�b���������" What can i do? 
Help Please?

Original comment by arunkuma...@gmail.com on 21 Apr 2011 at 5:45

GoogleCodeExporter commented 8 years ago
same problem

Original comment by epicgo...@gmail.com on 27 Feb 2012 at 12:41

GoogleCodeExporter commented 8 years ago
Same problem being faced

Original comment by m4ma...@gmail.com on 20 Mar 2012 at 7:32

GoogleCodeExporter commented 8 years ago
Same issue

Original comment by martin.a...@lemonhq.com on 2 May 2012 at 9:35

GoogleCodeExporter commented 8 years ago
You can get the decoded content of that message using:

$to = iconv_mime_decode($Parser->getHeader('to'), 0, "UTF-8");
$from = iconv_mime_decode($Parser->getHeader('from'), 0, "UTF-8");
$subject =  iconv_mime_decode($Parser->getHeader('subject'), 0, "UTF-8");
$html = iconv($Parser->getMessageEncoding('html'), "UTF-8", 
$Parser->getMessageBody('html'));
$text = iconv($Parser->getMessageEncoding('text'), "UTF-8", 
$Parser->getMessageBody('text'));
$attachments = $Parser->getAttachments();

You have to add this to MimeMailParser.class.php:

        public function getMessageEncoding($type = 'text', $attach = "NO") {
                $body = false;
                $mime_types = array(
                        'text'=> 'text/plain',
                        'html'=> 'text/html'
                );
                if (in_array($type, array_keys($mime_types))) {
                        foreach($this->parts as $part) {
                                if ($this->getPartContentType($part) == $mime_types[$type]) {
                    $headers = $this->getPartHeaders($part);
                                   switch($attach) {
                                     case "NO":
                                        if((!substr(trim($headers['content-disposition']),0,10) == "attachment"))
                                        $body .= $this->getPartContentCharset($part);
                                        break;
                                     case "ONLY":
                                        if((substr(trim($headers['content-disposition']),0,10) == "attachment"))
                                        $body .= $this->getPartContentCharset($part);
                                        break;
                                     default: // YES
                                        $body .= $this->getPartContentCharset($part);
                                        break;
                                   };// switch($atatch)
                                }
                        }
                } else {
                        throw new Exception('Invalid type specified for MimeMailParser::getMessageBody. "type" can either be text or html.');
                }
                return $body;
        }

I also used a fix from here: 
http://code.google.com/p/php-mime-mail-parser/issues/detail?id=18#c2

Original comment by peter.pe...@gmail.com on 29 Oct 2012 at 8:22

GoogleCodeExporter commented 8 years ago
I forgot to mention that you should also add this to MimeMailParser.class.php:

        private function getPartContentCharset($part) {
                if (isset($part['content-charset'])) {
                        return $part['content-charset'];
                }
                return false;
        }

Original comment by peter.pe...@gmail.com on 29 Oct 2012 at 8:28