blob: 603bec6ef2dfa8f58f409dd13ae0ee5d0dcb11b9 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`mimetools` --- Tools for parsing MIME messages
3====================================================
4
5.. module:: mimetools
6 :synopsis: Tools for parsing MIME-style message bodies.
7
8
9.. deprecated:: 2.3
10 The :mod:`email` package should be used in preference to the :mod:`mimetools`
11 module. This module is present only to maintain backward compatibility.
12
13.. index:: module: rfc822
14
15This module defines a subclass of the :mod:`rfc822` module's :class:`Message`
16class and a number of utility functions that are useful for the manipulation for
17MIME multipart or encoded message.
18
19It defines the following items:
20
21
22.. class:: Message(fp[, seekable])
23
24 Return a new instance of the :class:`Message` class. This is a subclass of the
25 :class:`rfc822.Message` class, with some additional methods (see below). The
26 *seekable* argument has the same meaning as for :class:`rfc822.Message`.
27
28
29.. function:: choose_boundary()
30
31 Return a unique string that has a high likelihood of being usable as a part
32 boundary. The string has the form ``'hostipaddr.uid.pid.timestamp.random'``.
33
34
35.. function:: decode(input, output, encoding)
36
37 Read data encoded using the allowed MIME *encoding* from open file object
38 *input* and write the decoded data to open file object *output*. Valid values
39 for *encoding* include ``'base64'``, ``'quoted-printable'``, ``'uuencode'``,
40 ``'x-uuencode'``, ``'uue'``, ``'x-uue'``, ``'7bit'``, and ``'8bit'``. Decoding
41 messages encoded in ``'7bit'`` or ``'8bit'`` has no effect. The input is simply
42 copied to the output.
43
44
45.. function:: encode(input, output, encoding)
46
47 Read data from open file object *input* and write it encoded using the allowed
48 MIME *encoding* to open file object *output*. Valid values for *encoding* are
49 the same as for :meth:`decode`.
50
51
52.. function:: copyliteral(input, output)
53
54 Read lines from open file *input* until EOF and write them to open file
55 *output*.
56
57
58.. function:: copybinary(input, output)
59
60 Read blocks until EOF from open file *input* and write them to open file
61 *output*. The block size is currently fixed at 8192.
62
63
64.. seealso::
65
66 Module :mod:`email`
67 Comprehensive email handling package; supersedes the :mod:`mimetools` module.
68
69 Module :mod:`rfc822`
70 Provides the base class for :class:`mimetools.Message`.
71
72 Module :mod:`multifile`
73 Support for reading files which contain distinct parts, such as MIME data.
74
75 http://www.cs.uu.nl/wais/html/na-dir/mail/mime-faq/.html
76 The MIME Frequently Asked Questions document. For an overview of MIME, see the
77 answer to question 1.1 in Part 1 of this document.
78
79
80.. _mimetools-message-objects:
81
82Additional Methods of Message Objects
83-------------------------------------
84
85The :class:`Message` class defines the following methods in addition to the
86:class:`rfc822.Message` methods:
87
88
89.. method:: Message.getplist()
90
91 Return the parameter list of the :mailheader:`Content-Type` header. This is a
92 list of strings. For parameters of the form ``key=value``, *key* is converted
93 to lower case but *value* is not. For example, if the message contains the
94 header ``Content-type: text/html; spam=1; Spam=2; Spam`` then :meth:`getplist`
95 will return the Python list ``['spam=1', 'spam=2', 'Spam']``.
96
97
98.. method:: Message.getparam(name)
99
100 Return the *value* of the first parameter (as returned by :meth:`getplist`) of
101 the form ``name=value`` for the given *name*. If *value* is surrounded by
102 quotes of the form '``<``...\ ``>``' or '``"``...\ ``"``', these are removed.
103
104
105.. method:: Message.getencoding()
106
107 Return the encoding specified in the :mailheader:`Content-Transfer-Encoding`
108 message header. If no such header exists, return ``'7bit'``. The encoding is
109 converted to lower case.
110
111
112.. method:: Message.gettype()
113
114 Return the message type (of the form ``type/subtype``) as specified in the
115 :mailheader:`Content-Type` header. If no such header exists, return
116 ``'text/plain'``. The type is converted to lower case.
117
118
119.. method:: Message.getmaintype()
120
121 Return the main type as specified in the :mailheader:`Content-Type` header. If
122 no such header exists, return ``'text'``. The main type is converted to lower
123 case.
124
125
126.. method:: Message.getsubtype()
127
128 Return the subtype as specified in the :mailheader:`Content-Type` header. If no
129 such header exists, return ``'plain'``. The subtype is converted to lower case.
130