Search code examples
emailmimerfc

Maximum length of headers


I am interested in the maximum length of the header name, header value. And are there any restrictions on the maximum number of parameters?


Solution

  • None of the relevant specifications define a maximum length for a header name or value, but rfc5321 section 4.5.3.1.6 states that the maximum line length is 1000 octets (aka 1000 bytes) including the terminating <CR><LF> sequence.

    How does that affect maximum header name/value lengths, you might ask?

    It doesn't affect the maximum header value length at all because rfc5322 section 3.2.2 defines CFWS (Comment Folding WhiteSpace) which is further used in the BNF grammar definitions for headers, which basically allows header values to be infinite in length.

    That said, while there is no explicit maximum length for a header field name, there is a practical one.

    • The maximum line length is 1000 octets (including the terminating <CR><LF> sequence).
    • The recommended maximum line length is 78 octets (see rfc5322 section 2.1.1).
    • The syntactical definition of a header looks like this:
      optional-field = field-name ":" unstructured CRLF
      field-name = 1*ftext
      ftext = %d33-57 / ; Printable US-ASCII
      %d59-126 ; characters not including
      ; ":".
      (where optional-field is any header field that is not pre-defined in the specification such as "To", "From", "Date", "Subject", etc). This syntax definition can be found in rfc5322 section 3.6.8.
    • Header field names cannot be folded (as seen by the syntax definition).

    Since it must be possible to represent a header field name and the colon (":") all within 998 bytes (1000 bytes minus the <CR><LF> sequence), we can safely conclude that the maximum length of a header field name is 997 bytes (or, since header field names are constrained to US-ASCII, 997 characters) and SHOULD be constrained to fit within a recommended maximum line length of 78 bytes, meaning the maximum header field name SHOULD be a maximum of 77 bytes/characters.