Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After spending a bit more time, I decided to just write up some heuristic detection, as Max suggested in the comments on my original question.</p> <p>I've built a more robust <code>decode7Bit()</code> method in <a href="https://github.com/geerlingguy/Imap/blob/c4b54e52576bf71a93045d2a4581589eab9a00b6/JJG/Imap.php#L398" rel="nofollow noreferrer">Imap.php</a>, which goes through a bunch of common encoded characters (like <code>=A0</code>) and replaces them with their UTF-8 equivalents, and then also decodes messages if they look like they are base64-encoded:</p> <pre><code>/** * Decodes 7-Bit text. * * PHP seems to think that most emails are 7BIT-encoded, therefore this * decoding method assumes that text passed through may actually be base64- * encoded, quoted-printable encoded, or just plain text. Instead of passing * the email directly through a particular decoding function, this method * runs through a bunch of common encoding schemes to try to decode everything * and simply end up with something *resembling* plain text. * * Results are not guaranteed, but it's pretty good at what it does. * * @param $text (string) * 7-Bit text to convert. * * @return (string) * Decoded text. */ public 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>This was taken from version 1.0-beta2 of the <a href="https://github.com/geerlingguy/Imap" rel="nofollow noreferrer">Imap class for PHP</a> that I have on GitHub.</p> <p>If you have any ideas for making this more efficient, let me know. I originally tried running everything through <code>quoted_printable_decode()</code>, but sometimes PHP would throw exceptions that were vague and unhelpful, so I gave up on that approach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload