title: IMAP PHP with embedded images not showing Codeigniter on localhost
body: <p>I use codeigniter and imap php currently developing project xampp. For some reason them embedded images not showing.</p>
  
  <p>In my getBody() function I have this call back</p>
  
  <pre><code>$body = preg_replace_callback(
          '/src="cid:(.*)"&gt;/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'&gt;";
         },
      $body);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Yrv3R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yrv3R.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/7WchX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7WchX.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>I get error</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/rgtcR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rgtcR.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p><strong>Question:</strong> How can I make sure it gets the images correct for the text/html body etc.</p>
  </blockquote>
  
  <pre><code>&lt;?php
  
  class Email extends MY_Controller {
  
      private $enc;
      private $host;
      private $user;
      private $pass;
      private $mailbox;
      private $mbox;
  
      public function __construct() {
          parent::__construct();
  
          $this-&gt;enc = '/imap/ssl/novalidate-cert';
          $this-&gt;host = '****';
          $this-&gt;user = '****'; // email
          $this-&gt;pass = '****'; // Pass
  
          $this-&gt;mailbox = '{' . $this-&gt;host . $this-&gt;enc . '}';
          $this-&gt;mbox = imap_open($this-&gt;mailbox, $this-&gt;user, $this-&gt;pass);
      }
  
      public function view() {
  
          $this-&gt;data['message'] = $this-&gt;getBody($this-&gt;uri-&gt;segment(4));
  
          $this-&gt;load-&gt;view('template/common/header', $this-&gt;data);
          $this-&gt;load-&gt;view('template/common/nav', $this-&gt;data);
          $this-&gt;load-&gt;view('template/mail/view', $this-&gt;data);
          $this-&gt;load-&gt;view('template/common/footer', $this-&gt;data);
  
          imap_close($this-&gt;mbox);
      }
  
      public function getBody($uid) {
          $body = $this-&gt;get_part($uid, "TEXT/HTML");
          // if HTML body is empty, try getting text body
          if ($body == "") {
              $body = $this-&gt;get_part($uid, "TEXT/PLAIN");
          }
  
          $email = $this-&gt;user;
  
          //replace cid with full path to image
          $body = preg_replace_callback(
              '/src="cid:(.*)"&gt;/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'&gt;";
             },
          $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-&gt;mbox, $uid);
          }
          if ($structure) {
              if ($mimetype == $this-&gt;get_mime_type($structure)) {
                  if (!$partNumber) {
                      $partNumber = 1;
                  }
                  $text = imap_fetchbody($this-&gt;mbox, $uid, $partNumber, FT_PEEK);
                  switch ($structure-&gt;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-&gt;type == 1) {
                  foreach ($structure-&gt;parts as $index =&gt; $subStruct) {
                      $prefix = "";
                      if ($partNumber) {
                          $prefix = $partNumber . ".";
                      }
                      $data = $this-&gt;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-&gt;subtype) {
             return $primaryMimetype[(int)$structure-&gt;type] . "/" . $structure-&gt;subtype;
          }
          return "TEXT/PLAIN";
      }
  
  }
  </code></pre>