write_multipart(fileobj,
subtype=' mixed ' ,
boundary=None)
|
|
Simple streaming MIME multipart writer.
This function returns a MultipartWriter object that has a few methods to
control the nested MIME parts. For example, to write a flat multipart
envelope you call the add(mimetype, content, [headers]) method for
every part, and finally call the close() method.
>>> from StringIO import StringIO
>>> buf = StringIO()
>>> envelope = write_multipart(buf, boundary='==123456789==')
>>> envelope.add('text/plain', 'Just testing')
>>> envelope.close()
>>> print buf.getvalue().replace('\r\n', '\n')
Content-Type: multipart/mixed; boundary="==123456789=="
<BLANKLINE>
--==123456789==
Content-Length: 12
Content-MD5: nHmX4a6el41B06x2uCpglQ==
Content-Type: text/plain
<BLANKLINE>
Just testing
--==123456789==--
<BLANKLINE>
Note that an explicit boundary is only specified for testing purposes. If
the boundary parameter is omitted, the multipart writer will generate a
random string for the boundary.
To write nested structures, call the open([headers]) method on the
respective envelope, and finish each envelope using the close() method:
>>> buf = StringIO()
>>> envelope = write_multipart(buf, boundary='==123456789==')
>>> part = envelope.open(boundary='==abcdefghi==')
>>> part.add('text/plain', 'Just testing')
>>> part.close()
>>> envelope.close()
>>> print buf.getvalue().replace('\r\n', '\n')
Content-Type: multipart/mixed; boundary="==123456789=="
<BLANKLINE>
--==123456789==
Content-Type: multipart/mixed; boundary="==abcdefghi=="
<BLANKLINE>
--==abcdefghi==
Content-Length: 12
Content-MD5: nHmX4a6el41B06x2uCpglQ==
Content-Type: text/plain
<BLANKLINE>
Just testing
--==abcdefghi==--
--==123456789==--
<BLANKLINE>
- Parameters:
fileobj - a writable file-like object that the output should get
written to
subtype - the subtype of the multipart MIME type (e.g. "mixed")
boundary - the boundary to use to separate the different parts
|