email.mime.rst 11.4 KB

:mod:`email.mime`: Creating email and MIME objects from scratch

Source code: :source:`Lib/email/mime/`


This module is part of the legacy (Compat32) email API. Its functionality is partially replaced by the :mod:`~email.contentmanager` in the new API, but in certain applications these classes may still be useful, even in non-legacy code.

Ordinarily, you get a message object structure by passing a file or some text to a parser, which parses the text and returns the root message object. However you can also build a complete message structure from scratch, or even individual :class:`~email.message.Message` objects by hand. In fact, you can also take an existing structure and add new :class:`~email.message.Message` objects, move them around, etc. This makes a very convenient interface for slicing-and-dicing MIME messages.

You can create a new object structure by creating :class:`~email.message.Message` instances, adding attachments and all the appropriate headers manually. For MIME messages though, the :mod:`email` package provides some convenient subclasses to make things easier.

Here are the classes:

Module: :mod:`email.mime.nonmultipart`

A subclass of :class:`~email.mime.base.MIMEBase`, this is an intermediate base class for MIME messages that are not :mimetype:`multipart`. The primary purpose of this class is to prevent the use of the :meth:`~email.message.Message.attach` method, which only makes sense for :mimetype:`multipart` messages. If :meth:`~email.message.Message.attach` is called, a :exc:`~email.errors.MultipartConversionError` exception is raised.