On this page:
encode-for-header
decode-for-header
generalize-encoding

9 Header Field Encoding

The net/unihead module provides utilities for encoding and decoding header fields using the =?encoding?transport?...?= format.

procedure

(encode-for-header s)  string?

  s : string?
Encodes s for use in a header.

If s contains only ASCII characters, then the result string will have the same content as the given string. If s contains only Latin-1 characters, then on each CRLF-delimited line, the space-delimited sequence containing all non-ASCII characters in s is encoded with a =?ISO-8859-1?Q?...?= sequence. If s contains non-Latin-1 characters, then on each CRLF-delimited line, a space-delimited sequence containing all non-ASCII characters in s is encoded with a =?UTF-8?B?...?= sequence.

Examples:
> (encode-for-header "English")

"English"

> (encode-for-header "français")

"=?ISO-8859-1?Q?fran=E7ais?="

> (encode-for-header "→")

"=?UTF-8?B?4oaS?="

> (encode-for-header "→\r\nboth → and français here")

"=?UTF-8?B?4oaS?=\r\nboth =?UTF-8?B?4oaSIGFuZCBmcmFuw6dhaXM=?= here"

procedure

(decode-for-header s)  string?

  s : string?
Decodes header fields that use the =?encoding?transport?...?= encoding format. The specified encoding is generalized via generalize-encoding before decoding content.

Examples:
> (decode-for-header "English")

"English"

> (decode-for-header "=?UTF-8?B?4oaS?= =?ISO-8859-1?Q?fran=E7ais?=")

"→ français"

procedure

(generalize-encoding s)  (or string? bytes?)

  s : (or string? bytes?)
Generalizes the encoding name s to compensate for typical mailer bugs: Latin-1 and ASCII encodings are geenralized to WINDOWS-1252; GB and GB2312 are generalized to GBK; and KS_C_5601-1987 is generalized to CP949.