title: How to really decode a 7Bit email message using PHP?
body: <p>I have code that reads emails from a server and then parse them before inserting the data into a database. </p>
  
  <p>I am using the <a href="http://php.net/manual/en/book.imap.php" rel="nofollow">IMAP extension</a> in PHP to help me with this.</p>
  
  <p>Here is what I am doing to read the data</p>
  
  <pre><code>//read new messages
  private function _getNewMessages(){
  
      // Checks the inbox
      if ($messages = imap_search($this-&gt;conn,'ALL'))
      {
          // Sorts the messages newest first
          sort($messages);
  
          // Loops through the messages
          foreach ($messages as $id)
          {
              // Grabs the overview and body
              //$overview = imap_fetch_overview($this-&gt;conn, $id, 0);
              $struct = imap_fetchstructure($this-&gt;conn, $id, 0);
              $header = imap_headerinfo($this-&gt;conn, $id);
              $message = imap_fetchbody($this-&gt;conn, $id, 1);
  
              //decode the message
              if(isset($struct-&gt;encoding)){
                  $message = $this-&gt;_decodeMessage($message, $struct-&gt;encoding);
              }
  
  
              $from = $header-&gt;from[0]-&gt;mailbox . '@' . $header-&gt;from[0]-&gt;host;
              $subject = $header-&gt;subject;    
              echo  $message;
          }
      }
      else
      {
          exit('No messages to process');
      }
  }
  </code></pre>
  
  <p>the issue that I am seeing is that when the message come encoded with the value 0 "7 Bit" it is returned black. it seems that the decoding is failing to decode the message properly.</p>
  
  <p>I am using this function to Decode the 7 Bit</p>
  
  <pre><code>    // function to decode 7BIT encoded message
      private function _decode7Bit($text) {
          // If there are no spaces on the first line, assume that the body is
          // actually base64-encoded, and decode it.
          $lines = explode("\r\n", $text);
          $first_line_words = explode(' ', $lines[0]);
          if ($first_line_words[0] == $lines[0]) {
              $text = base64_decode($text);
          }
  
          // Manually convert common encoded characters into their UTF-8 equivalents.
          $characters = array(
                       '=20' =&gt; ' ', // space.
                       '=E2=80=99' =&gt; "'", // single quote.
                       '=0A' =&gt; "\r\n", // line break.
                       '=A0' =&gt; ' ', // non-breaking space.
                       '=C2=A0' =&gt; ' ', // non-breaking space.
                       "=\r\n" =&gt; '', // joined line.
                       '=E2=80=A6' =&gt; '…', // ellipsis.
                       '=E2=80=A2' =&gt; '•', // bullet.
          );
  
          // Loop through the encoded characters and replace any that are found.
          foreach ($characters as $key =&gt; $value) {
              $text = str_replace($key, $value, $text);
          }
  
          return $text;
      }
  </code></pre>
  
  <p>I have also tried this method to decode the message</p>
  
  <pre><code>/**
   * decoding 7bit strings to ASCII
   * @param string $text
   * @return string
   */
  function decode7bit($text){
          $ret = '';
          $data = str_split(pack('H*', $text));
  
          $mask = 0xFF;
          $shift = 0;
          $carry = 0;
          foreach ($data as $char) {
                  if ($shift == 7) {
                          $ret .= chr($carry);
                          $carry = 0;
                          $shift = 0;
                  }
  
                  $a      =       ($mask &gt;&gt; ($shift+1)) &amp; 0xFF;
                  $b      =       $a ^ 0xFF;
  
                  $digit = ($carry) | ((ord($char) &amp; $a) &lt;&lt; ($shift)) &amp; 0xFF;
                  $carry = (ord($char) &amp; $b) &gt;&gt; (7-$shift);
                  $ret .= chr($digit);
  
                  $shift++;
          }
          if ($carry) $ret .= chr($carry);
          return $ret;
  }
  </code></pre>
  
  <p>but the message is blank.</p>
  
  <p>What am I doing wrong here? What I can do to make sure that the message is decoded properly?</p>
  
  <p>Thank you</p>