Broken HTML Email on Lucee
If you鈥檙e building an HTML email message with Lucee, and you find weird things like broken tags or formatting, it may be due to a difference in how <CFMAIL>
works in Lucee compared to ColdFusion. (TL;DR: Forced line breaks in minified code, will be fixed in Lucee 6)
And it brought back ancient memories of dealing with different email encodings that I haven鈥檛 had to deal with in at least a decade and a half.
If you鈥檙e unfamiliar with Lucee, it鈥檚 an alternate, free-and-open source (LGPL 2.1) implementation of CFML, the tag and scripting language originally created back in the 1990s to build websites with ColdFusion. After multiple rounds of corporate buyouts, ColdFusion is currently owned by Adobe.
So. Email generally is transmitted (and often stored) as plain text. Anything formatted, any images or videos or attachments, all get converted to plaintext behind the scenes and then back into your fancy newsletter or attached Excel document or whatever. And there are different ways to convert it, depending on what you鈥檙e attaching.
ColdFusion, when you create an HTML part for an email, uses a Content-Transfer-Encoding called quoted-printable. Which works great on just about any text you might throw at it.
Current versions of Lucee, however, use an encoding called 7bit. Which is more readable if you look under the hood, and handles plain ASCII fine, but isn鈥檛 going to handle much else very well. And has a sharp l000-character line limit.
Wait, 1000 characters, you say? That鈥檚 no problem! Nobody鈥檚 going to put a line that long in their email!
Well鈥f you have HTML and CSS formatting, and it鈥檚 minified before CFMAIL stuffs it into the message, it鈥檚 really easy to hit that limit. Add in long paragraphs relying on the end client to wrap the words?
So what happens when you toss your minified CSS and HTML into a message with Lucee? It puts a line break at every 998 characters to make sure it鈥檚 a valid 7bit message, plus an exclamation mark and a space on either side鈥o matter where it is in the code. Depending on where it breaks, you may have style rules that don鈥檛 work, broken HTML structure, you may have random exclamation points popping up in the message. Whatever the case, it鈥檚 not probably what you want!
This is fixed in Lucee 6.0 by just using quoted-printable, and making it possible to choose 7bit if you really want it. But 6.0 isn鈥檛 quite out yet (as of this writing, there鈥檚 a release candidate), and the latest stable 5.4 version still only uses the 7bit transfer encoding.
I鈥檓 not sure how it took until summer 2022 to spot, since my understanding is that 7bit doesn鈥檛 even handle accented letters, and the project is based in Switzerland. Even without the line break problem, there are a lot of local names that wouldn鈥檛 have been possible to send in a formatted email message.