blob: cf93473e439744de622813b5af7abeace82cfcf9 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`MimeWriter` --- Generic MIME file writer
2==============================================
3
4.. module:: MimeWriter
5 :synopsis: Write MIME format files.
6
7.. sectionauthor:: Christopher G. Petrilli <petrilli@amber.org>
8
9
10.. deprecated:: 2.3
11 The :mod:`email` package should be used in preference to the :mod:`MimeWriter`
12 module. This module is present only to maintain backward compatibility.
13
14This module defines the class :class:`MimeWriter`. The :class:`MimeWriter`
15class implements a basic formatter for creating MIME multi-part files. It
16doesn't seek around the output file nor does it use large amounts of buffer
17space. You must write the parts out in the order that they should occur in the
18final file. :class:`MimeWriter` does buffer the headers you add, allowing you
19to rearrange their order.
20
21
22.. class:: MimeWriter(fp)
23
24 Return a new instance of the :class:`MimeWriter` class. The only argument
25 passed, *fp*, is a file object to be used for writing. Note that a
26 :class:`StringIO` object could also be used.
27
28
29.. _mimewriter-objects:
30
31MimeWriter Objects
32------------------
33
34:class:`MimeWriter` instances have the following methods:
35
36
37.. method:: MimeWriter.addheader(key, value[, prefix])
38
39 Add a header line to the MIME message. The *key* is the name of the header,
40 where the *value* obviously provides the value of the header. The optional
41 argument *prefix* determines where the header is inserted; ``0`` means append
42 at the end, ``1`` is insert at the start. The default is to append.
43
44
45.. method:: MimeWriter.flushheaders()
46
47 Causes all headers accumulated so far to be written out (and forgotten). This is
48 useful if you don't need a body part at all, e.g. for a subpart of type
49 :mimetype:`message/rfc822` that's (mis)used to store some header-like
50 information.
51
52
53.. method:: MimeWriter.startbody(ctype[, plist[, prefix]])
54
55 Returns a file-like object which can be used to write to the body of the
56 message. The content-type is set to the provided *ctype*, and the optional
57 parameter *plist* provides additional parameters for the content-type
58 declaration. *prefix* functions as in :meth:`addheader` except that the default
59 is to insert at the start.
60
61
62.. method:: MimeWriter.startmultipartbody(subtype[, boundary[, plist[, prefix]]])
63
64 Returns a file-like object which can be used to write to the body of the
65 message. Additionally, this method initializes the multi-part code, where
66 *subtype* provides the multipart subtype, *boundary* may provide a user-defined
67 boundary specification, and *plist* provides optional parameters for the
68 subtype. *prefix* functions as in :meth:`startbody`. Subparts should be created
69 using :meth:`nextpart`.
70
71
72.. method:: MimeWriter.nextpart()
73
74 Returns a new instance of :class:`MimeWriter` which represents an individual
75 part in a multipart message. This may be used to write the part as well as
76 used for creating recursively complex multipart messages. The message must first
77 be initialized with :meth:`startmultipartbody` before using :meth:`nextpart`.
78
79
80.. method:: MimeWriter.lastpart()
81
82 This is used to designate the last part of a multipart message, and should
83 *always* be used when writing multipart messages.
84