Summary

  • RFC 2047, November 1996, is the part of MIME that carries "textual header information in character sets other than US-ASCII": an encoded-word "begins with '=?', ends with '?=', and has two '?'s in between", states a charset and an encoding, and "white space characters MUST NOT appear between components".
  • The parcels are capped at 75 characters including delimiters, longer text is split across several of them, and any line containing one is limited to 76 characters. By section 5 they may replace display text in Subject, Comments, extension and X- headers, sit inside comments, or replace a word in a phrase — never elsewhere in a field body, so the address itself stays ASCII.

The header was the last flat surface

MIME's list of purposes ends with a modest fourth item: header text in character sets other than US-ASCII. The body had been given ways to change its character set and its structure; the header had not, because RFC 822 described headers in detail as US-ASCII and left the problem of everything else to a later document.

A header is not a body. It is a set of fields a mail system parses to find out where a message goes and who sent it, so a change there has to survive programs that were written against the old grammar and still decide routing. RFC 2047 solved that by inventing a token that could not be mistaken for anything the old grammar already allowed.

The shape of the parcel

The syntax reads like a piece of punctuation that escaped: =?, a charset name, ?, an encoding name, ?, the text in graphic ASCII, ?=. No spaces anywhere inside, because a space is where the parser would otherwise split a word.

Two encodings carry the payload. "B" is the BASE64 of MIME; "Q" is a quoted-printable relative in which an 8-bit value becomes "=" and two hexadecimal digits, with 20 representable as "_", and in which space and tab "MUST NOT be represented as themselves". The character set names and the encoding names are case-independent, since nobody wanted to argue about capitals at three in the morning.

Why 75 characters

The memo gives a limit and then gives the reasons for it. An encoded-word "may not be more than 75 characters long, including 'charset', 'encoding', 'encoded-text', and delimiters", and any line containing one may not exceed 76. The two stated reasons are interoperability "through internetwork mail gateways" and a cap on "the amount of text that could be mistaken for a 'encoded-word' or something else".

That second reason is the interesting one. Every delimiter in the design also has to be unlikely to appear by accident, which is the same argument the memo makes at the start: the syntax is chosen so encoded-words "are unlikely to 'accidentally' appear as normal text in message headers". A parser deciding whether a name is encoded or literal is doing a guess, and the grammar is shaped to keep the guess cheap.

Where a parcel is allowed to sit

Section 5 is a map of the header's grammar, drawn by naming the places where an encoded-word may stand. It may replace display text in Subject, Comments, extension headers and user-defined "X-" headers. It may sit inside a comment. It may replace a word in a phrase, which is what the display name in front of an address is. And it may not appear elsewhere in a field body, so the address itself — the part a mail server routes on — stays in the one character set every implementation already handles.

The joining rules are equally fussy: an encoded-word in a text field must be separated from adjacent text by linear white space, an encoded-word in a phrase must be separated from adjacent words and specials the same way, and a run of encoded-words may be split across lines with CRLF and a space. Read the rules as a whole and they describe a parser being told exactly where to look.

A name that needs the reader's cooperation

An encoded-word is only a name if the reader agrees to decode it. The memo says a mail reader that implements the specification "will recognize encoded-words when they appear in certain portions of the message header" and, instead of displaying the token "as is", "will reverse the encoding and display the original text in the designated character set".

Everything about the format assumes that cooperation. A reader that does not implement it shows =?utf-8?B?5L2g5aW9?=; a reader that implements it shows the characters the sender meant. The same bytes are a name or a piece of punctuation depending on which program opened them, and the sender cannot tell from the outside which one arrived.

That is the trade the memo made, and it made it in the visible part of the message rather than the hidden part. The alternative — putting the encoding decision in a header that says "decode this other header" — would have needed every intermediate server to understand a new instruction. RFC 2047 instead put a self-describing parcel in the text, so an old server can pass it along without knowing what it is.