title: PHP imap_search: UTF-8 / Non-ASCII characters on Microsoft Exchange mail servers
body: <p>I want to fetch emails from <code>outlook.office365.com</code> using IMAP and PHP.</p>
  
  <p>Since the most emails contain non-ASCII characters like <code>äöü</code>, I use <code>UTF-8</code> in my <code>imap_search()</code> function:</p>
  
  <pre class="lang-php prettyprint-override"><code>imap_search($mbox_connection, 'ALL', SE_UID, "UTF-8")
  </code></pre>
  
  <p>With <code>UTF-8</code> and the search criteria <code>ALL</code> I get all emails as expected. Now, I wanted to restrict it to for example only unseen (unread) emails:</p>
  
  <pre class="lang-php prettyprint-override"><code>imap_search($mbox_connection, 'UNSEEN', SE_UID, "UTF-8")
  </code></pre>
  
  <p>But this unfortunately causes the issue, that no emails can be found anymore - although there are unseen emails - and it also throws this PHP notice:</p>
  
  <pre><code>PHP Notice:  Unknown: [BADCHARSET (US-ASCII)] The specified charset is not supported. (errflg=2) in Unknown on line 0
  </code></pre>
  
  <p>Based on this notice, I've changed the charset from <code>UTF-8</code> to <code>US-ASCII</code>:</p>
  
  <pre class="lang-php prettyprint-override"><code>imap_search($mbox_connection, 'UNSEEN', SE_UID, "US-ASCII")
  </code></pre>
  
  <p>Now, it returns all expected unseen (unread) emails.</p>
  
  <p>The problem is now, that I can't search for emails with UTF-8 characters. I've for example an email with these information:</p>
  
  <ul>
  <li>From: Äpfel Nürnberg </li>
  <li>Subject: Apfel vs. Äpfel</li>
  <li>Body:</li>
  </ul>
  
  <pre><code>Einzahl gegen Mehrzahl.
  
  Ein Apfel, mehrere Äpfel.
  </code></pre>
  
  <p>When I try to search for all emails with the subject "apfel" it works as expected - I can find the email:</p>
  
  <pre class="lang-php prettyprint-override"><code>imap_search($mbox_connection, 'FROM "apfel"', SE_UID, "US-ASCII")
  </code></pre>
  
  <pre><code>Trying to connect to '{outlook.office365.com:993/imap/ssl}INBOX'...
  Found 1 email(s)...
  +------ P A R S I N G ------+
  From: =?iso-8859-1?Q?=C4pfel=20N=FCrnberg?= &lt;=?iso-8859-1?Q?=C4pfel=20N=FCrnberg?= &lt;aepfel@nuernberg.de&gt;&gt;
  Subject: =?iso-8859-1?Q?Apfel_vs._=C4pfel?=
  </code></pre>
  
  <p>But when I instead search for the word with the UTF-8 character (in this case <code>äpfel</code>), it does NOT find the email:</p>
  
  <pre class="lang-php prettyprint-override"><code>imap_search($mbox_connection, 'FROM "äpfel"', SE_UID, "US-ASCII")
  </code></pre>
  
  <p>Due to this fact, I've changed back the charset from <code>US-ASCII</code> to <code>UTF-8</code>, but this only ends again at the error message <code>[BADCHARSET (US-ASCII)]</code>.</p>
  
  <p>My code is very simple:</p>
  
  <pre><code>$mailbox = "{outlook.office365.com:993/imap/ssl}INBOX";
  $mailbox_username = "someone@outlook.com";
  $mailbox_password = "*******";
  
  echo "Trying to connect to '$mailbox'...\n";
  
  $mbox_connection = imap_open($mailbox, $mailbox_username, $mailbox_password);
  
  $mailsIds = imap_search($mbox_connection, 'SUBJECT "äpfel"', SE_UID, "UTF-8");
  
  if(!$mailsIds) {
      echo "No emails found!\n";
      imap_close($mbox_connection);
      die();
  }
  
  echo "Found " . count($mailsIds) . " email(s)...\n";
  
  foreach($mailsIds as $mailId) {
      echo "+------ P A R S I N G ------+\n";
  
      $headersRaw = imap_fetchheader($mbox_connection, $mailId, FT_UID);
      $header = imap_rfc822_parse_headers($headersRaw);
  
      echo "From: " . $header-&gt;from[0]-&gt;personal . " &lt;" . $header-&gt;fromaddress . "&gt;\n";
      echo "Subject: " . $header-&gt;subject . "\n";
  }
  </code></pre>
  
  <p>I've already tried this solution, but this returns also no matching email:</p>
  
  <pre><code>$str = "äpfel";
  $str = preg_replace('/\=\?ISO\-8859\-1\?Q\?/i', '', mb_encode_mimeheader($str, "ISO-8859-1", "Q"));
  $mailsIds = imap_search($mbox_connection, 'SUBJECT "'.$str.'"', SE_UID, 'US-ASCII');
  
  </code></pre>
  
  <p>Any ideas, how I can search for non-ASCII characters in the email fields <code>From</code>, <code>Subject</code> and <code>Body</code> when the IMAP server does not support <code>UTF-8</code> and I also can NOT change this on server-side configuration?</p>
  
  <p>This seems to be an issue with all Microsoft Exchange servers. Only those servers have this issue as far as I could found it out via Google.</p>