Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php和imap获取gmail消息体_Php_Email_Gmail_Imap - Fatal编程技术网

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

如何使用php和imap获取gmail消息体,php,email,gmail,imap,Php,Email,Gmail,Imap,我正在使用IMAP检索gmail消息,实际上消息是在连续1小时的时间间隔后从数据记录器发出的,当我试图获取电子邮件正文时,它显示了编码正文,如VVNSOlNpdGUwMV82LDAsNDksVHJ5Q291bnQ9MSxGVFBTdWNjZXNzPS0x,原始文本是USR:Site01_6,0,49,TryCount=1,ftpsucess=-1 如果我手动复制文本并从我的另一封电子邮件发送电子邮件,那么我可以获取与原始文件相同的文本,无法理解问题出在何处,这是我用于此的代码 public fu

我正在使用IMAP检索gmail消息,实际上消息是在连续1小时的时间间隔后从数据记录器发出的,当我试图获取电子邮件正文时,它显示了编码正文,如VVNSOlNpdGUwMV82LDAsNDksVHJ5Q291bnQ9MSxGVFBTdWNjZXNzPS0x,原始文本是USR:Site01_6,0,49,TryCount=1,ftpsucess=-1

如果我手动复制文本并从我的另一封电子邮件发送电子邮件,那么我可以获取与原始文件相同的文本,无法理解问题出在何处,这是我用于此的代码

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



    }
任何帮助都将不胜感激

请尝试使用此

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;

    }

在这里试试这段代码[使用PHP和IMAP检索你的Gmail电子邮件][1][1]:@FarhanDharsi我也在使用同样的代码。我在网站上尝试过这段代码,它可以工作,而且还有演示版本,它也可以为我工作,请阅读问题是,如果我手动复制文本并从其他电子邮件发送电子邮件,那么我可以获取与原始邮件相同的内容,但如果我试图直接获取这些邮件,则无法获取。我发布了更新的代码,它可用于某些电子邮件,编码的类型是3,所以它在条件下运行,但不是对所有电子邮件进行编码。如果$structure->parts[0]->encoding==3 | |$structure->encoding==3{$message=imap_base64$message;},则它将在以下条件下运行