如何使用php和imap获取gmail消息体

时间:2014-11-20 06:52:20

标签: php email gmail imap

我正在使用IMAP来检索gmail消息,实际上这些消息是在连续1小时间隔之后来自数据记录器的,当我试图获取电子邮件正文时它正在显示编码主体,如“VVNSOlNpdGUwMV82LDAsNDksVHJ5Q291bnQ9MSxGVFBTdWNjZXNzPS0x”和原始文本是“USR:Site01_6,0,49,TryCount = 1,FTPSuccess = -1”,

如果我手动复制文本并从我的其他电子邮件发送电子邮件然后我可以像orignal一样获取此信息,无法理解问题出在哪里,这里是我正在使用的代码,

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);



    }

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

尝试使用此

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;

    }