blob: 070c9a27b208cef4fd2eb8454473d815c73790f4 [file] [log] [blame]
Barry Warsaw5b9da892002-10-01 01:05:52 +00001Ordinarily, you get a message object structure by passing a file or
Barry Warsaw5db478f2002-10-01 04:33:16 +00002some text to a parser, which parses the text and returns the root
3message object. However you can also build a complete message
4structure from scratch, or even individual \class{Message} objects by
5hand. In fact, you can also take an existing structure and add new
6\class{Message} objects, move them around, etc. This makes a very
7convenient interface for slicing-and-dicing MIME messages.
Barry Warsaw5b9da892002-10-01 01:05:52 +00008
9You can create a new object structure by creating \class{Message}
10instances, adding attachments and all the appropriate headers manually.
11For MIME messages though, the \module{email} package provides some
12convenient subclasses to make things easier. Each of these classes
13should be imported from a module with the same name as the class, from
14within the \module{email} package. E.g.:
15
16\begin{verbatim}
17import email.MIMEImage.MIMEImage
18\end{verbatim}
19
20or
21
22\begin{verbatim}
23from email.MIMEText import MIMEText
24\end{verbatim}
25
26Here are the classes:
27
28\begin{classdesc}{MIMEBase}{_maintype, _subtype, **_params}
29This is the base class for all the MIME-specific subclasses of
30\class{Message}. Ordinarily you won't create instances specifically
31of \class{MIMEBase}, although you could. \class{MIMEBase} is provided
32primarily as a convenient base class for more specific MIME-aware
33subclasses.
34
35\var{_maintype} is the \mailheader{Content-Type} major type
36(e.g. \mimetype{text} or \mimetype{image}), and \var{_subtype} is the
37\mailheader{Content-Type} minor type
38(e.g. \mimetype{plain} or \mimetype{gif}). \var{_params} is a parameter
39key/value dictionary and is passed directly to
40\method{Message.add_header()}.
41
42The \class{MIMEBase} class always adds a \mailheader{Content-Type} header
43(based on \var{_maintype}, \var{_subtype}, and \var{_params}), and a
44\mailheader{MIME-Version} header (always set to \code{1.0}).
45\end{classdesc}
46
47\begin{classdesc}{MIMENonMultipart}{}
48A subclass of \class{MIMEBase}, this is an intermediate base class for
49MIME messages that are not \mimetype{multipart}. The primary purpose
50of this class is to prevent the use of the \method{attach()} method,
51which only makes sense for \mimetype{multipart} messages. If
52\method{attach()} is called, a \exception{MultipartConversionError}
53exception is raised.
54
55\versionadded{2.2.2}
56\end{classdesc}
57
58\begin{classdesc}{MIMEMultipart}{\optional{subtype\optional{,
59 boundary\optional{, _subparts\optional{, _params}}}}}
60
61A subclass of \class{MIMEBase}, this is an intermediate base class for
62MIME messages that are \mimetype{multipart}. Optional \var{_subtype}
63defaults to \mimetype{mixed}, but can be used to specify the subtype
64of the message. A \mailheader{Content-Type} header of
65\mimetype{multipart/}\var{_subtype} will be added to the message
66object. A \mailheader{MIME-Version} header will also be added.
67
68Optional \var{boundary} is the multipart boundary string. When
69\code{None} (the default), the boundary is calculated when needed.
70
71\var{_subparts} is a sequence of initial subparts for the payload. It
72must be possible to convert this sequence to a list. You can always
73attach new subparts to the message by using the
74\method{Message.attach()} method.
75
76Additional parameters for the \mailheader{Content-Type} header are
77taken from the keyword arguments, or passed into the \var{_params}
78argument, which is a keyword dictionary.
79
80\versionadded{2.2.2}
81\end{classdesc}
82
83\begin{classdesc}{MIMEAudio}{_audiodata\optional{, _subtype\optional{,
84 _encoder\optional{, **_params}}}}
85
86A subclass of \class{MIMENonMultipart}, the \class{MIMEAudio} class
87is used to create MIME message objects of major type \mimetype{audio}.
88\var{_audiodata} is a string containing the raw audio data. If this
89data can be decoded by the standard Python module \refmodule{sndhdr},
90then the subtype will be automatically included in the
91\mailheader{Content-Type} header. Otherwise you can explicitly specify the
92audio subtype via the \var{_subtype} parameter. If the minor type could
93not be guessed and \var{_subtype} was not given, then \exception{TypeError}
94is raised.
95
96Optional \var{_encoder} is a callable (i.e. function) which will
97perform the actual encoding of the audio data for transport. This
98callable takes one argument, which is the \class{MIMEAudio} instance.
99It should use \method{get_payload()} and \method{set_payload()} to
100change the payload to encoded form. It should also add any
101\mailheader{Content-Transfer-Encoding} or other headers to the message
Barry Warsaw5db478f2002-10-01 04:33:16 +0000102object as necessary. The default encoding is base64. See the
Barry Warsaw5b9da892002-10-01 01:05:52 +0000103\refmodule{email.Encoders} module for a list of the built-in encoders.
104
105\var{_params} are passed straight through to the base class constructor.
106\end{classdesc}
107
108\begin{classdesc}{MIMEImage}{_imagedata\optional{, _subtype\optional{,
109 _encoder\optional{, **_params}}}}
110
111A subclass of \class{MIMENonMultipart}, the \class{MIMEImage} class is
112used to create MIME message objects of major type \mimetype{image}.
113\var{_imagedata} is a string containing the raw image data. If this
114data can be decoded by the standard Python module \refmodule{imghdr},
115then the subtype will be automatically included in the
116\mailheader{Content-Type} header. Otherwise you can explicitly specify the
117image subtype via the \var{_subtype} parameter. If the minor type could
118not be guessed and \var{_subtype} was not given, then \exception{TypeError}
119is raised.
120
121Optional \var{_encoder} is a callable (i.e. function) which will
122perform the actual encoding of the image data for transport. This
123callable takes one argument, which is the \class{MIMEImage} instance.
124It should use \method{get_payload()} and \method{set_payload()} to
125change the payload to encoded form. It should also add any
126\mailheader{Content-Transfer-Encoding} or other headers to the message
Barry Warsaw5db478f2002-10-01 04:33:16 +0000127object as necessary. The default encoding is base64. See the
Barry Warsaw5b9da892002-10-01 01:05:52 +0000128\refmodule{email.Encoders} module for a list of the built-in encoders.
129
130\var{_params} are passed straight through to the \class{MIMEBase}
131constructor.
132\end{classdesc}
133
134\begin{classdesc}{MIMEMessage}{_msg\optional{, _subtype}}
135A subclass of \class{MIMENonMultipart}, the \class{MIMEMessage} class
136is used to create MIME objects of main type \mimetype{message}.
137\var{_msg} is used as the payload, and must be an instance of class
138\class{Message} (or a subclass thereof), otherwise a
139\exception{TypeError} is raised.
140
141Optional \var{_subtype} sets the subtype of the message; it defaults
142to \mimetype{rfc822}.
143\end{classdesc}
144
Barry Warsawbb113862004-10-03 03:16:19 +0000145\begin{classdesc}{MIMEText}{_text\optional{, _subtype\optional{, _charset}}}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000146A subclass of \class{MIMENonMultipart}, the \class{MIMEText} class is
147used to create MIME objects of major type \mimetype{text}.
148\var{_text} is the string for the payload. \var{_subtype} is the
149minor type and defaults to \mimetype{plain}. \var{_charset} is the
150character set of the text and is passed as a parameter to the
151\class{MIMENonMultipart} constructor; it defaults to \code{us-ascii}. No
Barry Warsawa996d4f2003-03-11 05:03:25 +0000152guessing or encoding is performed on the text data.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000153
Barry Warsawbb113862004-10-03 03:16:19 +0000154\versionchanged[The previously deprecated \var{_encoding} argument has
155been removed. Encoding happens implicitly based on the \var{_charset}
156argument]{2.4}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000157\end{classdesc}