How to encode Non-ASCII characters in an Email Subject

In this tutorial we will see how to send non-ASCII characters in an Email subject. You have probably already used UTF to send e-mails in different language than English or using characters outside the ASCII range. Specifying the use of UTF-8 in the body of e-mail is similar as for HTTP response. You can specify the content-type in an email header like below:

Content-Type: text/plain; charset=utf-8

But the subject of an e-mail is a header itself and headers must contain only ASCII characters. We have found a work around. RFC 1342 is a recommendation that provides a way to represent non ASCII characters inside e-mail headers in a way that won’t confuse e-mail servers.

To encode a header using this technique you must use this format:


And this is an example of its use:


The encoding must be either B or Q, these mean base 64 and quoted-printable respectively. You can read the RFC 1342 document for more information about how they work.

Here is a code snippet of how to use php’s mail function to send an e-mail using UTF-8 in the subject and content:

$to = '';
$subject = 'Subject with non ASCII ó¿¡á';
$message = 'Message with non ASCII ó¿¡á';
$headers = 'From:'."\r\n"
.'Content-Type: text/plain; charset=utf-8'."\r\n";
mail($to, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $headers);