如何在php中使用imap显示或解码gmail内联图像或消息

如何在php中使用imap显示或解码gmail内联图像或消息,php,email,gmail,imap,Php,Email,Gmail,Imap,我正在编写php脚本如何在php中使用imap显示或解码gmail内联图像或消息,目前它显示的是decode code,而不是内联图像 我如何使用imap_fetchbody检查消息的编码或显示内联图像(已经做了大约两个小时了,所以丢失了),然后正确解码 这里是我的代码 /* Connecting Gmail server with IMAP */ $connection = imap_open('{imap.gmail.com:

我正在编写php脚本如何在php中使用imap显示或解码gmail内联图像或消息,目前它显示的是
decode code
,而不是内联图像

我如何使用imap_fetchbody检查消息的编码或显示内联图像(已经做了大约两个小时了,所以丢失了),然后正确解码

这里是我的代码

 /* Connecting Gmail server with IMAP */
                                $connection = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', "$email", "$password") or die('Cannot connect to Gmail: ' . imap_last_error());

                                /* Search Emails having the specified keyword in the email subject */
                                $emailData = imap_search($connection, 'ALL');



                                if (! empty($emailData)) {
                                    foreach ($emailData as $emailIdent) {


                                        $z++;

                                        $overview = imap_fetch_overview($connection, $emailIdent, 0);
                                        $message = imap_fetchbody($connection, $emailIdent, 1);
                                        $messageExcerpt = substr($message, 0, 150);
                                        $partialMessage = trim(quoted_printable_decode($messageExcerpt));
                                        $date = date("d F, Y", strtotime($overview[0]->date));

                                        //$email_number = imap_msgno($connection,$uid);

                                        $structure = imap_fetchstructure($connection, $emailIdent);

                                        $attachments = array();

                                        /* if any attachments found... */
                                        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($connection, $emailIdent, $i+1);

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

                                        /* iterate through each attachment and save it */
                                        foreach($attachments as $attachment)
                                        {
                                            if($attachment['is_attachment'] == 1)
                                            {
                                                $filename = $attachment['name'];
                                                if(empty($filename)) $filename = $attachment['filename'];

                                                if(empty($filename)) $filename = time() . ".dat";
                                                $folder = "attachment";
                                                if(!is_dir($folder))
                                                {
                                                    mkdir($folder);
                                                }
                                                $fp = fopen("./". $folder ."/". $emailIdent . "-" . $filename, "w+");
                                                fwrite($fp, $attachment['attachment']);
                                                fclose($fp);
                                            }
                                        }
/*使用IMAP连接Gmail服务器*/
$connection=imap_open('{imap.gmail.com:993/imap/ssl/novalidate cert}收件箱',“$email”,“$password”)或die('无法连接到gmail:'.imap_last_error());
/*搜索电子邮件主题中具有指定关键字的电子邮件*/
$emailData=imap_搜索($connection,'ALL');
如果(!empty($emailData)){
foreach($emailData作为$emailIdent){
$z++;
$overview=imap_fetch_overview($connection,$emailIdent,0);
$message=imap_fetchbody($connection,$emailIdent,1);
$messageexecrpt=substr($message,0150);
$partialMessage=trim(引用可打印解码($messageextract));
$date=日期(“df,Y”,strottime($overview[0]->date));
//$email\u number=imap\u msgno($connection,$uid);
$structure=imap_fetchstructure($connection,$emailIdent);
$attachments=array();
/*如果找到任何附件*/
if(设置($structure->parts)和计数($structure->parts))
{
对于($i=0;$iparts);$i++)
{
$attachments[$i]=数组(
'is_attachment'=>false,
'文件名'=>'',
'名称'=>'',
“附件”=>“
);
if($structure->parts[$i]->ifdparameters)
{
foreach($structure->parts[$i]->dparameters as$object)
{
如果(strtolower($object->attribute)='filename')
{
$attachments[$i]['is_attachment']=true;
$attachments[$i]['filename']=$object->value;
}
}
}
如果($structure->parts[$i]->ifparameters)
{
foreach($structure->parts[$i]->参数作为$object)
{
如果(strtolower($object->attribute)='name')
{
$attachments[$i]['is_attachment']=true;
$attachments[$i]['name']=$object->value;
}
}
}
如果($attachments[$i]['is_attachment']))
{
$attachments[$i]['attachment']=imap_fetchbody($connection,$emailIdent,$i+1);
/*3=BASE64编码*/
如果($structure->parts[$i]->encoding==3)
{
$attachments[$i]['attachment']=base64_decode($attachments[$i]['attachment']);
}
/*4=引用的可打印编码*/
elseif($structure->parts[$i]->encoding==4)
{
$attachments[$i]['attachment']=引用的可打印解码($attachments[$i]['attachment']);
}
}
}
}
/*遍历每个附件并保存它*/
foreach($attachments作为$attachment)
{
如果($attachment['is_attachment']==1)
{
$filename=$attachment['name'];
如果(空($filename))$filename=$atta