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