Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`email`: Creating email and MIME objects from scratch |
| 2 | ---------------------------------------------------------- |
| 3 | |
| 4 | .. module:: email.mime |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 5 | :synopsis: Build MIME messages. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | Ordinarily, you get a message object structure by passing a file or some text to |
| 9 | a parser, which parses the text and returns the root message object. However |
| 10 | you can also build a complete message structure from scratch, or even individual |
| 11 | :class:`Message` objects by hand. In fact, you can also take an existing |
| 12 | structure and add new :class:`Message` objects, move them around, etc. This |
| 13 | makes a very convenient interface for slicing-and-dicing MIME messages. |
| 14 | |
| 15 | You can create a new object structure by creating :class:`Message` instances, |
| 16 | adding attachments and all the appropriate headers manually. For MIME messages |
| 17 | though, the :mod:`email` package provides some convenient subclasses to make |
| 18 | things easier. |
| 19 | |
| 20 | Here are the classes: |
| 21 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 22 | .. currentmodule:: email.mime.base |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
| 24 | .. class:: MIMEBase(_maintype, _subtype, **_params) |
| 25 | |
| 26 | Module: :mod:`email.mime.base` |
| 27 | |
| 28 | This is the base class for all the MIME-specific subclasses of :class:`Message`. |
| 29 | Ordinarily you won't create instances specifically of :class:`MIMEBase`, |
| 30 | although you could. :class:`MIMEBase` is provided primarily as a convenient |
| 31 | base class for more specific MIME-aware subclasses. |
| 32 | |
| 33 | *_maintype* is the :mailheader:`Content-Type` major type (e.g. :mimetype:`text` |
| 34 | or :mimetype:`image`), and *_subtype* is the :mailheader:`Content-Type` minor |
| 35 | type (e.g. :mimetype:`plain` or :mimetype:`gif`). *_params* is a parameter |
| 36 | key/value dictionary and is passed directly to :meth:`Message.add_header`. |
| 37 | |
| 38 | The :class:`MIMEBase` class always adds a :mailheader:`Content-Type` header |
| 39 | (based on *_maintype*, *_subtype*, and *_params*), and a |
| 40 | :mailheader:`MIME-Version` header (always set to ``1.0``). |
| 41 | |
| 42 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 43 | .. currentmodule:: email.mime.nonmultipart |
| 44 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | .. class:: MIMENonMultipart() |
| 46 | |
| 47 | Module: :mod:`email.mime.nonmultipart` |
| 48 | |
| 49 | A subclass of :class:`MIMEBase`, this is an intermediate base class for MIME |
| 50 | messages that are not :mimetype:`multipart`. The primary purpose of this class |
| 51 | is to prevent the use of the :meth:`attach` method, which only makes sense for |
| 52 | :mimetype:`multipart` messages. If :meth:`attach` is called, a |
| 53 | :exc:`MultipartConversionError` exception is raised. |
| 54 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 56 | .. currentmodule:: email.mime.multipart |
| 57 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 58 | .. class:: MIMEMultipart([_subtype[, boundary[, _subparts[, _params]]]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 60 | Module: :mod:`email.mime.multipart` |
| 61 | |
| 62 | A subclass of :class:`MIMEBase`, this is an intermediate base class for MIME |
| 63 | messages that are :mimetype:`multipart`. Optional *_subtype* defaults to |
| 64 | :mimetype:`mixed`, but can be used to specify the subtype of the message. A |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 65 | :mailheader:`Content-Type` header of :mimetype:`multipart/_subtype` will be |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | added to the message object. A :mailheader:`MIME-Version` header will also be |
| 67 | added. |
| 68 | |
| 69 | Optional *boundary* is the multipart boundary string. When ``None`` (the |
| 70 | default), the boundary is calculated when needed. |
| 71 | |
| 72 | *_subparts* is a sequence of initial subparts for the payload. It must be |
| 73 | possible to convert this sequence to a list. You can always attach new subparts |
| 74 | to the message by using the :meth:`Message.attach` method. |
| 75 | |
| 76 | Additional parameters for the :mailheader:`Content-Type` header are taken from |
| 77 | the keyword arguments, or passed into the *_params* argument, which is a keyword |
| 78 | dictionary. |
| 79 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 81 | .. currentmodule:: email.mime.application |
| 82 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | .. class:: MIMEApplication(_data[, _subtype[, _encoder[, **_params]]]) |
| 84 | |
| 85 | Module: :mod:`email.mime.application` |
| 86 | |
| 87 | A subclass of :class:`MIMENonMultipart`, the :class:`MIMEApplication` class is |
| 88 | used to represent MIME message objects of major type :mimetype:`application`. |
| 89 | *_data* is a string containing the raw byte data. Optional *_subtype* specifies |
| 90 | the MIME subtype and defaults to :mimetype:`octet-stream`. |
| 91 | |
| 92 | Optional *_encoder* is a callable (i.e. function) which will perform the actual |
| 93 | encoding of the data for transport. This callable takes one argument, which is |
| 94 | the :class:`MIMEApplication` instance. It should use :meth:`get_payload` and |
| 95 | :meth:`set_payload` to change the payload to encoded form. It should also add |
| 96 | any :mailheader:`Content-Transfer-Encoding` or other headers to the message |
| 97 | object as necessary. The default encoding is base64. See the |
| 98 | :mod:`email.encoders` module for a list of the built-in encoders. |
| 99 | |
| 100 | *_params* are passed straight through to the base class constructor. |
| 101 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 103 | .. currentmodule:: email.mime.audio |
| 104 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | .. class:: MIMEAudio(_audiodata[, _subtype[, _encoder[, **_params]]]) |
| 106 | |
| 107 | Module: :mod:`email.mime.audio` |
| 108 | |
| 109 | A subclass of :class:`MIMENonMultipart`, the :class:`MIMEAudio` class is used to |
| 110 | create MIME message objects of major type :mimetype:`audio`. *_audiodata* is a |
| 111 | string containing the raw audio data. If this data can be decoded by the |
| 112 | standard Python module :mod:`sndhdr`, then the subtype will be automatically |
| 113 | included in the :mailheader:`Content-Type` header. Otherwise you can explicitly |
| 114 | specify the audio subtype via the *_subtype* parameter. If the minor type could |
| 115 | not be guessed and *_subtype* was not given, then :exc:`TypeError` is raised. |
| 116 | |
| 117 | Optional *_encoder* is a callable (i.e. function) which will perform the actual |
| 118 | encoding of the audio data for transport. This callable takes one argument, |
| 119 | which is the :class:`MIMEAudio` instance. It should use :meth:`get_payload` and |
| 120 | :meth:`set_payload` to change the payload to encoded form. It should also add |
| 121 | any :mailheader:`Content-Transfer-Encoding` or other headers to the message |
| 122 | object as necessary. The default encoding is base64. See the |
| 123 | :mod:`email.encoders` module for a list of the built-in encoders. |
| 124 | |
| 125 | *_params* are passed straight through to the base class constructor. |
| 126 | |
| 127 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 128 | .. currentmodule:: email.mime.image |
| 129 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 130 | .. class:: MIMEImage(_imagedata[, _subtype[, _encoder[, **_params]]]) |
| 131 | |
| 132 | Module: :mod:`email.mime.image` |
| 133 | |
| 134 | A subclass of :class:`MIMENonMultipart`, the :class:`MIMEImage` class is used to |
| 135 | create MIME message objects of major type :mimetype:`image`. *_imagedata* is a |
| 136 | string containing the raw image data. If this data can be decoded by the |
| 137 | standard Python module :mod:`imghdr`, then the subtype will be automatically |
| 138 | included in the :mailheader:`Content-Type` header. Otherwise you can explicitly |
| 139 | specify the image subtype via the *_subtype* parameter. If the minor type could |
| 140 | not be guessed and *_subtype* was not given, then :exc:`TypeError` is raised. |
| 141 | |
| 142 | Optional *_encoder* is a callable (i.e. function) which will perform the actual |
| 143 | encoding of the image data for transport. This callable takes one argument, |
| 144 | which is the :class:`MIMEImage` instance. It should use :meth:`get_payload` and |
| 145 | :meth:`set_payload` to change the payload to encoded form. It should also add |
| 146 | any :mailheader:`Content-Transfer-Encoding` or other headers to the message |
| 147 | object as necessary. The default encoding is base64. See the |
| 148 | :mod:`email.encoders` module for a list of the built-in encoders. |
| 149 | |
| 150 | *_params* are passed straight through to the :class:`MIMEBase` constructor. |
| 151 | |
| 152 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 153 | .. currentmodule:: email.mime.message |
| 154 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | .. class:: MIMEMessage(_msg[, _subtype]) |
| 156 | |
| 157 | Module: :mod:`email.mime.message` |
| 158 | |
| 159 | A subclass of :class:`MIMENonMultipart`, the :class:`MIMEMessage` class is used |
| 160 | to create MIME objects of main type :mimetype:`message`. *_msg* is used as the |
| 161 | payload, and must be an instance of class :class:`Message` (or a subclass |
| 162 | thereof), otherwise a :exc:`TypeError` is raised. |
| 163 | |
| 164 | Optional *_subtype* sets the subtype of the message; it defaults to |
| 165 | :mimetype:`rfc822`. |
| 166 | |
| 167 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 168 | .. currentmodule:: email.mime.text |
| 169 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | .. class:: MIMEText(_text[, _subtype[, _charset]]) |
| 171 | |
| 172 | Module: :mod:`email.mime.text` |
| 173 | |
| 174 | A subclass of :class:`MIMENonMultipart`, the :class:`MIMEText` class is used to |
| 175 | create MIME objects of major type :mimetype:`text`. *_text* is the string for |
| 176 | the payload. *_subtype* is the minor type and defaults to :mimetype:`plain`. |
| 177 | *_charset* is the character set of the text and is passed as a parameter to the |
| 178 | :class:`MIMENonMultipart` constructor; it defaults to ``us-ascii``. No guessing |
| 179 | or encoding is performed on the text data. |
| 180 | |