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

时间:2013-11-22 18:20:06

标签: 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_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,2);

但是有时它不能解决为什么会出现这个问题。 我尝试了三种3种显示体内容但没有一种显示体内容正确。 任何人都可以建议我如何正确显示消息的正文内容。 请帮帮我......

先谢谢。

1 个答案:

答案 0 :(得分:0)

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