title: PHP IMAP decoding messages
body: <p>I have emails sent via base64 encoding and 8bit encoding. I was wondering how I could check the encoding of the message using imap_fetchstructure (been doing this for about two hours, so lost) and then decode it properly.</p>
  
  <p>Gmail and Mailbox (app on iOS) are sending it as 8bit while Windows 8's Mail app is sending it as base64. Either way, I need to decode whether its 8bit or base64 by detecting what type of encoding it has used.</p>
  
  <p>Using PHP 5.1.6 (yes, I should update, been busy).</p>
  
  <p>I really have no code to show. This is all I have:</p>
  
  <pre><code>&lt;?php
  $hostname = '{********:993/imap/ssl}INBOX';
  $username = '*********';
  $password = '******';
  
  $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to server: ' . imap_last_error());
  
  $emails = imap_search($inbox,'ALL');
  
  if($emails) {
  
      $output = '';
  
      rsort($emails);
  
      foreach($emails as $email_number) {
  
          $overview = imap_fetch_overview($inbox,$email_number,0);
          $message = imap_fetchbody($inbox,$email_number,2);
          $struct = imap_fetchstructure($inbox, $email_number);
  
          $output.= '&lt;div class="toggle'.($overview[0]-&gt;seen ? 'read' : 'unread').'"&gt;';
          $output.= '&lt;span class="subject"&gt;'.$overview[0]-&gt;subject.'&lt;/span&gt; ';
          $output.= '&lt;span class="from"&gt;'.$overview[0]-&gt;from.'&lt;/span&gt;';
          $output.= '&lt;span class="date"&gt;on '.$overview[0]-&gt;date.'&lt;/span&gt;';
          $output.= '&lt;/div&gt;';
  
          /* output the email body */
          $output.= '&lt;div class="body"&gt;'.$message.'&lt;/div&gt;';
      }
  
      echo $output;
  } 
  
  imap_close($inbox);
  ?&gt;
  </code></pre>