blob: e94837c1c987d2175456c8b8b2a8bef08cc9cdb3 [file] [log] [blame]
R David Murray79cf3ba2012-05-27 17:10:36 -04001:mod:`email.header`: Internationalized headers
2----------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: email.header
5 :synopsis: Representing non-ASCII headers
6
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/email/header.py`
8
9--------------
Georg Brandl116aa622007-08-15 14:28:22 +000010
11:rfc:`2822` is the base standard that describes the format of email messages.
12It derives from the older :rfc:`822` standard which came into widespread use at
13a time when most email was composed of ASCII characters only. :rfc:`2822` is a
14specification written assuming email contains only 7-bit ASCII characters.
15
16Of course, as email has been deployed worldwide, it has become
17internationalized, such that language specific character sets can now be used in
18email messages. The base standard still requires email messages to be
19transferred using only 7-bit ASCII characters, so a slew of RFCs have been
20written describing how to encode email containing non-ASCII characters into
21:rfc:`2822`\ -compliant format. These RFCs include :rfc:`2045`, :rfc:`2046`,
22:rfc:`2047`, and :rfc:`2231`. The :mod:`email` package supports these standards
23in its :mod:`email.header` and :mod:`email.charset` modules.
24
25If you want to include non-ASCII characters in your email headers, say in the
26:mailheader:`Subject` or :mailheader:`To` fields, you should use the
Georg Brandl3638e482009-04-27 16:46:17 +000027:class:`Header` class and assign the field in the :class:`~email.message.Message`
28object to an instance of :class:`Header` instead of using a string for the header
29value. Import the :class:`Header` class from the :mod:`email.header` module.
30For example::
Georg Brandl116aa622007-08-15 14:28:22 +000031
32 >>> from email.message import Message
33 >>> from email.header import Header
34 >>> msg = Message()
35 >>> h = Header('p\xf6stal', 'iso-8859-1')
36 >>> msg['Subject'] = h
Andrew Svetlov199b78d2012-08-12 14:49:59 +030037 >>> msg.as_string()
38 'Subject: =?iso-8859-1?q?p=F6stal?=\n\n'
Georg Brandl116aa622007-08-15 14:28:22 +000039
40
41
42Notice here how we wanted the :mailheader:`Subject` field to contain a non-ASCII
43character? We did this by creating a :class:`Header` instance and passing in
44the character set that the byte string was encoded in. When the subsequent
Georg Brandl3638e482009-04-27 16:46:17 +000045:class:`~email.message.Message` instance was flattened, the :mailheader:`Subject`
46field was properly :rfc:`2047` encoded. MIME-aware mail readers would show this
47header using the embedded ISO-8859-1 character.
Georg Brandl116aa622007-08-15 14:28:22 +000048
Georg Brandl116aa622007-08-15 14:28:22 +000049Here is the :class:`Header` class description:
50
51
Georg Brandl3f076d82009-05-17 11:28:33 +000052.. class:: Header(s=None, charset=None, maxlinelen=None, header_name=None, continuation_ws=' ', errors='strict')
Georg Brandl116aa622007-08-15 14:28:22 +000053
54 Create a MIME-compliant header that can contain strings in different character
55 sets.
56
57 Optional *s* is the initial header value. If ``None`` (the default), the
58 initial header value is not set. You can later append to the header with
Georg Brandlf6945182008-02-01 11:56:49 +000059 :meth:`append` method calls. *s* may be an instance of :class:`bytes` or
60 :class:`str`, but see the :meth:`append` documentation for semantics.
Georg Brandl116aa622007-08-15 14:28:22 +000061
62 Optional *charset* serves two purposes: it has the same meaning as the *charset*
63 argument to the :meth:`append` method. It also sets the default character set
64 for all subsequent :meth:`append` calls that omit the *charset* argument. If
65 *charset* is not provided in the constructor (the default), the ``us-ascii``
66 character set is used both as *s*'s initial charset and as the default for
67 subsequent :meth:`append` calls.
68
R. David Murrayef1a8b62010-12-29 19:06:48 +000069 The maximum line length can be specified explicitly via *maxlinelen*. For
Georg Brandl116aa622007-08-15 14:28:22 +000070 splitting the first line to a shorter value (to account for the field header
71 which isn't included in *s*, e.g. :mailheader:`Subject`) pass in the name of the
72 field in *header_name*. The default *maxlinelen* is 76, and the default value
73 for *header_name* is ``None``, meaning it is not taken into account for the
74 first line of a long, split header.
75
Georg Brandl3f076d82009-05-17 11:28:33 +000076 Optional *continuation_ws* must be :rfc:`2822`\ -compliant folding
77 whitespace, and is usually either a space or a hard tab character. This
78 character will be prepended to continuation lines. *continuation_ws*
79 defaults to a single space character.
Georg Brandl116aa622007-08-15 14:28:22 +000080
Benjamin Petersone41251e2008-04-25 01:59:09 +000081 Optional *errors* is passed straight through to the :meth:`append` method.
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
Georg Brandl3f076d82009-05-17 11:28:33 +000084 .. method:: append(s, charset=None, errors='strict')
Georg Brandl116aa622007-08-15 14:28:22 +000085
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 Append the string *s* to the MIME header.
Georg Brandl116aa622007-08-15 14:28:22 +000087
Georg Brandl3638e482009-04-27 16:46:17 +000088 Optional *charset*, if given, should be a :class:`~email.charset.Charset`
89 instance (see :mod:`email.charset`) or the name of a character set, which
90 will be converted to a :class:`~email.charset.Charset` instance. A value
91 of ``None`` (the default) means that the *charset* given in the constructor
92 is used.
Georg Brandl116aa622007-08-15 14:28:22 +000093
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 *s* may be an instance of :class:`bytes` or :class:`str`. If it is an
95 instance of :class:`bytes`, then *charset* is the encoding of that byte
96 string, and a :exc:`UnicodeError` will be raised if the string cannot be
97 decoded with that character set.
Georg Brandl116aa622007-08-15 14:28:22 +000098
Benjamin Petersone41251e2008-04-25 01:59:09 +000099 If *s* is an instance of :class:`str`, then *charset* is a hint specifying
R. David Murray477efb32011-01-05 01:39:32 +0000100 the character set of the characters in the string.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
R. David Murray477efb32011-01-05 01:39:32 +0000102 In either case, when producing an :rfc:`2822`\ -compliant header using
103 :rfc:`2047` rules, the string will be encoded using the output codec of
104 the charset. If the string cannot be encoded using the output codec, a
105 UnicodeError will be raised.
106
107 Optional *errors* is passed as the errors argument to the decode call
108 if *s* is a byte string.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110
R. David Murray8451c4b2010-10-23 22:19:56 +0000111 .. method:: encode(splitchars=';, \\t', maxlinelen=None, linesep='\\n')
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 Encode a message header into an RFC-compliant format, possibly wrapping
114 long lines and encapsulating non-ASCII parts in base64 or quoted-printable
R David Murray01581ee2011-04-18 10:04:34 -0400115 encodings.
116
117 Optional *splitchars* is a string containing characters which should be
118 given extra weight by the splitting algorithm during normal header
119 wrapping. This is in very rough support of :RFC:`2822`\'s 'higher level
120 syntactic breaks': split points preceded by a splitchar are preferred
121 during line splitting, with the characters preferred in the order in
122 which they appear in the string. Space and tab may be included in the
123 string to indicate whether preference should be given to one over the
124 other as a split point when other split chars do not appear in the line
R David Murray530cc092011-04-18 15:54:58 -0400125 being split. Splitchars does not affect :RFC:`2047` encoded lines.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
Georg Brandl3f076d82009-05-17 11:28:33 +0000127 *maxlinelen*, if given, overrides the instance's value for the maximum
128 line length.
129
R. David Murray8451c4b2010-10-23 22:19:56 +0000130 *linesep* specifies the characters used to separate the lines of the
131 folded header. It defaults to the most useful value for Python
132 application code (``\n``), but ``\r\n`` can be specified in order
133 to produce headers with RFC-compliant line separators.
134
Georg Brandl872a7022010-10-24 14:32:45 +0000135 .. versionchanged:: 3.2
136 Added the *linesep* argument.
R. David Murray8451c4b2010-10-23 22:19:56 +0000137
Georg Brandl3f076d82009-05-17 11:28:33 +0000138
Benjamin Petersone41251e2008-04-25 01:59:09 +0000139 The :class:`Header` class also provides a number of methods to support
140 standard operators and built-in functions.
Georg Brandl116aa622007-08-15 14:28:22 +0000141
Benjamin Petersone41251e2008-04-25 01:59:09 +0000142 .. method:: __str__()
Georg Brandl116aa622007-08-15 14:28:22 +0000143
R. David Murray92532142011-01-07 23:25:30 +0000144 Returns an approximation of the :class:`Header` as a string, using an
145 unlimited line length. All pieces are converted to unicode using the
146 specified encoding and joined together appropriately. Any pieces with a
Éric Araujo941afed2011-09-01 02:47:34 +0200147 charset of ``'unknown-8bit'`` are decoded as ASCII using the ``'replace'``
R. David Murray92532142011-01-07 23:25:30 +0000148 error handler.
149
150 .. versionchanged:: 3.2
Éric Araujo941afed2011-09-01 02:47:34 +0200151 Added handling for the ``'unknown-8bit'`` charset.
Benjamin Petersone41251e2008-04-25 01:59:09 +0000152
Georg Brandlb30f3302011-01-06 09:23:56 +0000153
Benjamin Petersone41251e2008-04-25 01:59:09 +0000154 .. method:: __eq__(other)
155
156 This method allows you to compare two :class:`Header` instances for
157 equality.
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159
Benjamin Petersone41251e2008-04-25 01:59:09 +0000160 .. method:: __ne__(other)
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Benjamin Petersone41251e2008-04-25 01:59:09 +0000162 This method allows you to compare two :class:`Header` instances for
163 inequality.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165The :mod:`email.header` module also provides the following convenient functions.
166
167
168.. function:: decode_header(header)
169
170 Decode a message header value without converting the character set. The header
171 value is in *header*.
172
173 This function returns a list of ``(decoded_string, charset)`` pairs containing
174 each of the decoded parts of the header. *charset* is ``None`` for non-encoded
175 parts of the header, otherwise a lower case string containing the name of the
176 character set specified in the encoded string.
177
178 Here's an example::
179
180 >>> from email.header import decode_header
181 >>> decode_header('=?iso-8859-1?q?p=F6stal?=')
Andrew Svetlov199b78d2012-08-12 14:49:59 +0300182 [(b'p\xf6stal', 'iso-8859-1')]
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184
Georg Brandl3f076d82009-05-17 11:28:33 +0000185.. function:: make_header(decoded_seq, maxlinelen=None, header_name=None, continuation_ws=' ')
Georg Brandl116aa622007-08-15 14:28:22 +0000186
187 Create a :class:`Header` instance from a sequence of pairs as returned by
188 :func:`decode_header`.
189
190 :func:`decode_header` takes a header value string and returns a sequence of
191 pairs of the format ``(decoded_string, charset)`` where *charset* is the name of
192 the character set.
193
Georg Brandl3f076d82009-05-17 11:28:33 +0000194 This function takes one of those sequence of pairs and returns a
195 :class:`Header` instance. Optional *maxlinelen*, *header_name*, and
196 *continuation_ws* are as in the :class:`Header` constructor.
Georg Brandl116aa622007-08-15 14:28:22 +0000197