blob: 808f7e5f09c26c7202af47f239ff456e96d94381 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`email`: Internationalized headers
2---------------------------------------
3
4.. module:: email.header
5 :synopsis: Representing non-ASCII headers
6
7
8:rfc:`2822` is the base standard that describes the format of email messages.
9It derives from the older :rfc:`822` standard which came into widespread use at
10a time when most email was composed of ASCII characters only. :rfc:`2822` is a
11specification written assuming email contains only 7-bit ASCII characters.
12
13Of course, as email has been deployed worldwide, it has become
14internationalized, such that language specific character sets can now be used in
15email messages. The base standard still requires email messages to be
16transferred using only 7-bit ASCII characters, so a slew of RFCs have been
17written describing how to encode email containing non-ASCII characters into
18:rfc:`2822`\ -compliant format. These RFCs include :rfc:`2045`, :rfc:`2046`,
19:rfc:`2047`, and :rfc:`2231`. The :mod:`email` package supports these standards
20in its :mod:`email.header` and :mod:`email.charset` modules.
21
22If you want to include non-ASCII characters in your email headers, say in the
23:mailheader:`Subject` or :mailheader:`To` fields, you should use the
Georg Brandl3638e482009-04-27 16:46:17 +000024:class:`Header` class and assign the field in the :class:`~email.message.Message`
25object to an instance of :class:`Header` instead of using a string for the header
26value. Import the :class:`Header` class from the :mod:`email.header` module.
27For example::
Georg Brandl116aa622007-08-15 14:28:22 +000028
29 >>> from email.message import Message
30 >>> from email.header import Header
31 >>> msg = Message()
32 >>> h = Header('p\xf6stal', 'iso-8859-1')
33 >>> msg['Subject'] = h
Georg Brandl6911e3c2007-09-04 07:15:32 +000034 >>> print(msg.as_string())
Georg Brandl116aa622007-08-15 14:28:22 +000035 Subject: =?iso-8859-1?q?p=F6stal?=
36
37
38
39Notice here how we wanted the :mailheader:`Subject` field to contain a non-ASCII
40character? We did this by creating a :class:`Header` instance and passing in
41the character set that the byte string was encoded in. When the subsequent
Georg Brandl3638e482009-04-27 16:46:17 +000042:class:`~email.message.Message` instance was flattened, the :mailheader:`Subject`
43field was properly :rfc:`2047` encoded. MIME-aware mail readers would show this
44header using the embedded ISO-8859-1 character.
Georg Brandl116aa622007-08-15 14:28:22 +000045
Georg Brandl116aa622007-08-15 14:28:22 +000046Here is the :class:`Header` class description:
47
48
Georg Brandl3f076d82009-05-17 11:28:33 +000049.. class:: Header(s=None, charset=None, maxlinelen=None, header_name=None, continuation_ws=' ', errors='strict')
Georg Brandl116aa622007-08-15 14:28:22 +000050
51 Create a MIME-compliant header that can contain strings in different character
52 sets.
53
54 Optional *s* is the initial header value. If ``None`` (the default), the
55 initial header value is not set. You can later append to the header with
Georg Brandlf6945182008-02-01 11:56:49 +000056 :meth:`append` method calls. *s* may be an instance of :class:`bytes` or
57 :class:`str`, but see the :meth:`append` documentation for semantics.
Georg Brandl116aa622007-08-15 14:28:22 +000058
59 Optional *charset* serves two purposes: it has the same meaning as the *charset*
60 argument to the :meth:`append` method. It also sets the default character set
61 for all subsequent :meth:`append` calls that omit the *charset* argument. If
62 *charset* is not provided in the constructor (the default), the ``us-ascii``
63 character set is used both as *s*'s initial charset and as the default for
64 subsequent :meth:`append` calls.
65
R. David Murrayef1a8b62010-12-29 19:06:48 +000066 The maximum line length can be specified explicitly via *maxlinelen*. For
Georg Brandl116aa622007-08-15 14:28:22 +000067 splitting the first line to a shorter value (to account for the field header
68 which isn't included in *s*, e.g. :mailheader:`Subject`) pass in the name of the
69 field in *header_name*. The default *maxlinelen* is 76, and the default value
70 for *header_name* is ``None``, meaning it is not taken into account for the
71 first line of a long, split header.
72
Georg Brandl3f076d82009-05-17 11:28:33 +000073 Optional *continuation_ws* must be :rfc:`2822`\ -compliant folding
74 whitespace, and is usually either a space or a hard tab character. This
75 character will be prepended to continuation lines. *continuation_ws*
76 defaults to a single space character.
Georg Brandl116aa622007-08-15 14:28:22 +000077
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 Optional *errors* is passed straight through to the :meth:`append` method.
Georg Brandl116aa622007-08-15 14:28:22 +000079
80
Georg Brandl3f076d82009-05-17 11:28:33 +000081 .. method:: append(s, charset=None, errors='strict')
Georg Brandl116aa622007-08-15 14:28:22 +000082
Benjamin Petersone41251e2008-04-25 01:59:09 +000083 Append the string *s* to the MIME header.
Georg Brandl116aa622007-08-15 14:28:22 +000084
Georg Brandl3638e482009-04-27 16:46:17 +000085 Optional *charset*, if given, should be a :class:`~email.charset.Charset`
86 instance (see :mod:`email.charset`) or the name of a character set, which
87 will be converted to a :class:`~email.charset.Charset` instance. A value
88 of ``None`` (the default) means that the *charset* given in the constructor
89 is used.
Georg Brandl116aa622007-08-15 14:28:22 +000090
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 *s* may be an instance of :class:`bytes` or :class:`str`. If it is an
92 instance of :class:`bytes`, then *charset* is the encoding of that byte
93 string, and a :exc:`UnicodeError` will be raised if the string cannot be
94 decoded with that character set.
Georg Brandl116aa622007-08-15 14:28:22 +000095
Benjamin Petersone41251e2008-04-25 01:59:09 +000096 If *s* is an instance of :class:`str`, then *charset* is a hint specifying
R. David Murray477efb32011-01-05 01:39:32 +000097 the character set of the characters in the string.
Georg Brandl116aa622007-08-15 14:28:22 +000098
R. David Murray477efb32011-01-05 01:39:32 +000099 In either case, when producing an :rfc:`2822`\ -compliant header using
100 :rfc:`2047` rules, the string will be encoded using the output codec of
101 the charset. If the string cannot be encoded using the output codec, a
102 UnicodeError will be raised.
103
104 Optional *errors* is passed as the errors argument to the decode call
105 if *s* is a byte string.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107
R. David Murray8451c4b2010-10-23 22:19:56 +0000108 .. method:: encode(splitchars=';, \\t', maxlinelen=None, linesep='\\n')
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Benjamin Petersone41251e2008-04-25 01:59:09 +0000110 Encode a message header into an RFC-compliant format, possibly wrapping
111 long lines and encapsulating non-ASCII parts in base64 or quoted-printable
112 encodings. Optional *splitchars* is a string containing characters to
113 split long ASCII lines on, in rough support of :rfc:`2822`'s *highest
114 level syntactic breaks*. This doesn't affect :rfc:`2047` encoded lines.
Georg Brandl116aa622007-08-15 14:28:22 +0000115
Georg Brandl3f076d82009-05-17 11:28:33 +0000116 *maxlinelen*, if given, overrides the instance's value for the maximum
117 line length.
118
R. David Murray8451c4b2010-10-23 22:19:56 +0000119 *linesep* specifies the characters used to separate the lines of the
120 folded header. It defaults to the most useful value for Python
121 application code (``\n``), but ``\r\n`` can be specified in order
122 to produce headers with RFC-compliant line separators.
123
Georg Brandl872a7022010-10-24 14:32:45 +0000124 .. versionchanged:: 3.2
125 Added the *linesep* argument.
R. David Murray8451c4b2010-10-23 22:19:56 +0000126
Georg Brandl3f076d82009-05-17 11:28:33 +0000127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 The :class:`Header` class also provides a number of methods to support
129 standard operators and built-in functions.
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 .. method:: __str__()
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Benjamin Petersone41251e2008-04-25 01:59:09 +0000133 A synonym for :meth:`Header.encode`. Useful for ``str(aHeader)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
Benjamin Petersone41251e2008-04-25 01:59:09 +0000136 .. method:: __unicode__()
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 A helper for :class:`str`'s :func:`encode` method. Returns the header as
139 a Unicode string.
140
141 .. method:: __eq__(other)
142
143 This method allows you to compare two :class:`Header` instances for
144 equality.
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146
Benjamin Petersone41251e2008-04-25 01:59:09 +0000147 .. method:: __ne__(other)
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Benjamin Petersone41251e2008-04-25 01:59:09 +0000149 This method allows you to compare two :class:`Header` instances for
150 inequality.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
152The :mod:`email.header` module also provides the following convenient functions.
153
154
155.. function:: decode_header(header)
156
157 Decode a message header value without converting the character set. The header
158 value is in *header*.
159
160 This function returns a list of ``(decoded_string, charset)`` pairs containing
161 each of the decoded parts of the header. *charset* is ``None`` for non-encoded
162 parts of the header, otherwise a lower case string containing the name of the
163 character set specified in the encoded string.
164
165 Here's an example::
166
167 >>> from email.header import decode_header
168 >>> decode_header('=?iso-8859-1?q?p=F6stal?=')
169 [('p\xf6stal', 'iso-8859-1')]
170
171
Georg Brandl3f076d82009-05-17 11:28:33 +0000172.. function:: make_header(decoded_seq, maxlinelen=None, header_name=None, continuation_ws=' ')
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174 Create a :class:`Header` instance from a sequence of pairs as returned by
175 :func:`decode_header`.
176
177 :func:`decode_header` takes a header value string and returns a sequence of
178 pairs of the format ``(decoded_string, charset)`` where *charset* is the name of
179 the character set.
180
Georg Brandl3f076d82009-05-17 11:28:33 +0000181 This function takes one of those sequence of pairs and returns a
182 :class:`Header` instance. Optional *maxlinelen*, *header_name*, and
183 *continuation_ws* are as in the :class:`Header` constructor.
Georg Brandl116aa622007-08-15 14:28:22 +0000184