微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在编写 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,1);
                                        $messageExcerpt = substr($message,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,$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);
                                            }
                                        }

当前消息示例

enter image description here

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。