When a parcel leaves one of our depots, our notification service emails the customer a despatch confirmation with the carrier's tracking link. The service is old. It builds the raw message from a template file that holds the headers and an HTML body, and one of those headers, written in 2016, says Content-Transfer-Encoding: 8bit. The messages go out through the Postfix 3.7 relay that Debian 12 ships, on one of our servers.
In October a front end colleague tidied our email templates and added a build step that minified the HTML, to make the messages smaller. The despatch email's body became one line of about eleven thousand characters.
Within a week customer service had a pattern. Some customers clicked the tracking link and got the carrier's page saying the parcel did not exist. A few had screenshots of order numbers with a space in the middle.
The mail standard limits a line to 998 characters, not counting the line ending. Postfix enforces that when it sends a message on. Its smtp_line_length_limit is 998 by default, and a longer line is broken by inserting a line ending followed by a space, which keeps the message valid and changes its content. Our single line was cut every 998 characters, wherever that fell. When it fell inside an href, the browser kept the space, encoded it as %20, and the carrier rejected the reference. When it fell inside text, the order number on screen had a gap. Which customers were affected depended on the length of their name, address and items, because those moved every break point.
Before the minification, the template's HTML had short lines and the limit never came near it. The header saying 8bit had been a promise the message no longer kept, and nothing between our code and the relay checked it.
The template now declares Content-Transfer-Encoding: quoted-printable, and the service encodes the body that way. Quoted-printable keeps lines under seventy six characters with soft breaks that the receiving mail client removes, so the HTML arrives exactly as written, however long its lines are. A test builds a despatch email for an order with a very long address and fails if any line in the raw message is longer than 998 characters. We kept the minification.
A message passes through systems that are allowed to rewrite whatever breaks the rules. If you declare an encoding, make the content keep the promise, or the next system will keep it for you, in its own way.
– Sergey Shinder
