Monday, March 21, 2016

Workaround on How to "Save and Render" Emojis

I've came up to this problem of sending marketing emails that contains emojis in the subject ('cause yeah, emojis can be eye-catching, inducing users to open and read the email).

This marketing emails are stored in the database with all the info needed including the subject to be used. Upon sending, these details are to be fetched from the database.

The problem is that the database uses utf-8 character set whereas utf8mb4 is needed to support emojis. I have to implement this preferably without changing the encoding of the database, if possible.

So I've come up to this hackish trick of encoding the subject first before saving it to the database. Then just decode it in the application layer, if necessary.

In my case, since I use mb_encode_mimeheader(), I don't even need to decode it upon sending the email. I can just use it as is and this will be rendered properly by email providers.

Emojis in Email Subject

If you need to display/render the emojis back to the user, simply decode it.

The only drawback I could think of is when you try to query the data directly from the database. If you use the db records directly to track stats or something, that would be difficult. But anyway, emojis are supposed to be part of texts (e.g. message content, comment, etc.) which most of the time aren't the data scientists' subject. But in case it is, or you just really need to store the emojis unencoded, you'll then have to change the database character set to utf8mb4.

No comments:

Post a Comment