In this quick article we will see how we can set/use Unicode characters 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.
I cant understand $updated_subject = “=?UTF-8?B?” . base64_encode($subject) . “?=”;,what does it mean?
That instruct to email client that email subject is being encoded with Base64.
Impressive, tricky code snippet… I just loved your post. I bookmarked your post for further and good reading. Keep sharing.