12.3.4 Creating MIME objects from scratch

Ordinarily, you get a message object tree by passing some text to a Parser instance, which parses the text and returns the root of the represented object tree. Then you can manipulate that tree before transforming the object tree back into text with a Generator instance. However you can also build a complete object tree from scratch, or even individual Message object, by hand. In fact, you can also take an existing object tree and add new Message objects to the tree before generating the output text.

You can create a new message object tree by creating Message instances, adding payloads, and all the appropriate headers manually, but for MIME messages, mimelib provides some convenient classes to make things easier. Each of these classes should be imported from a module with the same name as the class, from within the mimelib package. E.g.:

import mimelib.Image.Image

or

from mimelib.Text import Text

MIMEBase (_major, _minor, **_params)
This is the base class for all the MIME-specific subclasses of Message. Ordinarily you won't create instances specifically of MIMEBase, although you could. MIMEBase is provided primarily as a convenient base class for more specific MIME-aware subclasses.

_major is the Content-Type: major type (e.g. ``text'' or ``image''), and _minor is the Content-Type: minor type (e.g. ``plain'' or ``gif''). _params is a parameter key/value dictionary and is passed directly to Message.addheader().

The MIMEBase class always adds a Content-Type: header (based on _major, _minor, and _params), and a MIME-Version: header (always set to ``1.0'').

Image (_imagedata[, _minor[, _encoding[**_params]]])

A subclass of MIMEBase, the Image class is used to create MIME message objects of major type ``image''. _imagedata is a string containing the raw image data. If this data can be decoded by the standard Python module imghdr, then the subtype will be automatically included in the Content-Type: header. Otherwise you can explicitly specify the image subtype via the _minor parameter. If the minor type could not be guessed and _minor was not given, then ImageTypeError is raised.

_encoding is a callable (i.e. function) which will perform the actual encoding of the image data for transport. The callable takes one argument, which is the Image object instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding: or other headers to the message object as necessary. The default encoding is base64.

_params are passed straight through to the MIMEBase constructor.

Text (_text[, _minor[, _charset]])
A subclass of MIMEBase, the Text class is used to create MIME objects of major type ``text''. _text is the string for the payload. _minor is the minor type and defaults to ``plain''. _charset is the character set of the text and is passed as a parameter to the MIMEBase constructor; it defaults to ``us-ascii''. No guessing or encoding is performed on the text data, but a newline is appended to _text if it doesn't already end with a newline.

See About this document... for information on suggesting changes.