Web Development

Set Unicode Character in Email Subject with PHP

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.

[cc lang=”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.”);

[/cc]

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

Shares:
  • www.turen.me
    www.turen.me
    May 6, 2013 at 1:41 pm

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

    Reply
    • Avinash
      May 8, 2013 at 11:26 pm

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

      Reply
  • php development india
    May 13, 2013 at 11:22 am

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *