title: PHP Imap Body Decode Issue
body: <p>I have copied the following code from <a href="https://stackoverflow.com/questions/15539902/php-imap-decoding-messages">PHP IMAP decoding messages</a>, but I am still struggling with the implementation. This is how I have implemented this:</p>
  
  <pre><code>public function readMessage($imap,$email_number){
  
      $message['header'] = imap_headerinfo($imap,$email_number,0);
      $message['overview'] = imap_fetch_overview($imap,$email_number,0);
      //$message['body'] = imap_fetchbody($imap,$email_number,2);
  
      // Get the message body.
      $body = imap_fetchbody($imap, $email_number, 1.2);
      if (!strlen($body) &gt; 0) {
          $body = imap_fetchbody($imap, $email_number, 1);
      }
  
      // Get the message body encoding.
      $encoding = $this-&gt;getEncodingType($imap,$email_number);
  
      // Decode body into plaintext (8bit, 7bit, and binary are exempt).
      if ($encoding == 'BASE64') {
          $message['body'] = $this-&gt;decodeBase64($body);
      }
      elseif ($encoding == 'QUOTED-PRINTABLE') {
          $message['body'] = $this-&gt;decodeQuotedPrintable($body);
      }
      elseif ($encoding == '8BIT') {
          $message['body'] = $this-&gt;decode8Bit($body);
      }
      elseif ($encoding == '7BIT') {
          $message['body'] = $this-&gt;decode7Bit($body);
      }
  
      return $message;
  }
  </code></pre>
  
  <p>For some reason the getEncodingType call always returns the format as 7bit which causes issues when displaying the text and shows garbage. Here is that function:</p>
  
  <pre><code>public function getEncodingType($imap, $messageId, $numeric = false) {
      // See imap_fetchstructure() documentation for explanation.
      $encodings = array(
        0 =&gt; '7BIT',
        1 =&gt; '8BIT',
        2 =&gt; 'BINARY',
        3 =&gt; 'BASE64',
        4 =&gt; 'QUOTED-PRINTABLE',
        5 =&gt; 'OTHER',
      );
      // Get the structure of the message.
      $structure = $this-&gt;getStructure($imap, $messageId);
      // Return a number or a string, depending on the $numeric value.
      if ($numeric) {
          return $structure-&gt;encoding;
      } else {
          return $encodings[$structure-&gt;encoding];
      }
  }
  </code></pre>
  
  <p>Can you perhaps point out a few things that I am doing wrong?</p>