How to fetch a gmail email and display it as it is shown in gmail
<p>I made a program to using php to download email from gmail and save to html file.
everything works file, its connect, downloading mail, downloading attachment, downloading embedded mails and it is also saving to html files and all works fine BUT only SOME mails comes like:</p>

<p><strong>Example 1:</strong>
--_008_8BDE32B1FDB5C947B54F92503EC9934F015611E6glmrsvrmail1gil_ Content-Type: multipart/alternative; boundary="_000_8BDE32B1FDB5C947B54F92503EC9934F015611E6glmrsvrmail1gil_" --_000_8BDE32B1FDB5C947B54F92503EC9934F015611E6glmrsvrmail1gil_ Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: base64 RGVhciBSb3NlLA0KDQpHcmVldGluZ3MgZnJvbSBHaWxpIExhbmthbmZ1c2hp IE1hbGRpdmVzIQ0KDQpXZSBhcmUgZGVsaWdodGVkIHRvIGNvbmZpcm0geW91 </p>

<p><strong>Example 2:</strong>
in Gmail it shows with colorful and HTML but here it will come all the text in one like but no html &amp; color</p>

<p><strong>Example 3:</strong>
N§’çè®êßjËZžÌ®(î²ÜžÛh’z0†Œ&amp;¹ÈpŠY\¢Ëf{‰à†+</p>

<p><strong>Example 4:</strong>
------_=_NextPart_002_01CE2076.4A2A4F64 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: base64 RGVhciBSb3NlLA0KDQogDQoNClRoaXMgaXMgbXkgb2ZmaWNlIGVtYWlsIGFkZHJlc3MuIEVuY2xv c2VkIGRvY3VtZW50cyBhcyByZXF1aXJlZC4gQWxzbyBlbmNsb3NlZCBteSBjb25maXJtZWQgdGlj a2V0IGZvciB5b3VyIGZ1cnRoZXIgYWN0aW9ucywgcmVxdWVzdCB5b3UgdG8gYXJyYW5nZSBhaXJw </p>

<p>I am using the following way top download the mail, please it is coming for only some mails
NOTE: I have no issue with attachments.. </p>

<pre><code>function read_all_parts($mbox,$uid)
{
global $mime,$ret_info,$enc;
$mime = array("text","multipart","message","application","audio","image","video","other","unknow");
$enc  = array("7BIT","8BIT","BINARY","BASE64","QUOTED-PRINTABLE","OTHER","base64");
$struct = imap_fetchstructure($mbox, $uid );
$ret_info = array();


$ret_info = $this-&gt;scan($struct-&gt;parts,0);
return $ret_info;
}

$structure = imap_fetchstructure($mbox, $message_id);
$mailDivided= $mailObj-&gt;read_all_parts($mbox,$message_id);

if(count($mailDivided)!=0){
    /* ////////////////////////////////////////////////////////////////////// */
    $plainText = 1;
    foreach ($mailDivided as $key=&gt;$parts)  {
        if($parts['pid']=='1.2' &amp;&amp; $parts['encoding']=='QUOTED-PRINTABLE'):
            $plainText = '1.2'; # which means this is contains html text
        endif;

        if($parts['pid']=='1.2' &amp;&amp; $parts['encoding']=='7BIT'):
            $plainText = '1.2'; # which means this is contains html text
        endif;

        if($parts['pid']=='1.2' &amp;&amp; $parts['encoding']=='8BIT'):
            $plainText = '1.2'; # which means this is contains html text
        endif;

        if($parts['pid']=='1.1.2' &amp;&amp; $parts['encoding']=='QUOTED-PRINTABLE'):
            $plainText = '1.1.2'; # which means this is contains html text
        endif;

        if($parts['pid']=='2' &amp;&amp; $parts['encoding']=='QUOTED-PRINTABLE'):
            $plainText = '2'; # which means this is contains html text
        endif;
    }
    /* ////////////////////////////////////////////////////////////////////// */
    /* ////////////////////////////////////////////////////////////////////// */
    $mymail = imap_fetchbody($mbox,$message_id,$plainText);
    if(imap_base64($mymail)){
        print "This is imap_base64 &lt;br&gt;";
        $mymailBody = imap_base64($mymail);
        $mymailBody =  nl2br($mymailBody);

    }else if (imap_qprint($mymail)){
        print "This is imap_qprint &lt;br&gt;";
        $mymailBody = imap_qprint($mymail);

    }else{
        if ($plainTex==1){
            print "This is nl2br &lt;br&gt;";
            $mymailBody =  nl2br($mymail);
        }else{
            print "This is quoted_printable_decode &lt;br&gt;";
            $mymailBody = quoted_printable_decode($mymail);
        }
    }
    /* ////////////////////////////////////////////////////////////////////// */
}else{
    print "This is encodingMailbody &lt;br&gt;";
    $mymail = imap_fetchbody($mbox,$message_id,'1');
    $mymailBody = $mailObj-&gt;encodingMailbody($mymail,$structure-&gt;encoding);
    $mymailBody = nl2br($mymailBody);
}
</code></pre>

<p>I have been checking and trying all the possible things, nothing is helping me.</p>
