In my earlier publish on utilizing canonicalize()
to render emoji characters in ColdFusion, I discussed that this system will be useful in contexts the place HTML entities aren’t nicely supported. E mail topic strains look like one such context; as I found yesterday when making an attempt so as to add a police siren emoji to an e mail topic line for a time-sensitive (expiring) hyperlink. To get round this, we are able to use the canonicalize()
perform to embed emoji safely inside e mail topic strains in our CFML.
To be clear, I do not know if that is typically a problem with e mail topic strains (the requirement of plain-text content material); or, if that is one thing particular to the way in which by which CFMail
works. So, take this publish with an oz. of measure.
That mentioned, let’s attempt to ship two emails, one with HTML entities within the topic line and one with canonicalized HTML entities:
<cfscript>
// Topic line with a left-arrow HTML entity and an eyes-emoji HTML entity.
topic = "← Hiya, is it me you are on the lookout for &##x1f440;?";
// Strive sending with embedded HTML entities.
sendEmail( topic );
// Strive sending with canonicalized HTML entities.
sendEmail( canonicalize( topic, false, false ) );
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
/**
* I ship a take a look at e mail with the given topic line.
*/
public void perform sendEmail( required string topic ) {
cfmail(
to = "ben@instance.com",
from = "no-reply@instance.com",
topic = topic,
sort = "textual content/html",
server = "127.0.0.1",
port = 1025,
spoolEnable = false
) {
writeOutput( "<h1> Hiya There </h1>" );
};
}
</cfscript>
As you possibly can see, within the second sendEmail()
name, we’re passing the topic line by means of the canonicalize()
perform first. And, once we run this ColdFusion code and have a look at the Mailhog mail server, we see the next:
Notice: The inbox renders in reverse order of the ship because the latest emails are all the time listed on the prime.
Trying to make use of HTML entities straight does not work—the topic line simply renders the uncooked HTML code. Nonetheless, once we canonicalize()
the topic line, and convert the HTML entities into native characters, the topic line renders the supposed emoji characters.
Notice that on this ColdFusion demo, I am passing the entire topic line by means of ColdFusion’s canonicalize()
perform. It most likely would have been safer if I solely handed the person HTML entities by means of the canonicalize()
perform. This fashion, if I had been to alter the topic textual content sooner or later, I would not run the chance of by accident normalizing textual content that wasn’t anticipating to be normalized.
Need to use code from this publish?
Take a look at the license.
https://bennadel.com/4709