title: Parsing Email Body with 7BIT Content-Transfer-Encoding - PHP
body: <p>I've been implementing some PHP/IMAP-based email handling functionality lately, and have most everything working great, except for message body decoding (in some circumstances).</p>
  
  <p>I think that, by now, I've half-memorized <a href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> (the 'Internet Message Format' document guidelines), read through email-handling code for half a dozen open source CMSes, and read a bajillion forum posts, blog posts, etc. dealing with handling email in PHP.</p>
  
  <p>I've also forked and completely rewritten a class for PHP, <a href="https://github.com/geerlingguy/Imap">Imap</a>, and the class handles email respectably well—I have some helpful methods in there to detect autoresponders (for out of office, old addresses, etc.), decode base64 and 8bit messages, etc.</p>
  
  <p><strong>However, the one thing I simply can't get to work reliably (or, sometimes, at all) is when a message comes in with <code>Content-Transfer-Encoding: 7bit</code>.</strong></p>
  
  <p>It seems that different email clients/services interpret <code>7BIT</code> to mean different things. I've gotten some emails that are supposedly <code>7BIT</code> that are <em>actually</em> Base64-encoded. I've gotten some that are <em>actually</em> quoted-printable-encoded. And some that are not encoded in any way whatsoever. And some that are HTML, but aren't indicated as being HTML, and they're also listed as <code>7BIT</code>...</p>
  
  <p>Here are a few examples (snips) of message bodies received with 7Bit encodings:</p>
  
  <p>1:</p>
  
  <pre><code>A random message=20
  
  Sent from my iPhone
  </code></pre>
  
  <p>2:</p>
  
  <pre><code>PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwi
  IHhtbG5zOm89InVybjpzY2hlbWFzLW1pY3Jvc29mdC1jb206b2ZmaWNlOm9m
  </code></pre>
  
  <p>3:</p>
  
  <pre><code>tangerine apricot pepper.=0A=C2=A0=0ALet me know if you have any availabili=
  ty over the next month or so. =0A=C2=A0=0AThank you,=0ANames Withheld=0A908=
  -319-5916=0A=C2=A0=0A=C2=A0=0A=C2=A0=0A=0A=0A______________________________=
  __=0AFrom: Names Witheld =0ATo: Names Withheld=
  </code></pre>
  
  <p>These are <strong>all</strong> sent with '7Bit' encodings (well, at least according to PHP/<code>imap_*</code>), but they're obviously in need of more decoding before I can pass them along as plaintext. Is there any way to reliably convert all messages with supposedly-7Bit encodings to plaintext?</p>