18.1.1. email: Representing an email message¶
The central class in the email package is the Message class,
imported from the email.message module. It is the base class for the
email object model. Message provides the core functionality for
setting and querying header fields, and for accessing message bodies.
Conceptually, a Message object consists of headers and payloads.
Headers are RFC 2822 style field names and values where the field name and
value are separated by a colon. The colon is not part of either the field name
or the field value.
Headers are stored and returned in case-preserving form but are matched
case-insensitively. There may also be a single envelope header, also known as
the Unix-From header or the From_ header. The payload is either a string
in the case of simple message objects or a list of Message objects for
MIME container documents (e.g. multipart/* and
message/rfc822).
Message objects provide a mapping style interface for accessing the
message headers, and an explicit interface for accessing both the headers and
the payload. It provides convenience methods for generating a flat text
representation of the message object tree, for accessing commonly used header
parameters, and for recursively walking over the object tree.
Here are the methods of the Message class:
-
class
email.message.Message¶ The constructor takes no arguments.
-
as_string([unixfrom])¶ Return the entire message flattened as a string. When optional unixfrom is
True, the envelope header is included in the returned string. unixfrom defaults toFalse. Flattening the message may trigger changes to theMessageif defaults need to be filled in to complete the transformation to a string (for example, MIME boundaries may be generated or modified).Note that this method is provided as a convenience and may not always format the message the way you want. For example, by default it mangles lines that begin with
From. For more flexibility, instantiate aGeneratorinstance and use itsflatten()method directly. For example:from cStringIO import StringIO from email.generator import Generator fp = StringIO() g = Generator(fp, mangle_from_=False, maxheaderlen=60) g.flatten(msg) text = fp.getvalue()
-
__str__()¶ Equivalent to
as_string(unixfrom=True).
-
is_multipart()¶ Return
Trueif the message’s payload is a list of sub-Messageobjects, otherwise returnFalse. Whenis_multipart()returns False, the payload should be a string object.
-
set_unixfrom(unixfrom)¶ Set the message’s envelope header to unixfrom, which should be a string.
-
get_unixfrom()¶ Return the message’s envelope header. Defaults to
Noneif the envelope header was never set.
-
attach(payload)¶ Add the given payload to the current payload, which must be
Noneor a list ofMessageobjects before the call. After the call, the payload will always be a list ofMessageobjects. If you want to set the payload to a scalar object (e.g. a string), useset_payload()instead.
-
get_payload([i[, decode]])¶ Return the current payload, which will be a list of
Messageobjects whenis_multipart()isTrue, or a string whenis_multipart()isFalse. If the payload is a list and you mutate the list object, you modify the message’s payload in place.With optional argument i,
get_payload()will return the i-th element of the payload, counting from zero, ifis_multipart()isTrue. AnIndexErrorwill be raised if i is less than 0 or greater than or equal to the number of items in the payload. If the payload is a string (i.e.is_multipart()isFalse) and i is given, aTypeErroris raised.Optional decode is a flag indicating whether the payload should be decoded or not, according to the Content-Transfer-Encoding header. When
Trueand the message is not a multipart, the payload will be decoded if this header’s value isquoted-printableorbase64. If some other encoding is used, or Content-Transfer-Encoding header is missing, or if the payload has bogus base64 data, the payload is returned as-is (undecoded). If the message is a multipart and the decode flag isTrue, thenNoneis returned. The default for decode isFalse.
-
set_payload(payload[, charset])¶ Set the entire message object’s payload to payload. It is the client’s responsibility to ensure the payload invariants. Optional charset sets the message’s default character set; see
set_charset()for details.Changed in version 2.2.2:
Changed in version 2.2.2: charset argument added.
-
-
set_charset(charset)¶ Set the character set of the payload to charset, which can either be a
Charsetinstance (seeemail.charset), a string naming a character set, orNone. If it is a string, it will be converted to aCharsetinstance. If charset isNone, thecharsetparameter will be removed from the Content-Type header. Anything else will generate aTypeError.The message will be assumed to be of type text/*, with the payload either in unicode or encoded with charset.input_charset. It will be encoded or converted to charset.output_charset and transfer encoded properly, if needed, when generating the plain text representation of the message. MIME headers (MIME-Version, Content-Type, Content-Transfer-Encoding) will be added as needed.
New in version 2.2.2:
New in version 2.2.2.