在php中使用imap通过gmail获取邮件时,正文内容无法正确显示

在php中使用imap通过gmail获取邮件时,正文内容无法正确显示,php,imap,gmail-imap,Php,Imap,Gmail Imap,我使用此代码显示正文内容和附件。它工作正常,只是身体的内容显示不正常 <?php ini_set('max_execution_time', 0); require_once('front_include/connect.inc.php'); $mail = new imap_mail(); $configuration_data = $mail->get_configuration(); $hostname = $configuration_data['host_nam

我使用此代码显示正文内容和附件。它工作正常,只是身体的内容显示不正常

<?php 
ini_set('max_execution_time', 0); 
require_once('front_include/connect.inc.php');
$mail = new imap_mail();
$configuration_data = $mail->get_configuration();
    $hostname = $configuration_data['host_name'];
    $username = $configuration_data['user_name'];
    $password = $configuration_data['password'];
    /* 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) {
          /* begin output var */
          $output = '';
          /* put the newest emails on top */
          rsort($emails);
          /* for every email... */
          $m=1;
          foreach($emails as $email_number) {
                /* output the email body */
                $message = imap_fetchbody($inbox,$email_number,2);

                //$output.= '<div class="body">'.utf8_encode(quoted_printable_decode($message)).'</div>';

                // start for detail
                $header = imap_header($inbox, $email_number);
                //print_r($header);
                $email[$m]['from'] = $header->from[0]->mailbox.'@'.$header->from[0]->host;
                $email[$m]['fromaddress'] = $header->from[0]->personal;
                $email[$m]['to'] = $header->to[0]->mailbox;
                $email[$m]['subject'] = $header->subject.'</br>';
                $email[$m]['message_id'] = $header->Msgno;
                $email[$m]['date'] = $header->MailDate;             

                // Start for attachment
                $structure = imap_fetchstructure($inbox, $email_number);
                $message = imap_fetchbody($inbox,$email_number,2);
                echo $message.'First';

                $message2 = imap_fetchbody($inbox,$email_number,1.2);
                echo $message2.'Second';

                $attachments = array();
                if(isset($structure->parts) && count($structure->parts)) {

                if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
                    $part = $structure->parts[1];
                    $message1 = imap_fetchbody($inbox,$email_number,2);

                    if($part->encoding == 3) {
                        $message1 = imap_base64($message1);
                    } else if($part->encoding == 1) {
                        $message1 = imap_8bit($message1);
                    } else {
                        $message1 = imap_qprint($message1);
                    }
                    echo $message1.'Hello';
                }


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

                }
                /********* insert data in databse ***********/
                $mail->mail_insert($email[$m]['from'],$email[$m]['fromaddress'],$email[$m]['to'],$email[$m]['subject'],$email[$m]['date'],$email[$m]['message_id'],$message);
                $mail_id = mysql_insert_id();
                /********* insert data in databse ***********/

                foreach ($attachments as $key => $attachment) {
                  if($attachment['name'] != '')
                  {
                    $name = $header->Msgno.date('d-m-y').time().$attachment['name'];
                    $display_name = $attachment['name'];
                    $contents = $attachment['attachment'];
                    $mail->attachment_insert($header->Msgno,$mail_id,$display_name,$name);
                    file_put_contents($name, $contents);
                  }
                }
            $m++;   
          }
    } 
    /* close the connection */
imap_close($inbox);
?>
但有时它不工作,为什么这个问题是创造。 我尝试了三种类型的显示正文内容,但没有一种显示正文内容是正确的。 有谁能建议我如何正确显示消息的正文内容吗。 请帮帮我

提前谢谢

    $message = imap_fetchbody($inbox,$email_number, 1.2);
    $message = imap_fetchbody($inbox,$email_number, 1.2);