2007-08-15 11:28:22 -03:00
|
|
|
:mod:`email`: Representing an email message
|
|
|
|
-------------------------------------------
|
|
|
|
|
|
|
|
.. module:: email.message
|
|
|
|
:synopsis: The base class representing email messages.
|
|
|
|
|
|
|
|
|
|
|
|
The central class in the :mod:`email` package is the :class:`Message` class,
|
|
|
|
imported from the :mod:`email.message` module. It is the base class for the
|
|
|
|
:mod:`email` object model. :class:`Message` provides the core functionality for
|
|
|
|
setting and querying header fields, and for accessing message bodies.
|
|
|
|
|
|
|
|
Conceptually, a :class:`Message` object consists of *headers* and *payloads*.
|
|
|
|
Headers are :rfc:`2822` style field names and values where the field name and
|
|
|
|
value are separated by a colon. The colon is not part of either the field name
|
|
|
|
or the field value.
|
|
|
|
|
|
|
|
Headers are stored and returned in case-preserving form but are matched
|
|
|
|
case-insensitively. There may also be a single envelope header, also known as
|
|
|
|
the *Unix-From* header or the ``From_`` header. The payload is either a string
|
|
|
|
in the case of simple message objects or a list of :class:`Message` objects for
|
|
|
|
MIME container documents (e.g. :mimetype:`multipart/\*` and
|
|
|
|
:mimetype:`message/rfc822`).
|
|
|
|
|
|
|
|
:class:`Message` objects provide a mapping style interface for accessing the
|
|
|
|
message headers, and an explicit interface for accessing both the headers and
|
|
|
|
the payload. It provides convenience methods for generating a flat text
|
|
|
|
representation of the message object tree, for accessing commonly used header
|
|
|
|
parameters, and for recursively walking over the object tree.
|
|
|
|
|
|
|
|
Here are the methods of the :class:`Message` class:
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Message()
|
|
|
|
|
|
|
|
The constructor takes no arguments.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.as_string([unixfrom])
|
|
|
|
|
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61239 | andrew.kuchling | 2008-03-05 01:44:41 +0100 (Wed, 05 Mar 2008) | 1 line
Add more items; add fragmentary notes
........
r61240 | amaury.forgeotdarc | 2008-03-05 02:50:33 +0100 (Wed, 05 Mar 2008) | 13 lines
Issue#2238: some syntax errors from *args or **kwargs expressions
would give bogus error messages, because of untested exceptions::
>>> f(**g(1=2))
XXX undetected error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
instead of the expected SyntaxError: keyword can't be an expression
Will backport.
........
r61241 | neal.norwitz | 2008-03-05 06:10:48 +0100 (Wed, 05 Mar 2008) | 3 lines
Remove the files/dirs after closing the DB so the tests work on Windows.
Patch from Trent Nelson. Also simplified removing a file by using test_support.
........
r61242 | neal.norwitz | 2008-03-05 06:14:18 +0100 (Wed, 05 Mar 2008) | 3 lines
Get this test to pass even when there is no sound card in the system.
Patch from Trent Nelson. (I can't test this.)
........
r61243 | neal.norwitz | 2008-03-05 06:20:44 +0100 (Wed, 05 Mar 2008) | 3 lines
Catch OSError when trying to remove a file in case removal fails. This
should prevent a failure in tearDown masking any real test failure.
........
r61244 | neal.norwitz | 2008-03-05 06:38:06 +0100 (Wed, 05 Mar 2008) | 5 lines
Make the timeout longer to give slow machines a chance to pass the test
before timing out. This doesn't change the duration of the test under
normal circumstances. This is targetted at fixing the spurious failures
on the FreeBSD buildbot primarily.
........
r61245 | neal.norwitz | 2008-03-05 06:49:03 +0100 (Wed, 05 Mar 2008) | 1 line
Tabs -> spaces
........
r61246 | neal.norwitz | 2008-03-05 06:50:20 +0100 (Wed, 05 Mar 2008) | 1 line
Use -u urlfetch to run more tests
........
r61247 | neal.norwitz | 2008-03-05 06:51:20 +0100 (Wed, 05 Mar 2008) | 1 line
test_smtplib sometimes reports leaks too, suppress it
........
r61248 | jeffrey.yasskin | 2008-03-05 07:19:56 +0100 (Wed, 05 Mar 2008) | 5 lines
Fix test_socketserver on Windows after r61099 added several signal.alarm()
calls (which don't exist on non-Unix platforms).
Thanks to Trent Nelson for the report and patch.
........
r61249 | georg.brandl | 2008-03-05 08:10:35 +0100 (Wed, 05 Mar 2008) | 2 lines
Fix some rst.
........
r61252 | thomas.heller | 2008-03-05 15:53:39 +0100 (Wed, 05 Mar 2008) | 2 lines
News entry for yesterdays commit.
........
r61253 | thomas.heller | 2008-03-05 16:34:29 +0100 (Wed, 05 Mar 2008) | 3 lines
Issue 1872: Changed the struct module typecode from 't' to '?', for
compatibility with PEP3118.
........
r61254 | skip.montanaro | 2008-03-05 17:41:09 +0100 (Wed, 05 Mar 2008) | 4 lines
Elaborate on the role of the altinstall target when installing multiple
versions.
........
r61255 | georg.brandl | 2008-03-05 20:31:44 +0100 (Wed, 05 Mar 2008) | 2 lines
#2239: PYTHONPATH delimiter is os.pathsep.
........
r61256 | raymond.hettinger | 2008-03-05 21:59:58 +0100 (Wed, 05 Mar 2008) | 1 line
C implementation of itertools.permutations().
........
r61257 | raymond.hettinger | 2008-03-05 22:04:32 +0100 (Wed, 05 Mar 2008) | 1 line
Small code cleanup.
........
r61260 | martin.v.loewis | 2008-03-05 23:24:31 +0100 (Wed, 05 Mar 2008) | 2 lines
cd PCbuild only after deleting all pyc files.
........
r61261 | raymond.hettinger | 2008-03-06 02:15:52 +0100 (Thu, 06 Mar 2008) | 1 line
Add examples.
........
r61262 | andrew.kuchling | 2008-03-06 02:36:27 +0100 (Thu, 06 Mar 2008) | 1 line
Add two items
........
r61263 | georg.brandl | 2008-03-06 07:47:18 +0100 (Thu, 06 Mar 2008) | 2 lines
#1725737: ignore other VC directories other than CVS and SVN's too.
........
r61264 | martin.v.loewis | 2008-03-06 07:55:22 +0100 (Thu, 06 Mar 2008) | 4 lines
Patch #2232: os.tmpfile might fail on Windows if the user has no
permission to create files in the root directory.
Will backport to 2.5.
........
r61269 | georg.brandl | 2008-03-06 08:19:15 +0100 (Thu, 06 Mar 2008) | 2 lines
Expand on re.split behavior with captured expressions.
........
r61270 | georg.brandl | 2008-03-06 08:22:09 +0100 (Thu, 06 Mar 2008) | 2 lines
Little clarification of assignments.
........
r61271 | georg.brandl | 2008-03-06 08:31:34 +0100 (Thu, 06 Mar 2008) | 2 lines
Add isinstance/issubclass to tutorial.
........
r61272 | georg.brandl | 2008-03-06 08:34:52 +0100 (Thu, 06 Mar 2008) | 2 lines
Add missing NEWS entry for r61263.
........
r61273 | georg.brandl | 2008-03-06 08:41:16 +0100 (Thu, 06 Mar 2008) | 2 lines
#2225: return nonzero status code from py_compile if not all files could be compiled.
........
r61274 | georg.brandl | 2008-03-06 08:43:02 +0100 (Thu, 06 Mar 2008) | 2 lines
#2220: handle matching failure more gracefully.
........
r61275 | georg.brandl | 2008-03-06 08:45:52 +0100 (Thu, 06 Mar 2008) | 2 lines
Bug #2220: handle rlcompleter attribute match failure more gracefully.
........
r61278 | martin.v.loewis | 2008-03-06 14:49:47 +0100 (Thu, 06 Mar 2008) | 1 line
Rely on x64 platform configuration when building _bsddb on AMD64.
........
r61279 | martin.v.loewis | 2008-03-06 14:50:28 +0100 (Thu, 06 Mar 2008) | 1 line
Update db-4.4.20 build procedure.
........
r61285 | raymond.hettinger | 2008-03-06 21:52:01 +0100 (Thu, 06 Mar 2008) | 1 line
More tests.
........
r61286 | raymond.hettinger | 2008-03-06 23:51:36 +0100 (Thu, 06 Mar 2008) | 1 line
Issue 2246: itertools grouper object did not participate in GC (should be backported).
........
r61288 | raymond.hettinger | 2008-03-07 02:33:20 +0100 (Fri, 07 Mar 2008) | 1 line
Tweak recipes and tests
........
r61289 | jeffrey.yasskin | 2008-03-07 07:22:15 +0100 (Fri, 07 Mar 2008) | 5 lines
Progress on issue #1193577 by adding a polling .shutdown() method to
SocketServers. The core of the patch was written by Pedro Werneck, but any bugs
are mine. I've also rearranged the code for timeouts in order to avoid
interfering with the shutdown poll.
........
r61290 | nick.coghlan | 2008-03-07 15:13:28 +0100 (Fri, 07 Mar 2008) | 1 line
Speed up with statements by storing the __exit__ method on the stack instead of in a temp variable (bumps the magic number for pyc files)
........
r61298 | andrew.kuchling | 2008-03-07 22:09:23 +0100 (Fri, 07 Mar 2008) | 1 line
Grammar fix
........
r61303 | georg.brandl | 2008-03-08 10:54:06 +0100 (Sat, 08 Mar 2008) | 2 lines
#2253: fix continue vs. finally docs.
........
r61304 | marc-andre.lemburg | 2008-03-08 11:01:43 +0100 (Sat, 08 Mar 2008) | 3 lines
Add new name for Mandrake: Mandriva.
........
r61305 | georg.brandl | 2008-03-08 11:05:24 +0100 (Sat, 08 Mar 2008) | 2 lines
#1533486: fix types in refcount intro.
........
r61312 | facundo.batista | 2008-03-08 17:50:27 +0100 (Sat, 08 Mar 2008) | 5 lines
Issue 1106316. post_mortem()'s parameter, traceback, is now
optional: it defaults to the traceback of the exception that is currently
being handled.
........
r61313 | jeffrey.yasskin | 2008-03-08 19:26:54 +0100 (Sat, 08 Mar 2008) | 2 lines
Add tests for with and finally performance to pybench.
........
r61314 | jeffrey.yasskin | 2008-03-08 21:08:21 +0100 (Sat, 08 Mar 2008) | 2 lines
Fix pybench for pythons < 2.6, tested back to 2.3.
........
r61317 | jeffrey.yasskin | 2008-03-08 22:35:15 +0100 (Sat, 08 Mar 2008) | 3 lines
Well that was dumb. platform.python_implementation returns a function, not a
string.
........
r61329 | georg.brandl | 2008-03-09 16:11:39 +0100 (Sun, 09 Mar 2008) | 2 lines
#2249: document assertTrue and assertFalse.
........
r61332 | neal.norwitz | 2008-03-09 20:03:42 +0100 (Sun, 09 Mar 2008) | 4 lines
Introduce a lock to fix a race condition which caused an exception in the test.
Some buildbots were consistently failing (e.g., amd64).
Also remove a couple of semi-colons.
........
r61344 | raymond.hettinger | 2008-03-11 01:19:07 +0100 (Tue, 11 Mar 2008) | 1 line
Add recipe to docs.
........
r61350 | guido.van.rossum | 2008-03-11 22:18:06 +0100 (Tue, 11 Mar 2008) | 3 lines
Fix the overflows in expandtabs(). "This time for sure!"
(Exploit at request.)
........
r61351 | raymond.hettinger | 2008-03-11 22:37:46 +0100 (Tue, 11 Mar 2008) | 1 line
Improve docs for itemgetter(). Show that it works with slices.
........
r61363 | georg.brandl | 2008-03-13 08:15:56 +0100 (Thu, 13 Mar 2008) | 2 lines
#2265: fix example.
........
r61364 | georg.brandl | 2008-03-13 08:17:14 +0100 (Thu, 13 Mar 2008) | 2 lines
#2270: fix typo.
........
r61365 | georg.brandl | 2008-03-13 08:21:41 +0100 (Thu, 13 Mar 2008) | 2 lines
#1720705: add docs about import/threading interaction, wording by Nick.
........
r61366 | andrew.kuchling | 2008-03-13 12:07:35 +0100 (Thu, 13 Mar 2008) | 1 line
Add class decorators
........
r61367 | raymond.hettinger | 2008-03-13 17:43:17 +0100 (Thu, 13 Mar 2008) | 1 line
Add 2-to-3 support for the itertools moved to builtins or renamed.
........
r61368 | raymond.hettinger | 2008-03-13 17:43:59 +0100 (Thu, 13 Mar 2008) | 1 line
Consistent tense.
........
r61369 | raymond.hettinger | 2008-03-13 20:03:51 +0100 (Thu, 13 Mar 2008) | 1 line
Issue 2274: Add heapq.heappushpop().
........
r61370 | raymond.hettinger | 2008-03-13 20:33:34 +0100 (Thu, 13 Mar 2008) | 1 line
Simplify the nlargest() code using heappushpop().
........
r61371 | brett.cannon | 2008-03-13 21:27:00 +0100 (Thu, 13 Mar 2008) | 4 lines
Move test_thread over to unittest. Commits GHOP 237.
Thanks Benjamin Peterson for the patch.
........
r61372 | brett.cannon | 2008-03-13 21:33:10 +0100 (Thu, 13 Mar 2008) | 4 lines
Move test_tokenize to doctest.
Done as GHOP 238 by Josip Dzolonga.
........
r61373 | brett.cannon | 2008-03-13 21:47:41 +0100 (Thu, 13 Mar 2008) | 4 lines
Convert test_contains, test_crypt, and test_select to unittest.
Patch from GHOP 294 by David Marek.
........
r61374 | brett.cannon | 2008-03-13 22:02:16 +0100 (Thu, 13 Mar 2008) | 4 lines
Move test_gdbm to use unittest.
Closes issue #1960. Thanks Giampaolo Rodola.
........
r61375 | brett.cannon | 2008-03-13 22:09:28 +0100 (Thu, 13 Mar 2008) | 4 lines
Convert test_fcntl to unittest.
Closes issue #2055. Thanks Giampaolo Rodola.
........
r61376 | raymond.hettinger | 2008-03-14 06:03:44 +0100 (Fri, 14 Mar 2008) | 1 line
Leave heapreplace() unchanged.
........
r61378 | martin.v.loewis | 2008-03-14 14:56:09 +0100 (Fri, 14 Mar 2008) | 2 lines
Patch #2284: add -x64 option to rt.bat.
........
r61379 | martin.v.loewis | 2008-03-14 14:57:59 +0100 (Fri, 14 Mar 2008) | 2 lines
Use -x64 flag.
........
r61382 | brett.cannon | 2008-03-14 15:03:10 +0100 (Fri, 14 Mar 2008) | 2 lines
Remove a bad test.
........
r61383 | mark.dickinson | 2008-03-14 15:23:37 +0100 (Fri, 14 Mar 2008) | 9 lines
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
across platforms: it should now raise OverflowError on all
platforms. (Previously it raised OverflowError only on
non IEEE 754 platforms.)
Also fix the (already existing) test for this behaviour
so that it actually raises TestFailed instead of just
referencing it.
........
r61387 | thomas.heller | 2008-03-14 22:06:21 +0100 (Fri, 14 Mar 2008) | 1 line
Remove unneeded initializer.
........
r61388 | martin.v.loewis | 2008-03-14 22:19:28 +0100 (Fri, 14 Mar 2008) | 2 lines
Run debug version, cd to PCbuild.
........
r61392 | georg.brandl | 2008-03-15 00:10:34 +0100 (Sat, 15 Mar 2008) | 2 lines
Remove obsolete paragraph. #2288.
........
r61395 | georg.brandl | 2008-03-15 01:20:19 +0100 (Sat, 15 Mar 2008) | 2 lines
Fix lots of broken links in the docs, found by Sphinx' external link checker.
........
r61396 | skip.montanaro | 2008-03-15 03:32:49 +0100 (Sat, 15 Mar 2008) | 1 line
note that fork and forkpty raise OSError on failure
........
r61402 | skip.montanaro | 2008-03-15 17:04:45 +0100 (Sat, 15 Mar 2008) | 1 line
add %f format to datetime - issue 1158
........
r61403 | skip.montanaro | 2008-03-15 17:07:11 +0100 (Sat, 15 Mar 2008) | 2 lines
.
........
2008-03-15 21:07:10 -03:00
|
|
|
Return the entire message flattened as a string. When optional *unixfrom* is
|
2007-08-15 11:28:22 -03:00
|
|
|
``True``, the envelope header is included in the returned string. *unixfrom*
|
|
|
|
defaults to ``False``.
|
|
|
|
|
|
|
|
Note that this method is provided as a convenience and may not always format the
|
|
|
|
message the way you want. For example, by default it mangles lines that begin
|
|
|
|
with ``From``. For more flexibility, instantiate a :class:`Generator` instance
|
|
|
|
and use its :meth:`flatten` method directly. For example::
|
|
|
|
|
|
|
|
from cStringIO import StringIO
|
|
|
|
from email.generator import Generator
|
|
|
|
fp = StringIO()
|
|
|
|
g = Generator(fp, mangle_from_=False, maxheaderlen=60)
|
|
|
|
g.flatten(msg)
|
|
|
|
text = fp.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.__str__()
|
|
|
|
|
|
|
|
Equivalent to ``as_string(unixfrom=True)``.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.is_multipart()
|
|
|
|
|
|
|
|
Return ``True`` if the message's payload is a list of sub-\ :class:`Message`
|
|
|
|
objects, otherwise return ``False``. When :meth:`is_multipart` returns False,
|
|
|
|
the payload should be a string object.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.set_unixfrom(unixfrom)
|
|
|
|
|
|
|
|
Set the message's envelope header to *unixfrom*, which should be a string.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_unixfrom()
|
|
|
|
|
|
|
|
Return the message's envelope header. Defaults to ``None`` if the envelope
|
|
|
|
header was never set.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.attach(payload)
|
|
|
|
|
|
|
|
Add the given *payload* to the current payload, which must be ``None`` or a list
|
|
|
|
of :class:`Message` objects before the call. After the call, the payload will
|
|
|
|
always be a list of :class:`Message` objects. If you want to set the payload to
|
|
|
|
a scalar object (e.g. a string), use :meth:`set_payload` instead.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_payload([i[, decode]])
|
|
|
|
|
|
|
|
Return a reference the current payload, which will be a list of :class:`Message`
|
|
|
|
objects when :meth:`is_multipart` is ``True``, or a string when
|
|
|
|
:meth:`is_multipart` is ``False``. If the payload is a list and you mutate the
|
|
|
|
list object, you modify the message's payload in place.
|
|
|
|
|
|
|
|
With optional argument *i*, :meth:`get_payload` will return the *i*-th element
|
|
|
|
of the payload, counting from zero, if :meth:`is_multipart` is ``True``. An
|
|
|
|
:exc:`IndexError` will be raised if *i* is less than 0 or greater than or equal
|
|
|
|
to the number of items in the payload. If the payload is a string (i.e.
|
|
|
|
:meth:`is_multipart` is ``False``) and *i* is given, a :exc:`TypeError` is
|
|
|
|
raised.
|
|
|
|
|
|
|
|
Optional *decode* is a flag indicating whether the payload should be decoded or
|
|
|
|
not, according to the :mailheader:`Content-Transfer-Encoding` header. When
|
|
|
|
``True`` and the message is not a multipart, the payload will be decoded if this
|
|
|
|
header's value is ``quoted-printable`` or ``base64``. If some other encoding is
|
|
|
|
used, or :mailheader:`Content-Transfer-Encoding` header is missing, or if the
|
|
|
|
payload has bogus base64 data, the payload is returned as-is (undecoded). If
|
|
|
|
the message is a multipart and the *decode* flag is ``True``, then ``None`` is
|
|
|
|
returned. The default for *decode* is ``False``.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.set_payload(payload[, charset])
|
|
|
|
|
|
|
|
Set the entire message object's payload to *payload*. It is the client's
|
|
|
|
responsibility to ensure the payload invariants. Optional *charset* sets the
|
|
|
|
message's default character set; see :meth:`set_charset` for details.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.set_charset(charset)
|
|
|
|
|
|
|
|
Set the character set of the payload to *charset*, which can either be a
|
|
|
|
:class:`Charset` instance (see :mod:`email.charset`), a string naming a
|
|
|
|
character set, or ``None``. If it is a string, it will be converted to a
|
|
|
|
:class:`Charset` instance. If *charset* is ``None``, the ``charset`` parameter
|
|
|
|
will be removed from the :mailheader:`Content-Type` header. Anything else will
|
|
|
|
generate a :exc:`TypeError`.
|
|
|
|
|
|
|
|
The message will be assumed to be of type :mimetype:`text/\*` encoded with
|
|
|
|
*charset.input_charset*. It will be converted to *charset.output_charset* and
|
|
|
|
encoded properly, if needed, when generating the plain text representation of
|
|
|
|
the message. MIME headers (:mailheader:`MIME-Version`,
|
|
|
|
:mailheader:`Content-Type`, :mailheader:`Content-Transfer-Encoding`) will be
|
|
|
|
added as needed.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_charset()
|
|
|
|
|
|
|
|
Return the :class:`Charset` instance associated with the message's payload.
|
|
|
|
|
|
|
|
The following methods implement a mapping-like interface for accessing the
|
|
|
|
message's :rfc:`2822` headers. Note that there are some semantic differences
|
|
|
|
between these methods and a normal mapping (i.e. dictionary) interface. For
|
|
|
|
example, in a dictionary there are no duplicate keys, but here there may be
|
|
|
|
duplicate message headers. Also, in dictionaries there is no guaranteed order
|
|
|
|
to the keys returned by :meth:`keys`, but in a :class:`Message` object, headers
|
|
|
|
are always returned in the order they appeared in the original message, or were
|
|
|
|
added to the message later. Any header deleted and then re-added are always
|
|
|
|
appended to the end of the header list.
|
|
|
|
|
|
|
|
These semantic differences are intentional and are biased toward maximal
|
|
|
|
convenience.
|
|
|
|
|
|
|
|
Note that in all cases, any envelope header present in the message is not
|
|
|
|
included in the mapping interface.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.__len__()
|
|
|
|
|
|
|
|
Return the total number of headers, including duplicates.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.__contains__(name)
|
|
|
|
|
|
|
|
Return true if the message object has a field named *name*. Matching is done
|
|
|
|
case-insensitively and *name* should not include the trailing colon. Used for
|
|
|
|
the ``in`` operator, e.g.::
|
|
|
|
|
|
|
|
if 'message-id' in myMessage:
|
2007-09-04 04:15:32 -03:00
|
|
|
print('Message-ID:', myMessage['message-id'])
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.__getitem__(name)
|
|
|
|
|
|
|
|
Return the value of the named header field. *name* should not include the colon
|
|
|
|
field separator. If the header is missing, ``None`` is returned; a
|
|
|
|
:exc:`KeyError` is never raised.
|
|
|
|
|
|
|
|
Note that if the named field appears more than once in the message's headers,
|
|
|
|
exactly which of those field values will be returned is undefined. Use the
|
|
|
|
:meth:`get_all` method to get the values of all the extant named headers.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.__setitem__(name, val)
|
|
|
|
|
|
|
|
Add a header to the message with field name *name* and value *val*. The field
|
|
|
|
is appended to the end of the message's existing fields.
|
|
|
|
|
|
|
|
Note that this does *not* overwrite or delete any existing header with the same
|
|
|
|
name. If you want to ensure that the new header is the only one present in the
|
|
|
|
message with field name *name*, delete the field first, e.g.::
|
|
|
|
|
|
|
|
del msg['subject']
|
|
|
|
msg['subject'] = 'Python roolz!'
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.__delitem__(name)
|
|
|
|
|
|
|
|
Delete all occurrences of the field with name *name* from the message's headers.
|
|
|
|
No exception is raised if the named field isn't present in the headers.
|
|
|
|
|
|
|
|
|
2007-09-01 20:34:30 -03:00
|
|
|
.. method:: Message.__contains__(name)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Return true if the message contains a header field named *name*, otherwise
|
|
|
|
return false.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.keys()
|
|
|
|
|
|
|
|
Return a list of all the message's header field names.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.values()
|
|
|
|
|
|
|
|
Return a list of all the message's field values.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.items()
|
|
|
|
|
|
|
|
Return a list of 2-tuples containing all the message's field headers and values.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get(name[, failobj])
|
|
|
|
|
|
|
|
Return the value of the named header field. This is identical to
|
|
|
|
:meth:`__getitem__` except that optional *failobj* is returned if the named
|
|
|
|
header is missing (defaults to ``None``).
|
|
|
|
|
|
|
|
Here are some additional useful methods:
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_all(name[, failobj])
|
|
|
|
|
|
|
|
Return a list of all the values for the field named *name*. If there are no such
|
|
|
|
named headers in the message, *failobj* is returned (defaults to ``None``).
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.add_header(_name, _value, **_params)
|
|
|
|
|
|
|
|
Extended header setting. This method is similar to :meth:`__setitem__` except
|
|
|
|
that additional header parameters can be provided as keyword arguments. *_name*
|
|
|
|
is the header field to add and *_value* is the *primary* value for the header.
|
|
|
|
|
|
|
|
For each item in the keyword argument dictionary *_params*, the key is taken as
|
|
|
|
the parameter name, with underscores converted to dashes (since dashes are
|
|
|
|
illegal in Python identifiers). Normally, the parameter will be added as
|
|
|
|
``key="value"`` unless the value is ``None``, in which case only the key will be
|
|
|
|
added.
|
|
|
|
|
|
|
|
Here's an example::
|
|
|
|
|
|
|
|
msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
|
|
|
|
|
|
|
|
This will add a header that looks like ::
|
|
|
|
|
|
|
|
Content-Disposition: attachment; filename="bud.gif"
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.replace_header(_name, _value)
|
|
|
|
|
|
|
|
Replace a header. Replace the first header found in the message that matches
|
|
|
|
*_name*, retaining header order and field name case. If no matching header was
|
|
|
|
found, a :exc:`KeyError` is raised.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_content_type()
|
|
|
|
|
|
|
|
Return the message's content type. The returned string is coerced to lower case
|
|
|
|
of the form :mimetype:`maintype/subtype`. If there was no
|
|
|
|
:mailheader:`Content-Type` header in the message the default type as given by
|
|
|
|
:meth:`get_default_type` will be returned. Since according to :rfc:`2045`,
|
|
|
|
messages always have a default type, :meth:`get_content_type` will always return
|
|
|
|
a value.
|
|
|
|
|
|
|
|
:rfc:`2045` defines a message's default type to be :mimetype:`text/plain` unless
|
|
|
|
it appears inside a :mimetype:`multipart/digest` container, in which case it
|
|
|
|
would be :mimetype:`message/rfc822`. If the :mailheader:`Content-Type` header
|
|
|
|
has an invalid type specification, :rfc:`2045` mandates that the default type be
|
|
|
|
:mimetype:`text/plain`.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_content_maintype()
|
|
|
|
|
|
|
|
Return the message's main content type. This is the :mimetype:`maintype` part
|
|
|
|
of the string returned by :meth:`get_content_type`.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_content_subtype()
|
|
|
|
|
|
|
|
Return the message's sub-content type. This is the :mimetype:`subtype` part of
|
|
|
|
the string returned by :meth:`get_content_type`.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_default_type()
|
|
|
|
|
|
|
|
Return the default content type. Most messages have a default content type of
|
|
|
|
:mimetype:`text/plain`, except for messages that are subparts of
|
|
|
|
:mimetype:`multipart/digest` containers. Such subparts have a default content
|
|
|
|
type of :mimetype:`message/rfc822`.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.set_default_type(ctype)
|
|
|
|
|
|
|
|
Set the default content type. *ctype* should either be :mimetype:`text/plain`
|
|
|
|
or :mimetype:`message/rfc822`, although this is not enforced. The default
|
|
|
|
content type is not stored in the :mailheader:`Content-Type` header.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_params([failobj[, header[, unquote]]])
|
|
|
|
|
|
|
|
Return the message's :mailheader:`Content-Type` parameters, as a list. The
|
|
|
|
elements of the returned list are 2-tuples of key/value pairs, as split on the
|
|
|
|
``'='`` sign. The left hand side of the ``'='`` is the key, while the right
|
|
|
|
hand side is the value. If there is no ``'='`` sign in the parameter the value
|
|
|
|
is the empty string, otherwise the value is as described in :meth:`get_param`
|
|
|
|
and is unquoted if optional *unquote* is ``True`` (the default).
|
|
|
|
|
|
|
|
Optional *failobj* is the object to return if there is no
|
|
|
|
:mailheader:`Content-Type` header. Optional *header* is the header to search
|
|
|
|
instead of :mailheader:`Content-Type`.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_param(param[, failobj[, header[, unquote]]])
|
|
|
|
|
|
|
|
Return the value of the :mailheader:`Content-Type` header's parameter *param* as
|
|
|
|
a string. If the message has no :mailheader:`Content-Type` header or if there
|
|
|
|
is no such parameter, then *failobj* is returned (defaults to ``None``).
|
|
|
|
|
|
|
|
Optional *header* if given, specifies the message header to use instead of
|
|
|
|
:mailheader:`Content-Type`.
|
|
|
|
|
|
|
|
Parameter keys are always compared case insensitively. The return value can
|
|
|
|
either be a string, or a 3-tuple if the parameter was :rfc:`2231` encoded. When
|
|
|
|
it's a 3-tuple, the elements of the value are of the form ``(CHARSET, LANGUAGE,
|
|
|
|
VALUE)``. Note that both ``CHARSET`` and ``LANGUAGE`` can be ``None``, in which
|
|
|
|
case you should consider ``VALUE`` to be encoded in the ``us-ascii`` charset.
|
|
|
|
You can usually ignore ``LANGUAGE``.
|
|
|
|
|
|
|
|
If your application doesn't care whether the parameter was encoded as in
|
|
|
|
:rfc:`2231`, you can collapse the parameter value by calling
|
|
|
|
:func:`email.Utils.collapse_rfc2231_value`, passing in the return value from
|
|
|
|
:meth:`get_param`. This will return a suitably decoded Unicode string whn the
|
|
|
|
value is a tuple, or the original string unquoted if it isn't. For example::
|
|
|
|
|
|
|
|
rawparam = msg.get_param('foo')
|
|
|
|
param = email.Utils.collapse_rfc2231_value(rawparam)
|
|
|
|
|
|
|
|
In any case, the parameter value (either the returned string, or the ``VALUE``
|
|
|
|
item in the 3-tuple) is always unquoted, unless *unquote* is set to ``False``.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.set_param(param, value[, header[, requote[, charset[, language]]]])
|
|
|
|
|
|
|
|
Set a parameter in the :mailheader:`Content-Type` header. If the parameter
|
|
|
|
already exists in the header, its value will be replaced with *value*. If the
|
|
|
|
:mailheader:`Content-Type` header as not yet been defined for this message, it
|
|
|
|
will be set to :mimetype:`text/plain` and the new parameter value will be
|
|
|
|
appended as per :rfc:`2045`.
|
|
|
|
|
|
|
|
Optional *header* specifies an alternative header to :mailheader:`Content-Type`,
|
|
|
|
and all parameters will be quoted as necessary unless optional *requote* is
|
|
|
|
``False`` (the default is ``True``).
|
|
|
|
|
|
|
|
If optional *charset* is specified, the parameter will be encoded according to
|
|
|
|
:rfc:`2231`. Optional *language* specifies the RFC 2231 language, defaulting to
|
|
|
|
the empty string. Both *charset* and *language* should be strings.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.del_param(param[, header[, requote]])
|
|
|
|
|
|
|
|
Remove the given parameter completely from the :mailheader:`Content-Type`
|
|
|
|
header. The header will be re-written in place without the parameter or its
|
|
|
|
value. All values will be quoted as necessary unless *requote* is ``False``
|
|
|
|
(the default is ``True``). Optional *header* specifies an alternative to
|
|
|
|
:mailheader:`Content-Type`.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.set_type(type[, header][, requote])
|
|
|
|
|
|
|
|
Set the main type and subtype for the :mailheader:`Content-Type` header. *type*
|
|
|
|
must be a string in the form :mimetype:`maintype/subtype`, otherwise a
|
|
|
|
:exc:`ValueError` is raised.
|
|
|
|
|
|
|
|
This method replaces the :mailheader:`Content-Type` header, keeping all the
|
|
|
|
parameters in place. If *requote* is ``False``, this leaves the existing
|
|
|
|
header's quoting as is, otherwise the parameters will be quoted (the default).
|
|
|
|
|
|
|
|
An alternative header can be specified in the *header* argument. When the
|
|
|
|
:mailheader:`Content-Type` header is set a :mailheader:`MIME-Version` header is
|
|
|
|
also added.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_filename([failobj])
|
|
|
|
|
|
|
|
Return the value of the ``filename`` parameter of the
|
|
|
|
:mailheader:`Content-Disposition` header of the message. If the header does not
|
|
|
|
have a ``filename`` parameter, this method falls back to looking for the
|
|
|
|
``name`` parameter. If neither is found, or the header is missing, then
|
|
|
|
*failobj* is returned. The returned string will always be unquoted as per
|
|
|
|
:meth:`Utils.unquote`.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_boundary([failobj])
|
|
|
|
|
|
|
|
Return the value of the ``boundary`` parameter of the :mailheader:`Content-Type`
|
|
|
|
header of the message, or *failobj* if either the header is missing, or has no
|
|
|
|
``boundary`` parameter. The returned string will always be unquoted as per
|
|
|
|
:meth:`Utils.unquote`.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.set_boundary(boundary)
|
|
|
|
|
|
|
|
Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
|
|
|
|
*boundary*. :meth:`set_boundary` will always quote *boundary* if necessary. A
|
|
|
|
:exc:`HeaderParseError` is raised if the message object has no
|
|
|
|
:mailheader:`Content-Type` header.
|
|
|
|
|
|
|
|
Note that using this method is subtly different than deleting the old
|
|
|
|
:mailheader:`Content-Type` header and adding a new one with the new boundary via
|
|
|
|
:meth:`add_header`, because :meth:`set_boundary` preserves the order of the
|
|
|
|
:mailheader:`Content-Type` header in the list of headers. However, it does *not*
|
|
|
|
preserve any continuation lines which may have been present in the original
|
|
|
|
:mailheader:`Content-Type` header.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_content_charset([failobj])
|
|
|
|
|
|
|
|
Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
|
|
|
|
coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
|
|
|
|
that header has no ``charset`` parameter, *failobj* is returned.
|
|
|
|
|
|
|
|
Note that this method differs from :meth:`get_charset` which returns the
|
|
|
|
:class:`Charset` instance for the default encoding of the message body.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.get_charsets([failobj])
|
|
|
|
|
|
|
|
Return a list containing the character set names in the message. If the message
|
|
|
|
is a :mimetype:`multipart`, then the list will contain one element for each
|
|
|
|
subpart in the payload, otherwise, it will be a list of length 1.
|
|
|
|
|
|
|
|
Each item in the list will be a string which is the value of the ``charset``
|
|
|
|
parameter in the :mailheader:`Content-Type` header for the represented subpart.
|
|
|
|
However, if the subpart has no :mailheader:`Content-Type` header, no ``charset``
|
|
|
|
parameter, or is not of the :mimetype:`text` main MIME type, then that item in
|
|
|
|
the returned list will be *failobj*.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Message.walk()
|
|
|
|
|
|
|
|
The :meth:`walk` method is an all-purpose generator which can be used to iterate
|
|
|
|
over all the parts and subparts of a message object tree, in depth-first
|
|
|
|
traversal order. You will typically use :meth:`walk` as the iterator in a
|
|
|
|
``for`` loop; each iteration returns the next subpart.
|
|
|
|
|
|
|
|
Here's an example that prints the MIME type of every part of a multipart message
|
|
|
|
structure::
|
|
|
|
|
|
|
|
>>> for part in msg.walk():
|
2007-09-04 04:15:32 -03:00
|
|
|
... print(part.get_content_type())
|
2007-08-15 11:28:22 -03:00
|
|
|
multipart/report
|
|
|
|
text/plain
|
|
|
|
message/delivery-status
|
|
|
|
text/plain
|
|
|
|
text/plain
|
|
|
|
message/rfc822
|
|
|
|
|
|
|
|
:class:`Message` objects can also optionally contain two instance attributes,
|
|
|
|
which can be used when generating the plain text of a MIME message.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: preamble
|
|
|
|
|
|
|
|
The format of a MIME document allows for some text between the blank line
|
|
|
|
following the headers, and the first multipart boundary string. Normally, this
|
|
|
|
text is never visible in a MIME-aware mail reader because it falls outside the
|
|
|
|
standard MIME armor. However, when viewing the raw text of the message, or when
|
|
|
|
viewing the message in a non-MIME aware reader, this text can become visible.
|
|
|
|
|
|
|
|
The *preamble* attribute contains this leading extra-armor text for MIME
|
|
|
|
documents. When the :class:`Parser` discovers some text after the headers but
|
|
|
|
before the first boundary string, it assigns this text to the message's
|
|
|
|
*preamble* attribute. When the :class:`Generator` is writing out the plain text
|
|
|
|
representation of a MIME message, and it finds the message has a *preamble*
|
|
|
|
attribute, it will write this text in the area between the headers and the first
|
|
|
|
boundary. See :mod:`email.parser` and :mod:`email.generator` for details.
|
|
|
|
|
|
|
|
Note that if the message object has no preamble, the *preamble* attribute will
|
|
|
|
be ``None``.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: epilogue
|
|
|
|
|
|
|
|
The *epilogue* attribute acts the same way as the *preamble* attribute, except
|
|
|
|
that it contains text that appears between the last boundary and the end of the
|
|
|
|
message.
|
|
|
|
|
2007-09-01 10:51:09 -03:00
|
|
|
You do not need to set the epilogue to the empty string in order for the
|
|
|
|
:class:`Generator` to print a newline at the end of the file.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. data:: defects
|
|
|
|
|
|
|
|
The *defects* attribute contains a list of all the problems found when parsing
|
|
|
|
this message. See :mod:`email.errors` for a detailed description of the
|
|
|
|
possible parsing defects.
|