Q:

How to fetch the gmail message body with php and imap

I am using IMAP to retrieve the gmail message , actually the messages are coming from a data-logger after a continuous time of 1 hour interval when i am trying to fetching the email body it is showing the encoded body like "VVNSOlNpdGUwMV82LDAsNDksVHJ5Q291bnQ9MSxGVFBTdWNjZXNzPS0x " and the orignal text is "USR:Site01_6,0,49,TryCount=1,FTPSuccess=-1", 
if i copy the text manualy and sent an email from my other email then i can fetching this as same as orignal , can't understanding where is the problem ,, here is the code i am using for this , 
public function fetch_gmail_inbox()
    {

        $res=array();
        /* connect to gmail */
        $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
        $username = 'clnvsdty@gmail.com';
        $password = 'Solarisgr8';

        /* try to connect */
        $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

        /* grab emails */
        $emails = imap_search($inbox,'UNSEEN');

        /* if emails are returned, cycle through each... */
        if($emails) {

            /* put the newest emails on top */
            rsort($emails);

            /* for every email... */
            foreach($emails as $email_number) {

                /* get information specific to this email */
                $overview = imap_fetch_overview($inbox,$email_number,0);
                $message =imap_fetchbody($inbox,$email_number,1);
                 $structure = imap_fetchstructure($inbox,$email_number);

                if($structure->parts[0]->encoding == 3 ||$structure->encoding == 3 )
                {
                     $message=imap_base64($message);

                }
                if($structure->parts[0]->encoding == 4 ||$structure->encoding == 4) 
                {
                    $message = imap_qprint($message);
                }
                $message2= quoted_printable_decode(imap_fetchbody($inbox,$email_number,0));
                $date=explode(':',$message2);
                $date2= date('d-m-Y h:i:s',strtotime($date[8].':00:00'));

                $sub=$string = preg_replace('/\s+/', '', $overview[0]->subject);
                 $tomatch=preg_replace('/\s+/', '', "USR:Site01_Comms Complete");

                if(strcmp($sub,$tomatch)==0)
                {

                    $res['date']=$date2;
                    $res['body']=$message;
                }
            }

            return $res;
        } 

        /* close the connection */
        imap_close($inbox);

    }

any help will be appreciated 

A:

try using this 
function fetch_gmail_inbox($check='UNSEEN')
    {

        $res=array();
        /* connect to gmail */
        $hostname = '{mail.enlighten-energy.net:143/imap/novalidate-cert}INBOX';
        $username = Yii::app()->getModule('user')->get_config('datalogger_email');
        $password = Yii::app()->getModule('user')->get_config('datalogger_email_pwd');

        /* try to connect */
        $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

        /* grab emails */
        $emails = imap_search($inbox,$check);
        /* if emails are returned, cycle through each... */
        if($emails) {

            /* put the newest emails on top */
            rsort($emails);

            /* for every email... */
            $data3=array();
            foreach($emails as $email_number)
             {
                //print_r($email_number); echo "#####";
                /* get information specific to this email */
                $overview = imap_fetch_overview($inbox,$email_number,0);
                //echo "<pre>";print_r($overview);
                $message = quoted_printable_decode(imap_fetchbody($inbox,$email_number,1));
                 $structure = imap_fetchstructure($inbox,$email_number);

                 $attachments = array();
                 if(isset($structure->parts) && count($structure->parts)) 
                   {
                      for($i = 0; $i < count($structure->parts); $i++) 
                      {
                         $attachments[$i] = array(
                             'is_attachment' => false,
                             'filename' => '',
                             'name' => '',
                             'attachment' => ''
                         );

                         if($structure->parts[$i]->ifdparameters) 
                         {
                             foreach($structure->parts[$i]->dparameters as $object) 
                             {
                                 if(strtolower($object->attribute) == 'filename') 
                                 {
                                     $attachments[$i]['is_attachment'] = true;
                                     $attachments[$i]['filename'] = $object->value;
                                 }
                             }
                         }

                         if($structure->parts[$i]->ifparameters) 
                         {
                             foreach($structure->parts[$i]->parameters as $object) 
                             {
                                 if(strtolower($object->attribute) == 'name') 
                                 {
                                     $attachments[$i]['is_attachment'] = true;
                                     $attachments[$i]['name'] = $object->value;
                                 }
                             }
                         }

                         if($attachments[$i]['is_attachment']) 
                         {
                             $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);

                             /* 4 = QUOTED-PRINTABLE encoding */
                             if($structure->parts[$i]->encoding == 3) 
                             { 
                                 $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                             }
                             /* 3 = BASE64 encoding */
                             elseif($structure->parts[$i]->encoding == 4) 
                             { 
                                 $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                             }
                         }
                      }
                   }

                  $data2=array();
                 foreach($attachments as $attachment)
                   {
                      if($attachment['is_attachment'] == 1)
                      {
                        $filename = $attachment['name'];
                         if(empty($filename)) $filename = $attachment['filename'];

                         if(empty($filename)) $filename = time() . ".dat";
                        $filename=str_replace('/','',$filename);
                          $filename="uploads/gmail_attachment/".$email_number . "-" . $filename;
                         $fp = fopen($filename, "w+");

                         if(fwrite($fp, $attachment['attachment']))
                         {
                            $data=self::fetch_xls_data($email_number,$filename);

                         }
                         fclose($fp);
                      }
                      $data2[$email_number]=$data;
                    unlink($filename);
                   }
                  $data3[$email_number]=$data2[$email_number];
            }

            return $data3;
        }

        /* close the connection */
        imap_close($inbox);

    //return $data;

    }

