Using UTF-8 characters on an e-mail subject

If you have sent e-mails in a different language than English or using characters outside the ASCII range you have probably already used utf8 to send them.

Specifying the use of UTF-8 in the body of an e-mail is very similar to doing it for a HTTP response. You can specify the content-type in an e-mail header like this:

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

But there is catch. The subject line of an e-mail is a header by itself, and headers must contain only ASCII characters. Happily, there is 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.

I am going to show a 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);
Leave a comment ?


  1. nice, and thanks this is exactly what i was looking for…
    do you know if this is compatible with old outlook clients?

  2. Thanks, this work fine.

  3. Not working

  4. Your approach is what I need, because I am no programmer, although I thrust deep into the matter, wanting to produce programs for Esperanto.
    My problem is just how to present the letter U+016D in php-script for producing the subject line in an email.
    I tried all hexadecimal encodings of utf-8:
    =?utf-8?Q?la=0xC5 0xAD?=
    None of them gave the correct result: laŭ
    Would be great if you help me.

    • adrian.ancona

      Hello Johan,

      Can you try this?


      I think it works.

      • Dear Adrian,
        I tried both =?utf-8?B?bGHFrQ==?= and
        =?utf-8?B?bGHFrQ?=, but they didn’t work.
        B is more complicated. I need to encode only separate letters: ĉ ĝ ĥ ĵ ŝ ŭ
        Do you have a suggestion with Q-encoding?

        • Perhaps a little late, but you could use imap_8bit() function to solve this:

          $subject = "=?UTF-8?Q?" . imap_8bit("ĉ ĝ ĥ ĵ ŝ ŭ") . "?=";
          • Dear Roy thanks for sharing snipet

            $subject = “=?UTF-8?Q?” . imap_8bit(“ĉ ĝ ĥ ĵ ŝ ŭ”) . “?=”;

            but i want to know how i can use subject in this.i m begainer so plz help me

    • Hi Johan,

      I know it’s late, but this could help someone else.
      Both following strings should work:

      First is in quoted printable, second in Base64


  5. Excellent snippet, great share, very useful.

  6. hey mate, nice piece of code. but i have the same problem as the others. i get something like Kh2=?naheKen=?= when i use your code. i’m using arabic and persian characters. and i want to get something like سلام instead of Kh2=?naheKen=?= :mrgreen:
    can you help me a bit through this?
    any help would be appreciated.

  7. Thanks very much for this clear info!
    I was looking for this building my own Webmail-client with MIME-parser.

  8. I am use this solution for email header:
    UTF-8 header for email

  9. Very well explained! Right to the point. Thank you very much.

  10. Thanks for the article.
    It was up to the point.

  11. Many thanks for this – I had a problem with my checkout getting customers’ names wrong in the license key emails when their name included non-Latin characters, and it’s all now fixed thanks to your post. Thanks again! 🙂

  12. I like this article.Thans from Peru.

  13. You have just saved my life. Great workaround! Thanks so much


    Also note that the encoded subject may not be more than 76 characters.
    Long subjects should be split into multiple lines and each line encoded on it’s own.
    When splitting the subject ensure you do not break multi-byte characters in half.

    Final result should look like this:
    Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=

    Note if you encode subjects that have the majority of characters ASCII (like most european languages) you may use quoted printable encoding, but if not use base64.

  15. Ty very much sir!

  16. Very well explained! Right to the point. Thank you very much.

  17. Matt Ramadanovic

    Great explanation. My problem is that I needed to do the opposite. I needed to convert base64 encoded utf-8 email subject to ascii. What have found is that spammers like to encode the subjects in an attempt to avoid filtering. Using your article I wrote a sub to do this (in Perl because I’m old like that)

    sub convert_subject {
    use utf8;
    use MIME::Base64;
    use Encode;
    my $subject = $_[0];
    my $decoded_subject;
    my $character_set = “”;
    my $encoding_type = “”;
    my $encoded_subject = “”;
    $_ = $subject;
    if ($1) {
    if ($character_set eq ‘utf-8’) {
    if ($encoding_type eq ‘B’) {
    $decoded_subject = decode_base64($encoded_subject);
    #my $windows_1252 = Encode::encode(“Windows-1252”, $decoded_subject);
    my $ISO88591 = Encode::encode(“ISO-8859-1”, $decoded_subject);
    my $ascii = $ISO88591;
    $ascii =~ s/[^[:ascii:]]//g;
    return $ascii;
    } else {
    return $subject;
    } else {
    return $subject;

  18. Matt Ramadanovic

    Oops, call it like this :

    my $subject = “=?utf-8?B?44Cw8J+GleOAsEZpbmQgRiNjayBCdWRkaWVzIGluIHlvdXIgdG93bj/jgLDwn4aV44CwIA==?=”;
    $subject = “How are you today?”;
    my $ascii_subject = convert_subject($subject); #returned cleaned up subject
    print $subject . “->” . $ascii_subject . “\n”;

    $subject = “How are you???”;
    $ascii_subject = convert_subject($subject); #returns unaltered subject
    print $subject . “->” . $ascii_subject . “\n”;;

  19. Perfect! reduced my spam score from 3.0 to 0.1 !!! 😛

  20. Stephen Barner

    I’m running into the same issue. Some spammer has been sending me similar emails for quite a while now. They are similar in that the body is made up of graphics, the sender address is spoofed and always different, and the subject lines are encoded as UTF-8. The subjects are typically different as well. I realized this when I found that rules looking for common strings were not working. It wasn’t until I dug into the headers that I realized that those strings were not there. Right now I’m getting around it by moving all incoming emails that have “UTF-8” in the header to a spam folder. It’s quite the brute force approach, but it seems to be working. I’m glad that no legitimate senders of email to me are using UTF-8 encoding in their email headers.

  21. Nice work guys!
    May I ask what about the FROM field, if it is in CYRILLIC as same as the Subject is, this method works only for the subject, but not for the From field, any ideas?


  22. Great! I had problem while creating cards at trello from email. But that fixed no Polish characters in name of the card. Thank you.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Time limit is exhausted. Please reload CAPTCHA.