blob: 8297deaf93aa160d2ab70833a5dc0195ef650781 [file] [log] [blame]
R David Murray79cf3ba2012-05-27 17:10:36 -04001:mod:`email.mime`: Creating email and MIME objects from scratch
2---------------------------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: email.mime
Georg Brandl48310cd2009-01-03 21:18:54 +00005 :synopsis: Build MIME messages.
Georg Brandl116aa622007-08-15 14:28:22 +00006
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/email/mime/`
8
9--------------
Georg Brandl116aa622007-08-15 14:28:22 +000010
11Ordinarily, you get a message object structure by passing a file or some text to
12a parser, which parses the text and returns the root message object. However
13you can also build a complete message structure from scratch, or even individual
Georg Brandl3638e482009-04-27 16:46:17 +000014:class:`~email.message.Message` objects by hand. In fact, you can also take an
15existing structure and add new :class:`~email.message.Message` objects, move them
16around, etc. This makes a very convenient interface for slicing-and-dicing MIME
17messages.
Georg Brandl116aa622007-08-15 14:28:22 +000018
Georg Brandl3638e482009-04-27 16:46:17 +000019You can create a new object structure by creating :class:`~email.message.Message`
20instances, adding attachments and all the appropriate headers manually. For MIME
21messages though, the :mod:`email` package provides some convenient subclasses to
22make things easier.
Georg Brandl116aa622007-08-15 14:28:22 +000023
24Here are the classes:
25
Benjamin Peterson75edad02009-01-01 15:05:06 +000026.. currentmodule:: email.mime.base
Georg Brandl116aa622007-08-15 14:28:22 +000027
28.. class:: MIMEBase(_maintype, _subtype, **_params)
29
30 Module: :mod:`email.mime.base`
31
Georg Brandl3638e482009-04-27 16:46:17 +000032 This is the base class for all the MIME-specific subclasses of
33 :class:`~email.message.Message`. Ordinarily you won't create instances
34 specifically of :class:`MIMEBase`, although you could. :class:`MIMEBase`
35 is provided primarily as a convenient base class for more specific
36 MIME-aware subclasses.
Georg Brandl116aa622007-08-15 14:28:22 +000037
38 *_maintype* is the :mailheader:`Content-Type` major type (e.g. :mimetype:`text`
39 or :mimetype:`image`), and *_subtype* is the :mailheader:`Content-Type` minor
40 type (e.g. :mimetype:`plain` or :mimetype:`gif`). *_params* is a parameter
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030041 key/value dictionary and is passed directly to :meth:`Message.add_header
42 <email.message.Message.add_header>`.
Georg Brandl116aa622007-08-15 14:28:22 +000043
44 The :class:`MIMEBase` class always adds a :mailheader:`Content-Type` header
45 (based on *_maintype*, *_subtype*, and *_params*), and a
46 :mailheader:`MIME-Version` header (always set to ``1.0``).
47
48
Benjamin Peterson75edad02009-01-01 15:05:06 +000049.. currentmodule:: email.mime.nonmultipart
50
Georg Brandl116aa622007-08-15 14:28:22 +000051.. class:: MIMENonMultipart()
52
53 Module: :mod:`email.mime.nonmultipart`
54
Georg Brandl3638e482009-04-27 16:46:17 +000055 A subclass of :class:`~email.mime.base.MIMEBase`, this is an intermediate base
56 class for MIME messages that are not :mimetype:`multipart`. The primary
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030057 purpose of this class is to prevent the use of the
58 :meth:`~email.message.Message.attach` method, which only makes sense for
59 :mimetype:`multipart` messages. If :meth:`~email.message.Message.attach`
Georg Brandl3638e482009-04-27 16:46:17 +000060 is called, a :exc:`~email.errors.MultipartConversionError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000061
Georg Brandl116aa622007-08-15 14:28:22 +000062
Benjamin Peterson75edad02009-01-01 15:05:06 +000063.. currentmodule:: email.mime.multipart
64
Georg Brandl3f076d82009-05-17 11:28:33 +000065.. class:: MIMEMultipart(_subtype='mixed', boundary=None, _subparts=None, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +000066
67 Module: :mod:`email.mime.multipart`
68
Georg Brandl3638e482009-04-27 16:46:17 +000069 A subclass of :class:`~email.mime.base.MIMEBase`, this is an intermediate base
70 class for MIME messages that are :mimetype:`multipart`. Optional *_subtype*
71 defaults to :mimetype:`mixed`, but can be used to specify the subtype of the
72 message. A :mailheader:`Content-Type` header of :mimetype:`multipart/_subtype`
73 will be added to the message object. A :mailheader:`MIME-Version` header will
74 also be added.
Georg Brandl116aa622007-08-15 14:28:22 +000075
76 Optional *boundary* is the multipart boundary string. When ``None`` (the
R. David Murray101f2782010-01-10 19:18:27 +000077 default), the boundary is calculated when needed (for example, when the
78 message is serialized).
Georg Brandl116aa622007-08-15 14:28:22 +000079
80 *_subparts* is a sequence of initial subparts for the payload. It must be
81 possible to convert this sequence to a list. You can always attach new subparts
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030082 to the message by using the :meth:`Message.attach
83 <email.message.Message.attach>` method.
Georg Brandl116aa622007-08-15 14:28:22 +000084
85 Additional parameters for the :mailheader:`Content-Type` header are taken from
86 the keyword arguments, or passed into the *_params* argument, which is a keyword
87 dictionary.
88
Georg Brandl116aa622007-08-15 14:28:22 +000089
Benjamin Peterson75edad02009-01-01 15:05:06 +000090.. currentmodule:: email.mime.application
91
Georg Brandl3f076d82009-05-17 11:28:33 +000092.. class:: MIMEApplication(_data, _subtype='octet-stream', _encoder=email.encoders.encode_base64, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +000093
94 Module: :mod:`email.mime.application`
95
Georg Brandl3638e482009-04-27 16:46:17 +000096 A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
97 :class:`MIMEApplication` class is used to represent MIME message objects of
98 major type :mimetype:`application`. *_data* is a string containing the raw
99 byte data. Optional *_subtype* specifies the MIME subtype and defaults to
100 :mimetype:`octet-stream`.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102 Optional *_encoder* is a callable (i.e. function) which will perform the actual
103 encoding of the data for transport. This callable takes one argument, which is
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300104 the :class:`MIMEApplication` instance. It should use
105 :meth:`~email.message.Message.get_payload` and
106 :meth:`~email.message.Message.set_payload` to change the payload to encoded
107 form. It should also add
Georg Brandl116aa622007-08-15 14:28:22 +0000108 any :mailheader:`Content-Transfer-Encoding` or other headers to the message
109 object as necessary. The default encoding is base64. See the
110 :mod:`email.encoders` module for a list of the built-in encoders.
111
112 *_params* are passed straight through to the base class constructor.
113
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Benjamin Peterson75edad02009-01-01 15:05:06 +0000115.. currentmodule:: email.mime.audio
116
Georg Brandl3f076d82009-05-17 11:28:33 +0000117.. class:: MIMEAudio(_audiodata, _subtype=None, _encoder=email.encoders.encode_base64, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119 Module: :mod:`email.mime.audio`
120
Georg Brandl3638e482009-04-27 16:46:17 +0000121 A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
122 :class:`MIMEAudio` class is used to create MIME message objects of major type
123 :mimetype:`audio`. *_audiodata* is a string containing the raw audio data. If
124 this data can be decoded by the standard Python module :mod:`sndhdr`, then the
125 subtype will be automatically included in the :mailheader:`Content-Type` header.
126 Otherwise you can explicitly specify the audio subtype via the *_subtype*
Ezio Melotti93115e02013-03-20 13:53:32 +0200127 argument. If the minor type could not be guessed and *_subtype* was not given,
Georg Brandl3638e482009-04-27 16:46:17 +0000128 then :exc:`TypeError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130 Optional *_encoder* is a callable (i.e. function) which will perform the actual
131 encoding of the audio data for transport. This callable takes one argument,
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300132 which is the :class:`MIMEAudio` instance. It should use
133 :meth:`~email.message.Message.get_payload` and
134 :meth:`~email.message.Message.set_payload` to change the payload to encoded
135 form. It should also add
Georg Brandl116aa622007-08-15 14:28:22 +0000136 any :mailheader:`Content-Transfer-Encoding` or other headers to the message
137 object as necessary. The default encoding is base64. See the
138 :mod:`email.encoders` module for a list of the built-in encoders.
139
140 *_params* are passed straight through to the base class constructor.
141
142
Benjamin Peterson75edad02009-01-01 15:05:06 +0000143.. currentmodule:: email.mime.image
144
Georg Brandl3f076d82009-05-17 11:28:33 +0000145.. class:: MIMEImage(_imagedata, _subtype=None, _encoder=email.encoders.encode_base64, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147 Module: :mod:`email.mime.image`
148
Georg Brandl3638e482009-04-27 16:46:17 +0000149 A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
150 :class:`MIMEImage` class is used to create MIME message objects of major type
151 :mimetype:`image`. *_imagedata* is a string containing the raw image data. If
152 this data can be decoded by the standard Python module :mod:`imghdr`, then the
153 subtype will be automatically included in the :mailheader:`Content-Type` header.
154 Otherwise you can explicitly specify the image subtype via the *_subtype*
Ezio Melotti93115e02013-03-20 13:53:32 +0200155 argument. If the minor type could not be guessed and *_subtype* was not given,
Georg Brandl3638e482009-04-27 16:46:17 +0000156 then :exc:`TypeError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158 Optional *_encoder* is a callable (i.e. function) which will perform the actual
159 encoding of the image data for transport. This callable takes one argument,
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300160 which is the :class:`MIMEImage` instance. It should use
161 :meth:`~email.message.Message.get_payload` and
162 :meth:`~email.message.Message.set_payload` to change the payload to encoded
163 form. It should also add
Georg Brandl116aa622007-08-15 14:28:22 +0000164 any :mailheader:`Content-Transfer-Encoding` or other headers to the message
165 object as necessary. The default encoding is base64. See the
166 :mod:`email.encoders` module for a list of the built-in encoders.
167
Georg Brandl3638e482009-04-27 16:46:17 +0000168 *_params* are passed straight through to the :class:`~email.mime.base.MIMEBase`
169 constructor.
Georg Brandl116aa622007-08-15 14:28:22 +0000170
171
Benjamin Peterson75edad02009-01-01 15:05:06 +0000172.. currentmodule:: email.mime.message
173
Georg Brandl3f076d82009-05-17 11:28:33 +0000174.. class:: MIMEMessage(_msg, _subtype='rfc822')
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176 Module: :mod:`email.mime.message`
177
Georg Brandl3638e482009-04-27 16:46:17 +0000178 A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
179 :class:`MIMEMessage` class is used to create MIME objects of main type
180 :mimetype:`message`. *_msg* is used as the payload, and must be an instance
181 of class :class:`~email.message.Message` (or a subclass thereof), otherwise
182 a :exc:`TypeError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184 Optional *_subtype* sets the subtype of the message; it defaults to
185 :mimetype:`rfc822`.
186
187
Benjamin Peterson75edad02009-01-01 15:05:06 +0000188.. currentmodule:: email.mime.text
189
R David Murray8680bcc2012-03-22 22:17:51 -0400190.. class:: MIMEText(_text, _subtype='plain', _charset=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000191
192 Module: :mod:`email.mime.text`
193
Georg Brandl3638e482009-04-27 16:46:17 +0000194 A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
195 :class:`MIMEText` class is used to create MIME objects of major type
196 :mimetype:`text`. *_text* is the string for the payload. *_subtype* is the
197 minor type and defaults to :mimetype:`plain`. *_charset* is the character
Ezio Melotti93115e02013-03-20 13:53:32 +0200198 set of the text and is passed as an argument to the
Georg Brandl3638e482009-04-27 16:46:17 +0000199 :class:`~email.mime.nonmultipart.MIMENonMultipart` constructor; it defaults
Georg Brandl3be472b2015-01-14 08:26:30 +0100200 to ``us-ascii`` if the string contains only ``ascii`` code points, and
Berker Peksagfe21e4d2014-09-27 00:57:29 +0300201 ``utf-8`` otherwise. The *_charset* parameter accepts either a string or a
202 :class:`~email.charset.Charset` instance.
R David Murray14b01242013-03-19 18:18:55 -0400203
Ezio Melotti93115e02013-03-20 13:53:32 +0200204 Unless the *_charset* argument is explicitly set to ``None``, the
R David Murray14b01242013-03-19 18:18:55 -0400205 MIMEText object created will have both a :mailheader:`Content-Type` header
206 with a ``charset`` parameter, and a :mailheader:`Content-Transfer-Endcoding`
207 header. This means that a subsequent ``set_payload`` call will not result
208 in an encoded payload, even if a charset is passed in the ``set_payload``
209 command. You can "reset" this behavior by deleting the
210 ``Content-Transfer-Encoding`` header, after which a ``set_payload`` call
211 will automatically encode the new payload (and add a new
212 :mailheader:`Content-Transfer-Encoding` header).
Berker Peksagfe21e4d2014-09-27 00:57:29 +0300213
214 .. versionchanged:: 3.5
215 *_charset* also accepts :class:`~email.charset.Charset` instances.