具有嵌入式图像的IMAP PHP未在localhost上显示Codeigniter

问题描述 投票:3回答:1

我使用codeigniter和imap php目前正在开发项目xampp。由于某种原因,他们嵌入的图像没有显示

在我的getBody()函数中,我有这个回调

$body = preg_replace_callback(
        '/src="cid:(.*)">/Uims',
        function($m) use($email, $uid){
            //split on @
            $parts = explode('@', $m[1]);
            //replace local path with absolute path
            $img = str_replace($parts[0], '', $parts[0]);
            return "src='$img'>";
       },
    $body);

enter image description here

enter image description here

我收到错误

enter image description here

问题:如何确保文本/ html正文等图像正确?

<?php

class Email extends MY_Controller {

    private $enc;
    private $host;
    private $user;
    private $pass;
    private $mailbox;
    private $mbox;

    public function __construct() {
        parent::__construct();

        $this->enc = '/imap/ssl/novalidate-cert';
        $this->host = '****';
        $this->user = '****'; // email
        $this->pass = '****'; // Pass

        $this->mailbox = '{' . $this->host . $this->enc . '}';
        $this->mbox = imap_open($this->mailbox, $this->user, $this->pass);
    }

    public function view() {

        $this->data['message'] = $this->getBody($this->uri->segment(4));

        $this->load->view('template/common/header', $this->data);
        $this->load->view('template/common/nav', $this->data);
        $this->load->view('template/mail/view', $this->data);
        $this->load->view('template/common/footer', $this->data);

        imap_close($this->mbox);
    }

    public function getBody($uid) {
        $body = $this->get_part($uid, "TEXT/HTML");
        // if HTML body is empty, try getting text body
        if ($body == "") {
            $body = $this->get_part($uid, "TEXT/PLAIN");
        }

        $email = $this->user;

        //replace cid with full path to image
        $body = preg_replace_callback(
            '/src="cid:(.*)">/Uims',
            function($m) use($email, $uid){
                //split on @
                $parts = explode('@', $m[1]);
                //replace local path with absolute path
                $img = str_replace($parts[0], '', $parts[0]);
                return "src='$img'>";
           },
        $body);

        return trim(utf8_encode(quoted_printable_decode($body)));
    }    

    private function get_part($uid, $mimetype, $structure = false, $partNumber = false) {
        if (!$structure) {
               $structure = imap_fetchstructure($this->mbox, $uid);
        }
        if ($structure) {
            if ($mimetype == $this->get_mime_type($structure)) {
                if (!$partNumber) {
                    $partNumber = 1;
                }
                $text = imap_fetchbody($this->mbox, $uid, $partNumber, FT_PEEK);
                switch ($structure->encoding) {
                    # 7BIT
                    case 0:
                        return imap_qprint($text);
                    # 8BIT
                    case 1:
                        return imap_8bit($text);
                    # BINARY
                    case 2:
                        return imap_binary($text);
                    # BASE64
                    case 3:
                        return imap_base64($text);
                    # QUOTED-PRINTABLE
                    case 4:
                        return quoted_printable_decode($text);
                    # OTHER
                    case 5:
                        return $text;
                    # UNKNOWN
                    default:
                        return $text;
                }
           }
            // multipart
            if ($structure->type == 1) {
                foreach ($structure->parts as $index => $subStruct) {
                    $prefix = "";
                    if ($partNumber) {
                        $prefix = $partNumber . ".";
                    }
                    $data = $this->get_part($uid, $mimetype, $subStruct, $prefix . ($index + 1));
                    if ($data) {
                        return $data;
                    }
                }
            }
        }
        return false;
    }

    private function get_mime_type($structure) {
        $primaryMimetype = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
        if ($structure->subtype) {
           return $primaryMimetype[(int)$structure->type] . "/" . $structure->subtype;
        }
        return "TEXT/PLAIN";
    }

}
php codeigniter imap php-imap
1个回答
1
投票

介绍

您正在尝试将电子邮件转换为HTML页面。

电子邮件有多个部分:

  1. 基于文字的电邮
  2. HTML电子邮件
  3. 附件

在标题中,您将找到Message-ID以及其他相关元数据。

要将电子邮件转换为网站,您必须将HTML和附件公开给浏览器。

每个部件都有自己的标头。当你有一个url='cid:Whatever'时,你必须查找电子邮件的哪一部分有Content-Id标题。

将电子邮件作为网页提供

您需要找到包含HTML正文的电子邮件部分。解析它并替换你已经实现该部分的http://yourhost/{emailId}的CID URL,所以我不会在这里添加如何做到这一点。

替换HTML上的CID URL - 实现

这是一个可能适合您的原型。

$mailHTML="<html>All your long code here</html>";
$mailId="email.eml";
$generatePartURL=function ($messgeId, $cid) {
    return "http://myhost/".$messgeId."/".$cid; //Adapt this url according to your needs
};
$replace=function ($matches) use ($generatePartURL, $mailId) {
    list($uri, $cid) = $matches;
    return $generatePartURL($mailId, $cid);
};
$mailHTML=preg_replace_callback("/cid:([^'\" \]]*)/", $replace, $mailHTML);

通过CID查找零件

http://yourhost/{emailId}/{cid}

伪代码:

  • 加载邮箱
  • 通过CID查找零件
  • 从Base64或其他使用的编码解码(检查Content-Transfer-Encoding标头)
  • 将文件作为HTTP响应服务

哪部分有我的CID图片?

迭代所有查找与您的CID值匹配的Content-ID标头的电子邮件部分。

--_part_boundery_
Content-Type: image/jpeg; name="filename.jpg"
Content-Description: filename.jpg
Content-Disposition: inline; filename="filename.jpg"; size=39619; creation-date="Thu, 28 Dec 2017 10:53:51 GMT"; modification-date="Thu, 28 Dec 2017 10:53:51 GMT"
Content-ID: <YOUR CID WILL BE HERE>
Content-Transfer-Encoding: base64

解码传输编码并将内容作为常规http文件提供。

用PHP实现的Webmail

RoundCube是一个用PHP实现的webmail,你可以看看它们是如何做到的:https://github.com/roundcube/roundcubemail/blob/master/program/lib/Roundcube/rcube_washtml.php

注意:我的代码不是基于此解决方案。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.