Set Unicode Character in Email Subject with PHP

In this quick article we will see how we can set/use Unicode characters in email subject with PHP.

Set unicode character in email subject with PHP

Here I am referring to characters which includes all possible characters, Here are few examples which are very tough to get into email subject. Example: ❤, ★, etc. There are many more unicode characters available, full list can be found here.

So if you try to send all these characters in email subject in simple way then 99.99% you will end up with junk characters in email subject. I have mentioned 99.99% because some email client understand what we want to achieve and it correct our mistake. :)

Now let’s look at the tricky code snippet which enable us to send Unicode characters in email subject with PHP.

// Code without trick $subject = "I Love You ❤"; // You will end up with junk character in subject mail("[email protected]",$subject,"This email subject will contain JUNK characters."); // Code with trick $subject = "I Love You ❤"; $updated_subject = "=?UTF-8?B?" . base64_encode($subject) . "?="; mail("[email protected]",$updated_subject,"This email subject should not contain JUNK characters.");
// Code without trick
$subject = "I Love You ❤";

// You will end up with junk character in subject
mail("[email protected]",$subject,"This email subject will contain JUNK characters.");


// Code with trick
$subject = "I Love You ❤";
$updated_subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";

mail("[email protected]",$updated_subject,"This email subject should not contain JUNK characters.");

So, I am done with my article, let me know if you have any suggestion/comments/questions on this trick/article.

Share This Article

Tags:
  • www.turen.me

    I cant understand $updated_subject = “=?UTF-8?B?” . base64_encode($subject) . “?=”;,what does it mean?

    • http://www.xpertdeveloper.com/ Avinash

      That instruct to email client that email subject is being encoded with Base64.

  • http://www.biztechconsultancy.com/php-development-india.htm php development india

    Impressive, tricky code snippet… I just loved your post. I bookmarked your post for further and good reading. Keep sharing.