💾 Archived View for tris.fyi › pydoc › email.message captured on 2023-04-26 at 13:36:32. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2022-07-16)
-=-=-=-=-=-=-
Basic message object for the email package object model.
Buffered I/O implementation using an in-memory bytes buffer.
close(self, /) Disable all I/O operations.
detach(self, /) Disconnect this buffer from its underlying raw stream and return it. After the raw stream has been detached, the buffer is in an unusable state.
fileno(self, /) Returns underlying file descriptor if one exists. OSError is raised if the IO object does not use a file descriptor.
flush(self, /) Does nothing.
getbuffer(self, /) Get a read-write view over the contents of the BytesIO object.
getvalue(self, /) Retrieve the entire contents of the BytesIO object.
isatty(self, /) Always returns False. BytesIO objects are not connected to a TTY-like device.
read(self, size=-1, /) Read at most size bytes, returned as a bytes object. If the size argument is negative, read until EOF is reached. Return an empty bytes object at EOF.
read1(self, size=-1, /) Read at most size bytes, returned as a bytes object. If the size argument is negative or omitted, read until EOF is reached. Return an empty bytes object at EOF.
readable(self, /) Returns True if the IO object can be read.
readinto(self, buffer, /) Read bytes into buffer. Returns number of bytes read (0 for EOF), or None if the object is set not to block and has no data to read.
readinto1(self, buffer, /)
readline(self, size=-1, /) Next line from the file, as a bytes object. Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line may be returned then). Return an empty bytes object at EOF.
readlines(self, size=None, /) List of bytes objects, each a line from the file. Call readline() repeatedly and return a list of the lines so read. The optional size argument, if given, is an approximate bound on the total number of bytes in the lines returned.
seek(self, pos, whence=0, /) Change stream position. Seek to byte offset pos relative to position indicated by whence: 0 Start of stream (the default). pos should be >= 0; 1 Current position - pos may be negative; 2 End of stream - pos usually negative. Returns the new absolute position.
seekable(self, /) Returns True if the IO object can be seeked.
tell(self, /) Current file position, an integer.
truncate(self, size=None, /) Truncate the file to at most size bytes. Size defaults to the current file position, as returned by tell(). The current file position is unchanged. Returns the new size.
writable(self, /) Returns True if the IO object can be written.
write(self, b, /) Write bytes to file. Return the number of bytes written.
writelines(self, lines, /) Write lines to the file. Note that newlines are not added. lines can be any iterable object producing bytes-like objects. This is equivalent to calling write() for each element.
closed = <attribute 'closed' of '_io.BytesIO' objects> True if the file is closed.
Map character sets to their email properties. This class provides information about the requirements imposed on email for a specific character set. It also provides convenience routines for converting between character sets, given the availability of the applicable codecs. Given a character set, it will do its best to provide information on how to use that character set in an email in an RFC-compliant way. Certain character sets must be encoded with quoted-printable or base64 when used in email headers or bodies. Certain character sets must be converted outright, and are not allowed in email. Instances of this module expose the following information about a character set: input_charset: The initial character set specified. Common aliases are converted to their `official' email names (e.g. latin_1 is converted to iso-8859-1). Defaults to 7-bit us-ascii. header_encoding: If the character set must be encoded before it can be used in an email header, this attribute will be set to charset.QP (for quoted-printable), charset.BASE64 (for base64 encoding), or charset.SHORTEST for the shortest of QP or BASE64 encoding. Otherwise, it will be None. body_encoding: Same as header_encoding, but describes the encoding for the mail message's body, which indeed may be different than the header encoding. charset.SHORTEST is not allowed for body_encoding. output_charset: Some character sets must be converted before they can be used in email headers or bodies. If the input_charset is one of them, this attribute will contain the name of the charset output will be converted to. Otherwise, it will be None. input_codec: The name of the Python codec used to convert the input_charset to Unicode. If no conversion codec is necessary, this attribute will be None. output_codec: The name of the Python codec used to convert Unicode to the output_charset. If no conversion codec is necessary, this attribute will have the same value as the input_codec.
body_encode(self, string) Body-encode a string by converting it first to bytes. The type of encoding (base64 or quoted-printable) will be based on self.body_encoding. If body_encoding is None, we assume the output charset is a 7bit encoding, so re-encoding the decoded string using the ascii codec produces the correct string version of the content.
get_body_encoding(self) Return the content-transfer-encoding used for body encoding. This is either the string `quoted-printable' or `base64' depending on the encoding used, or it is a function in which case you should call the function with a single argument, the Message object being encoded. The function should then set the Content-Transfer-Encoding header itself to whatever is appropriate. Returns "quoted-printable" if self.body_encoding is QP. Returns "base64" if self.body_encoding is BASE64. Returns conversion function otherwise.
get_output_charset(self) Return the output character set. This is self.output_charset if that is not None, otherwise it is self.input_charset.
header_encode(self, string) Header-encode a string by converting it first to bytes. The type of encoding (base64 or quoted-printable) will be based on this charset's `header_encoding`. :param string: A unicode string for the header. It must be possible to encode this string to bytes using the character set's output codec. :return: The encoded string, with RFC 2047 chrome.
header_encode_lines(self, string, maxlengths) Header-encode a string by converting it first to bytes. This is similar to `header_encode()` except that the string is fit into maximum line lengths as given by the argument. :param string: A unicode string for the header. It must be possible to encode this string to bytes using the character set's output codec. :param maxlengths: Maximum line length iterator. Each element returned from this iterator will provide the next maximum line length. This parameter is used as an argument to built-in next() and should never be exhausted. The maximum line lengths should not count the RFC 2047 chrome. These line lengths are only a hint; the splitter does the best it can. :return: Lines of encoded strings, each with RFC 2047 chrome.
add_alternative(self, *args, **kw)
add_attachment(self, *args, **kw)
add_header(self, _name, _value, **_params) Extended header setting. name is the header field to add. keyword arguments can be used to set additional parameters for the header field, with underscores converted to dashes. Normally the parameter will be added as key="value" unless value is None, in which case only the key will be added. If a parameter value contains non-ASCII characters it can be specified as a three-tuple of (charset, language, value), in which case it will be encoded according to RFC2231 rules. Otherwise it will be encoded using the utf-8 charset and a language of ''. Examples: msg.add_header('content-disposition', 'attachment', filename='bud.gif') msg.add_header('content-disposition', 'attachment', filename=('utf-8', '', Fußballer.ppt')) msg.add_header('content-disposition', 'attachment', filename='Fußballer.ppt'))
add_related(self, *args, **kw)
as_bytes(self, unixfrom=False, policy=None) Return the entire formatted message as a bytes object. Optional 'unixfrom', when true, means include the Unix From_ envelope header. 'policy' is passed to the BytesGenerator instance used to serialize the message; if not specified the policy associated with the message instance is used.
as_string(self, unixfrom=False, maxheaderlen=None, policy=None) Return the entire formatted message as a string. Optional 'unixfrom', when true, means include the Unix From_ envelope header. maxheaderlen is retained for backward compatibility with the base Message class, but defaults to None, meaning that the policy value for max_line_length controls the header maximum length. 'policy' is passed to the Generator instance used to serialize the message; if it is not specified the policy associated with the message instance is used.
attach(self, payload) Add the given payload to the current payload. The current payload will always be a list of objects after this method is called. If you want to set the payload to a scalar object, use set_payload() instead.
clear(self)
clear_content(self)
del_param(self, param, header='content-type', requote=True) Remove the given parameter completely from the Content-Type header. The header will be re-written in place without the parameter or its value. All values will be quoted as necessary unless requote is False. Optional header specifies an alternative to the Content-Type header.
get(self, name, failobj=None) Get a header value. Like __getitem__() but return failobj instead of None when the field is missing.
get_all(self, name, failobj=None) Return a list of all the values for the named field. These will be sorted in the order they appeared in the original message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. If no such fields exist, failobj is returned (defaults to None).
get_body(self, preferencelist=('related', 'html', 'plain')) Return best candidate mime part for display as 'body' of message. Do a depth first search, starting with self, looking for the first part matching each of the items in preferencelist, and return the part corresponding to the first item that has a match, or None if no items have a match. If 'related' is not included in preferencelist, consider the root part of any multipart/related encountered as a candidate match. Ignore parts with 'Content-Disposition: attachment'.
get_boundary(self, failobj=None) Return the boundary associated with the payload if present. The boundary is extracted from the Content-Type header's `boundary' parameter, and it is unquoted.
get_charset(self) Return the Charset instance associated with the message's payload.
get_charsets(self, failobj=None) Return a list containing the charset(s) used in this message. The returned list of items describes the Content-Type headers' charset parameter for this message and all the subparts in its payload. Each item will either be a string (the value of the charset parameter in the Content-Type header of that part) or the value of the 'failobj' parameter (defaults to None), if the part does not have a main MIME type of "text", or the charset is not defined. The list will contain one string for each part of the message, plus one for the container message (i.e. self), so that a non-multipart message will still return a list of length 1.
get_content(self, *args, content_manager=None, **kw)
get_content_charset(self, failobj=None) Return the charset parameter of the Content-Type header. The returned string is always coerced to lower case. If there is no Content-Type header, or if that header has no charset parameter, failobj is returned.
get_content_disposition(self) Return the message's content-disposition if it exists, or None. The return values can be either 'inline', 'attachment' or None according to the rfc2183.
get_content_maintype(self) Return the message's main content type. This is the `maintype' part of the string returned by get_content_type().
get_content_subtype(self) Returns the message's sub-content type. This is the `subtype' part of the string returned by get_content_type().
get_content_type(self) Return the message's content type. The returned string is coerced to lower case of the form `maintype/subtype'. If there was no Content-Type header in the message, the default type as given by get_default_type() will be returned. Since according to RFC 2045, messages always have a default type this will always return a value. RFC 2045 defines a message's default type to be text/plain unless it appears inside a multipart/digest container, in which case it would be message/rfc822.
get_default_type(self) Return the `default' content type. Most messages have a default content type of text/plain, except for messages that are subparts of multipart/digest containers. Such subparts have a default content type of message/rfc822.
get_filename(self, failobj=None) Return the filename associated with the payload if present. The filename is extracted from the Content-Disposition header's `filename' parameter, and it is unquoted. If that header is missing the `filename' parameter, this method falls back to looking for the `name' parameter.
get_param(self, param, failobj=None, header='content-type', unquote=True) Return the parameter value if found in the Content-Type header. Optional failobj is the object to return if there is no Content-Type header, or the Content-Type header has no such parameter. Optional header is the header to search instead of Content-Type. Parameter keys are always compared case insensitively. The return value can either be a string, or a 3-tuple if the parameter was RFC 2231 encoded. When it's a 3-tuple, the elements of the value are of the form (CHARSET, LANGUAGE, VALUE). Note that both CHARSET and LANGUAGE can be None, in which case you should consider VALUE to be encoded in the us-ascii charset. You can usually ignore LANGUAGE. The parameter value (either the returned string, or the VALUE item in the 3-tuple) is always unquoted, unless unquote is set to False. If your application doesn't care whether the parameter was RFC 2231 encoded, it can turn the return value into a string as follows: rawparam = msg.get_param('foo') param = email.utils.collapse_rfc2231_value(rawparam)
get_params(self, failobj=None, header='content-type', unquote=True) Return the message's Content-Type parameters, as a list. The elements of the returned list are 2-tuples of key/value pairs, as split on the `=' sign. The left hand side of the `=' is the key, while the right hand side is the value. If there is no `=' sign in the parameter the value is the empty string. The value is as described in the get_param() method. Optional failobj is the object to return if there is no Content-Type header. Optional header is the header to search instead of Content-Type. If unquote is True, the value is unquoted.
get_payload(self, i=None, decode=False) Return a reference to the payload. The payload will either be a list object or a string. If you mutate the list object, you modify the message's payload in place. Optional i returns that index into the payload. Optional decode is a flag indicating whether the payload should be decoded or not, according to the Content-Transfer-Encoding header (default is False). When True and the message is not a multipart, the payload will be decoded if this header's value is `quoted-printable' or `base64'. If some other encoding is used, or the header is missing, or if the payload has bogus data (i.e. bogus base64 or uuencoded data), the payload is returned as-is. If the message is a multipart and the decode flag is True, then None is returned.
get_unixfrom(self)
is_attachment(self)
is_multipart(self) Return True if the message consists of multiple parts.
items(self) Get all the message's header fields and values. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.
iter_attachments(self) Return an iterator over the non-main parts of a multipart. Skip the first of each occurrence of text/plain, text/html, multipart/related, or multipart/alternative in the multipart (unless they have a 'Content-Disposition: attachment' header) and include all remaining subparts in the returned iterator. When applied to a multipart/related, return all parts except the root part. Return an empty iterator when applied to a multipart/alternative or a non-multipart.
iter_parts(self) Return an iterator over all immediate subparts of a multipart. Return an empty iterator for a non-multipart.
keys(self) Return a list of all the message's header field names. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.
make_alternative(self, boundary=None)
make_mixed(self, boundary=None)
make_related(self, boundary=None)
raw_items(self) Return the (name, value) header pairs without modification. This is an "internal" API, intended only for use by a generator.
replace_header(self, _name, _value) Replace a header. Replace the first matching header found in the message, retaining header order and case. If no matching header was found, a KeyError is raised.
set_boundary(self, boundary) Set the boundary parameter in Content-Type to 'boundary'. This is subtly different than deleting the Content-Type header and adding a new one with a new boundary parameter via add_header(). The main difference is that using the set_boundary() method preserves the order of the Content-Type header in the original message. HeaderParseError is raised if the message has no Content-Type header.
set_charset(self, charset) Set the charset of the payload to a given character set. charset can be a Charset instance, a string naming a character set, or None. If it is a string it will be converted to a Charset instance. If charset is None, the charset parameter will be removed from the Content-Type field. Anything else will generate a TypeError. The message will be assumed to be of type text/* encoded with charset.input_charset. It will be converted to charset.output_charset and 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.
set_content(self, *args, **kw)
set_default_type(self, ctype) Set the `default' content type. ctype should be either "text/plain" or "message/rfc822", although this is not enforced. The default content type is not stored in the Content-Type header.
set_param(self, param, value, header='Content-Type', requote=True, charset=None, language='', replace=False) Set a parameter in the Content-Type header. If the parameter already exists in the header, its value will be replaced with the new value. If header is Content-Type and has not yet been defined for this message, it will be set to "text/plain" and the new parameter and value will be appended as per RFC 2045. An alternate header can be specified in the header argument, and all parameters will be quoted as necessary unless requote is False. If charset is specified, the parameter will be encoded according to RFC 2231. Optional language specifies the RFC 2231 language, defaulting to the empty string. Both charset and language should be strings.
set_payload(self, payload, charset=None) Set the payload to the given value. Optional charset sets the message's default character set. See set_charset() for details.
set_raw(self, name, value) Store name and value in the model without modification. This is an "internal" API, intended only for use by a parser.
set_type(self, type, header='Content-Type', requote=True) Set the main type and subtype for the Content-Type header. type must be a string in the form "maintype/subtype", otherwise a ValueError is raised. This method replaces the Content-Type header, keeping all the parameters in place. If requote is False, this leaves the existing header's quoting as is. Otherwise, the parameters will be quoted (the default). An alternative header can be specified in the header argument. When the Content-Type header is set, we'll always also add a MIME-Version header.
set_unixfrom(self, unixfrom)
values(self) Return a list of all the message's header values. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.
walk(self) Walk over the message tree, yielding each subpart. The walk is performed in depth-first order. This method is a generator.
add_alternative(self, *args, **kw)
add_attachment(self, *args, **kw)
add_header(self, _name, _value, **_params) Extended header setting. name is the header field to add. keyword arguments can be used to set additional parameters for the header field, with underscores converted to dashes. Normally the parameter will be added as key="value" unless value is None, in which case only the key will be added. If a parameter value contains non-ASCII characters it can be specified as a three-tuple of (charset, language, value), in which case it will be encoded according to RFC2231 rules. Otherwise it will be encoded using the utf-8 charset and a language of ''. Examples: msg.add_header('content-disposition', 'attachment', filename='bud.gif') msg.add_header('content-disposition', 'attachment', filename=('utf-8', '', Fußballer.ppt')) msg.add_header('content-disposition', 'attachment', filename='Fußballer.ppt'))
add_related(self, *args, **kw)
as_bytes(self, unixfrom=False, policy=None) Return the entire formatted message as a bytes object. Optional 'unixfrom', when true, means include the Unix From_ envelope header. 'policy' is passed to the BytesGenerator instance used to serialize the message; if not specified the policy associated with the message instance is used.
as_string(self, unixfrom=False, maxheaderlen=None, policy=None) Return the entire formatted message as a string. Optional 'unixfrom', when true, means include the Unix From_ envelope header. maxheaderlen is retained for backward compatibility with the base Message class, but defaults to None, meaning that the policy value for max_line_length controls the header maximum length. 'policy' is passed to the Generator instance used to serialize the message; if it is not specified the policy associated with the message instance is used.
attach(self, payload) Add the given payload to the current payload. The current payload will always be a list of objects after this method is called. If you want to set the payload to a scalar object, use set_payload() instead.
clear(self)
clear_content(self)
del_param(self, param, header='content-type', requote=True) Remove the given parameter completely from the Content-Type header. The header will be re-written in place without the parameter or its value. All values will be quoted as necessary unless requote is False. Optional header specifies an alternative to the Content-Type header.
get(self, name, failobj=None) Get a header value. Like __getitem__() but return failobj instead of None when the field is missing.
get_all(self, name, failobj=None) Return a list of all the values for the named field. These will be sorted in the order they appeared in the original message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. If no such fields exist, failobj is returned (defaults to None).
get_body(self, preferencelist=('related', 'html', 'plain')) Return best candidate mime part for display as 'body' of message. Do a depth first search, starting with self, looking for the first part matching each of the items in preferencelist, and return the part corresponding to the first item that has a match, or None if no items have a match. If 'related' is not included in preferencelist, consider the root part of any multipart/related encountered as a candidate match. Ignore parts with 'Content-Disposition: attachment'.
get_boundary(self, failobj=None) Return the boundary associated with the payload if present. The boundary is extracted from the Content-Type header's `boundary' parameter, and it is unquoted.
get_charset(self) Return the Charset instance associated with the message's payload.
get_charsets(self, failobj=None) Return a list containing the charset(s) used in this message. The returned list of items describes the Content-Type headers' charset parameter for this message and all the subparts in its payload. Each item will either be a string (the value of the charset parameter in the Content-Type header of that part) or the value of the 'failobj' parameter (defaults to None), if the part does not have a main MIME type of "text", or the charset is not defined. The list will contain one string for each part of the message, plus one for the container message (i.e. self), so that a non-multipart message will still return a list of length 1.
get_content(self, *args, content_manager=None, **kw)
get_content_charset(self, failobj=None) Return the charset parameter of the Content-Type header. The returned string is always coerced to lower case. If there is no Content-Type header, or if that header has no charset parameter, failobj is returned.
get_content_disposition(self) Return the message's content-disposition if it exists, or None. The return values can be either 'inline', 'attachment' or None according to the rfc2183.
get_content_maintype(self) Return the message's main content type. This is the `maintype' part of the string returned by get_content_type().
get_content_subtype(self) Returns the message's sub-content type. This is the `subtype' part of the string returned by get_content_type().
get_content_type(self) Return the message's content type. The returned string is coerced to lower case of the form `maintype/subtype'. If there was no Content-Type header in the message, the default type as given by get_default_type() will be returned. Since according to RFC 2045, messages always have a default type this will always return a value. RFC 2045 defines a message's default type to be text/plain unless it appears inside a multipart/digest container, in which case it would be message/rfc822.
get_default_type(self) Return the `default' content type. Most messages have a default content type of text/plain, except for messages that are subparts of multipart/digest containers. Such subparts have a default content type of message/rfc822.
get_filename(self, failobj=None) Return the filename associated with the payload if present. The filename is extracted from the Content-Disposition header's `filename' parameter, and it is unquoted. If that header is missing the `filename' parameter, this method falls back to looking for the `name' parameter.
get_param(self, param, failobj=None, header='content-type', unquote=True) Return the parameter value if found in the Content-Type header. Optional failobj is the object to return if there is no Content-Type header, or the Content-Type header has no such parameter. Optional header is the header to search instead of Content-Type. Parameter keys are always compared case insensitively. The return value can either be a string, or a 3-tuple if the parameter was RFC 2231 encoded. When it's a 3-tuple, the elements of the value are of the form (CHARSET, LANGUAGE, VALUE). Note that both CHARSET and LANGUAGE can be None, in which case you should consider VALUE to be encoded in the us-ascii charset. You can usually ignore LANGUAGE. The parameter value (either the returned string, or the VALUE item in the 3-tuple) is always unquoted, unless unquote is set to False. If your application doesn't care whether the parameter was RFC 2231 encoded, it can turn the return value into a string as follows: rawparam = msg.get_param('foo') param = email.utils.collapse_rfc2231_value(rawparam)
get_params(self, failobj=None, header='content-type', unquote=True) Return the message's Content-Type parameters, as a list. The elements of the returned list are 2-tuples of key/value pairs, as split on the `=' sign. The left hand side of the `=' is the key, while the right hand side is the value. If there is no `=' sign in the parameter the value is the empty string. The value is as described in the get_param() method. Optional failobj is the object to return if there is no Content-Type header. Optional header is the header to search instead of Content-Type. If unquote is True, the value is unquoted.
get_payload(self, i=None, decode=False) Return a reference to the payload. The payload will either be a list object or a string. If you mutate the list object, you modify the message's payload in place. Optional i returns that index into the payload. Optional decode is a flag indicating whether the payload should be decoded or not, according to the Content-Transfer-Encoding header (default is False). When True and the message is not a multipart, the payload will be decoded if this header's value is `quoted-printable' or `base64'. If some other encoding is used, or the header is missing, or if the payload has bogus data (i.e. bogus base64 or uuencoded data), the payload is returned as-is. If the message is a multipart and the decode flag is True, then None is returned.
get_unixfrom(self)
is_attachment(self)
is_multipart(self) Return True if the message consists of multiple parts.
items(self) Get all the message's header fields and values. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.
iter_attachments(self) Return an iterator over the non-main parts of a multipart. Skip the first of each occurrence of text/plain, text/html, multipart/related, or multipart/alternative in the multipart (unless they have a 'Content-Disposition: attachment' header) and include all remaining subparts in the returned iterator. When applied to a multipart/related, return all parts except the root part. Return an empty iterator when applied to a multipart/alternative or a non-multipart.
iter_parts(self) Return an iterator over all immediate subparts of a multipart. Return an empty iterator for a non-multipart.
keys(self) Return a list of all the message's header field names. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.
make_alternative(self, boundary=None)
make_mixed(self, boundary=None)
make_related(self, boundary=None)
raw_items(self) Return the (name, value) header pairs without modification. This is an "internal" API, intended only for use by a generator.
replace_header(self, _name, _value) Replace a header. Replace the first matching header found in the message, retaining header order and case. If no matching header was found, a KeyError is raised.
set_boundary(self, boundary) Set the boundary parameter in Content-Type to 'boundary'. This is subtly different than deleting the Content-Type header and adding a new one with a new boundary parameter via add_header(). The main difference is that using the set_boundary() method preserves the order of the Content-Type header in the original message. HeaderParseError is raised if the message has no Content-Type header.
set_charset(self, charset) Set the charset of the payload to a given character set. charset can be a Charset instance, a string naming a character set, or None. If it is a string it will be converted to a Charset instance. If charset is None, the charset parameter will be removed from the Content-Type field. Anything else will generate a TypeError. The message will be assumed to be of type text/* encoded with charset.input_charset. It will be converted to charset.output_charset and 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.
set_content(self, *args, content_manager=None, **kw)
set_default_type(self, ctype) Set the `default' content type. ctype should be either "text/plain" or "message/rfc822", although this is not enforced. The default content type is not stored in the Content-Type header.
set_param(self, param, value, header='Content-Type', requote=True, charset=None, language='', replace=False) Set a parameter in the Content-Type header. If the parameter already exists in the header, its value will be replaced with the new value. If header is Content-Type and has not yet been defined for this message, it will be set to "text/plain" and the new parameter and value will be appended as per RFC 2045. An alternate header can be specified in the header argument, and all parameters will be quoted as necessary unless requote is False. If charset is specified, the parameter will be encoded according to RFC 2231. Optional language specifies the RFC 2231 language, defaulting to the empty string. Both charset and language should be strings.
set_payload(self, payload, charset=None) Set the payload to the given value. Optional charset sets the message's default character set. See set_charset() for details.
set_raw(self, name, value) Store name and value in the model without modification. This is an "internal" API, intended only for use by a parser.
set_type(self, type, header='Content-Type', requote=True) Set the main type and subtype for the Content-Type header. type must be a string in the form "maintype/subtype", otherwise a ValueError is raised. This method replaces the Content-Type header, keeping all the parameters in place. If requote is False, this leaves the existing header's quoting as is. Otherwise, the parameters will be quoted (the default). An alternative header can be specified in the header argument. When the Content-Type header is set, we'll always also add a MIME-Version header.
set_unixfrom(self, unixfrom)
values(self) Return a list of all the message's header values. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.
walk(self) Walk over the message tree, yielding each subpart. The walk is performed in depth-first order. This method is a generator.
Basic message object. A message object is defined as something that has a bunch of RFC 2822 headers and a payload. It may optionally have an envelope header (a.k.a. Unix-From or From_ header). If the message is a container (i.e. a multipart or a message/rfc822), then the payload is a list of Message objects, otherwise it is a string. Message objects implement part of the `mapping' interface, which assumes there is exactly one occurrence of the header per message. Some headers do in fact appear multiple times (e.g. Received) and for those headers, you must use the explicit API to set or get all the headers. Not all of the mapping methods are implemented.
add_header(self, _name, _value, **_params) Extended header setting. name is the header field to add. keyword arguments can be used to set additional parameters for the header field, with underscores converted to dashes. Normally the parameter will be added as key="value" unless value is None, in which case only the key will be added. If a parameter value contains non-ASCII characters it can be specified as a three-tuple of (charset, language, value), in which case it will be encoded according to RFC2231 rules. Otherwise it will be encoded using the utf-8 charset and a language of ''. Examples: msg.add_header('content-disposition', 'attachment', filename='bud.gif') msg.add_header('content-disposition', 'attachment', filename=('utf-8', '', Fußballer.ppt')) msg.add_header('content-disposition', 'attachment', filename='Fußballer.ppt'))
as_bytes(self, unixfrom=False, policy=None) Return the entire formatted message as a bytes object. Optional 'unixfrom', when true, means include the Unix From_ envelope header. 'policy' is passed to the BytesGenerator instance used to serialize the message; if not specified the policy associated with the message instance is used.
as_string(self, unixfrom=False, maxheaderlen=0, policy=None) Return the entire formatted message as a string. Optional 'unixfrom', when true, means include the Unix From_ envelope header. For backward compatibility reasons, if maxheaderlen is not specified it defaults to 0, so you must override it explicitly if you want a different maxheaderlen. 'policy' is passed to the Generator instance used to serialize the message; if it is not specified the policy associated with the message instance is used. If the message object contains binary data that is not encoded according to RFC standards, the non-compliant data will be replaced by unicode "unknown character" code points.
attach(self, payload) Add the given payload to the current payload. The current payload will always be a list of objects after this method is called. If you want to set the payload to a scalar object, use set_payload() instead.
del_param(self, param, header='content-type', requote=True) Remove the given parameter completely from the Content-Type header. The header will be re-written in place without the parameter or its value. All values will be quoted as necessary unless requote is False. Optional header specifies an alternative to the Content-Type header.
get(self, name, failobj=None) Get a header value. Like __getitem__() but return failobj instead of None when the field is missing.
get_all(self, name, failobj=None) Return a list of all the values for the named field. These will be sorted in the order they appeared in the original message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. If no such fields exist, failobj is returned (defaults to None).
get_boundary(self, failobj=None) Return the boundary associated with the payload if present. The boundary is extracted from the Content-Type header's `boundary' parameter, and it is unquoted.
get_charset(self) Return the Charset instance associated with the message's payload.
get_charsets(self, failobj=None) Return a list containing the charset(s) used in this message. The returned list of items describes the Content-Type headers' charset parameter for this message and all the subparts in its payload. Each item will either be a string (the value of the charset parameter in the Content-Type header of that part) or the value of the 'failobj' parameter (defaults to None), if the part does not have a main MIME type of "text", or the charset is not defined. The list will contain one string for each part of the message, plus one for the container message (i.e. self), so that a non-multipart message will still return a list of length 1.
get_content_charset(self, failobj=None) Return the charset parameter of the Content-Type header. The returned string is always coerced to lower case. If there is no Content-Type header, or if that header has no charset parameter, failobj is returned.
get_content_disposition(self) Return the message's content-disposition if it exists, or None. The return values can be either 'inline', 'attachment' or None according to the rfc2183.
get_content_maintype(self) Return the message's main content type. This is the `maintype' part of the string returned by get_content_type().
get_content_subtype(self) Returns the message's sub-content type. This is the `subtype' part of the string returned by get_content_type().
get_content_type(self) Return the message's content type. The returned string is coerced to lower case of the form `maintype/subtype'. If there was no Content-Type header in the message, the default type as given by get_default_type() will be returned. Since according to RFC 2045, messages always have a default type this will always return a value. RFC 2045 defines a message's default type to be text/plain unless it appears inside a multipart/digest container, in which case it would be message/rfc822.
get_default_type(self) Return the `default' content type. Most messages have a default content type of text/plain, except for messages that are subparts of multipart/digest containers. Such subparts have a default content type of message/rfc822.
get_filename(self, failobj=None) Return the filename associated with the payload if present. The filename is extracted from the Content-Disposition header's `filename' parameter, and it is unquoted. If that header is missing the `filename' parameter, this method falls back to looking for the `name' parameter.
get_param(self, param, failobj=None, header='content-type', unquote=True) Return the parameter value if found in the Content-Type header. Optional failobj is the object to return if there is no Content-Type header, or the Content-Type header has no such parameter. Optional header is the header to search instead of Content-Type. Parameter keys are always compared case insensitively. The return value can either be a string, or a 3-tuple if the parameter was RFC 2231 encoded. When it's a 3-tuple, the elements of the value are of the form (CHARSET, LANGUAGE, VALUE). Note that both CHARSET and LANGUAGE can be None, in which case you should consider VALUE to be encoded in the us-ascii charset. You can usually ignore LANGUAGE. The parameter value (either the returned string, or the VALUE item in the 3-tuple) is always unquoted, unless unquote is set to False. If your application doesn't care whether the parameter was RFC 2231 encoded, it can turn the return value into a string as follows: rawparam = msg.get_param('foo') param = email.utils.collapse_rfc2231_value(rawparam)
get_params(self, failobj=None, header='content-type', unquote=True) Return the message's Content-Type parameters, as a list. The elements of the returned list are 2-tuples of key/value pairs, as split on the `=' sign. The left hand side of the `=' is the key, while the right hand side is the value. If there is no `=' sign in the parameter the value is the empty string. The value is as described in the get_param() method. Optional failobj is the object to return if there is no Content-Type header. Optional header is the header to search instead of Content-Type. If unquote is True, the value is unquoted.
get_payload(self, i=None, decode=False) Return a reference to the payload. The payload will either be a list object or a string. If you mutate the list object, you modify the message's payload in place. Optional i returns that index into the payload. Optional decode is a flag indicating whether the payload should be decoded or not, according to the Content-Transfer-Encoding header (default is False). When True and the message is not a multipart, the payload will be decoded if this header's value is `quoted-printable' or `base64'. If some other encoding is used, or the header is missing, or if the payload has bogus data (i.e. bogus base64 or uuencoded data), the payload is returned as-is. If the message is a multipart and the decode flag is True, then None is returned.
get_unixfrom(self)
is_multipart(self) Return True if the message consists of multiple parts.
items(self) Get all the message's header fields and values. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.
keys(self) Return a list of all the message's header field names. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.
raw_items(self) Return the (name, value) header pairs without modification. This is an "internal" API, intended only for use by a generator.
replace_header(self, _name, _value) Replace a header. Replace the first matching header found in the message, retaining header order and case. If no matching header was found, a KeyError is raised.
set_boundary(self, boundary) Set the boundary parameter in Content-Type to 'boundary'. This is subtly different than deleting the Content-Type header and adding a new one with a new boundary parameter via add_header(). The main difference is that using the set_boundary() method preserves the order of the Content-Type header in the original message. HeaderParseError is raised if the message has no Content-Type header.
set_charset(self, charset) Set the charset of the payload to a given character set. charset can be a Charset instance, a string naming a character set, or None. If it is a string it will be converted to a Charset instance. If charset is None, the charset parameter will be removed from the Content-Type field. Anything else will generate a TypeError. The message will be assumed to be of type text/* encoded with charset.input_charset. It will be converted to charset.output_charset and 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.
set_default_type(self, ctype) Set the `default' content type. ctype should be either "text/plain" or "message/rfc822", although this is not enforced. The default content type is not stored in the Content-Type header.
set_param(self, param, value, header='Content-Type', requote=True, charset=None, language='', replace=False) Set a parameter in the Content-Type header. If the parameter already exists in the header, its value will be replaced with the new value. If header is Content-Type and has not yet been defined for this message, it will be set to "text/plain" and the new parameter and value will be appended as per RFC 2045. An alternate header can be specified in the header argument, and all parameters will be quoted as necessary unless requote is False. If charset is specified, the parameter will be encoded according to RFC 2231. Optional language specifies the RFC 2231 language, defaulting to the empty string. Both charset and language should be strings.
set_payload(self, payload, charset=None) Set the payload to the given value. Optional charset sets the message's default character set. See set_charset() for details.
set_raw(self, name, value) Store name and value in the model without modification. This is an "internal" API, intended only for use by a parser.
set_type(self, type, header='Content-Type', requote=True) Set the main type and subtype for the Content-Type header. type must be a string in the form "maintype/subtype", otherwise a ValueError is raised. This method replaces the Content-Type header, keeping all the parameters in place. If requote is False, this leaves the existing header's quoting as is. Otherwise, the parameters will be quoted (the default). An alternative header can be specified in the header argument. When the Content-Type header is set, we'll always also add a MIME-Version header.
set_unixfrom(self, unixfrom)
values(self) Return a list of all the message's header values. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.
walk(self) Walk over the message tree, yielding each subpart. The walk is performed in depth-first order. This method is a generator.
Controls for how messages are interpreted and formatted. Most of the classes and many of the methods in the email package accept Policy objects as parameters. A Policy object contains a set of values and functions that control how input is interpreted and how output is rendered. For example, the parameter 'raise_on_defect' controls whether or not an RFC violation results in an error being raised or not, while 'max_line_length' controls the maximum length of output lines when a Message is serialized. Any valid attribute may be overridden when a Policy is created by passing it as a keyword argument to the constructor. Policy objects are immutable, but a new Policy object can be created with only certain values changed by calling the Policy instance with keyword arguments. Policy objects can also be added, producing a new Policy object in which the non-default attributes set in the right hand operand overwrite those specified in the left operand. Settable attributes: raise_on_defect -- If true, then defects should be raised as errors. Default: False. linesep -- string containing the value to use as separation between output lines. Default '\n'. cte_type -- Type of allowed content transfer encodings 7bit -- ASCII only 8bit -- Content-Transfer-Encoding: 8bit is allowed Default: 8bit. Also controls the disposition of (RFC invalid) binary data in headers; see the documentation of the binary_fold method. max_line_length -- maximum length of lines, excluding 'linesep', during serialization. None or 0 means no line wrapping is done. Default is 78. mangle_from_ -- a flag that, when True escapes From_ lines in the body of the message by putting a `>' in front of them. This is used when the message is being serialized by a generator. Default: True. message_factory -- the class to use to create new message objects. If the value is None, the default is Message.
clone(self, **kw) Return a new instance with specified attributes changed. The new instance has the same attribute values as the current object, except for the changes passed in as keyword arguments.
fold(self, name, value) Given the header name and the value from the model, return a string containing linesep characters that implement the folding of the header according to the policy controls. The value passed in by the email package may contain surrogateescaped binary data if the lines were parsed by a BytesParser. The returned value should not contain any surrogateescaped data.
fold_binary(self, name, value) Given the header name and the value from the model, return binary data containing linesep characters that implement the folding of the header according to the policy controls. The value passed in by the email package may contain surrogateescaped binary data.
handle_defect(self, obj, defect) Based on policy, either raise defect or call register_defect. handle_defect(obj, defect) defect should be a Defect subclass, but in any case must be an Exception subclass. obj is the object on which the defect should be registered if it is not raised. If the raise_on_defect is True, the defect is raised as an error, otherwise the object and the defect are passed to register_defect. This method is intended to be called by parsers that discover defects. The email package parsers always call it with Defect instances.
header_fetch_parse(self, name, value) Given the header name and the value from the model, return the value to be returned to the application program that is requesting that header. The value passed in by the email package may contain surrogateescaped binary data if the lines were parsed by a BytesParser. The returned value should not contain any surrogateescaped data.
header_max_count(self, name) Return the maximum allowed number of headers named 'name'. Called when a header is added to a Message object. If the returned value is not 0 or None, and there are already a number of headers with the name 'name' equal to the value returned, a ValueError is raised. Because the default behavior of Message's __setitem__ is to append the value to the list of headers, it is easy to create duplicate headers without realizing it. This method allows certain headers to be limited in the number of instances of that header that may be added to a Message programmatically. (The limit is not observed by the parser, which will faithfully produce as many headers as exist in the message being parsed.) The default implementation returns None for all header names.
header_source_parse(self, sourcelines) Given a list of linesep terminated strings constituting the lines of a single header, return the (name, value) tuple that should be stored in the model. The input lines should retain their terminating linesep characters. The lines passed in by the email package may contain surrogateescaped binary data.
header_store_parse(self, name, value) Given the header name and the value provided by the application program, return the (name, value) that should be stored in the model.
register_defect(self, obj, defect) Record 'defect' on 'obj'. Called by handle_defect if raise_on_defect is False. This method is part of the Policy API so that Policy subclasses can implement custom defect handling. The default implementation calls the append method of the defects attribute of obj. The objects used by the email package by default that get passed to this method will always have a defects attribute with an append method.
cte_type = '8bit'
linesep = '\n'
mangle_from_ = False
max_line_length = 78
message_factory = None
raise_on_defect = False
Text I/O implementation using an in-memory buffer. The initial_value argument sets the value of object. The newline argument is like the one of TextIOWrapper's constructor.
close(self, /) Close the IO object. Attempting any further operation after the object is closed will raise a ValueError. This method has no effect if the file is already closed.
detach(...) Separate the underlying buffer from the TextIOBase and return it. After the underlying buffer has been detached, the TextIO is in an unusable state.
fileno(self, /) Returns underlying file descriptor if one exists. OSError is raised if the IO object does not use a file descriptor.
flush(self, /) Flush write buffers, if applicable. This is not implemented for read-only and non-blocking streams.
getvalue(self, /) Retrieve the entire contents of the object.
isatty(self, /) Return whether this is an 'interactive' stream. Return False if it can't be determined.
read(self, size=-1, /) Read at most size characters, returned as a string. If the argument is negative or omitted, read until EOF is reached. Return an empty string at EOF.
readable(self, /) Returns True if the IO object can be read.
readline(self, size=-1, /) Read until newline or EOF. Returns an empty string if EOF is hit immediately.
readlines(self, hint=-1, /) Return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.
seek(self, pos, whence=0, /) Change stream position. Seek to character offset pos relative to position indicated by whence: 0 Start of stream (the default). pos should be >= 0; 1 Current position - pos must be 0; 2 End of stream - pos must be 0. Returns the new absolute position.
seekable(self, /) Returns True if the IO object can be seeked.
tell(self, /) Tell the current file position.
truncate(self, pos=None, /) Truncate size to pos. The pos argument defaults to the current file position, as returned by tell(). The current file position is unchanged. Returns the new absolute position.
writable(self, /) Returns True if the IO object can be written.
write(self, s, /) Write string to file. Returns the number of characters written, which is always equal to the length of the string.
writelines(self, lines, /) Write a list of lines to stream. Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.
closed = <attribute 'closed' of '_io.StringIO' objects>
encoding = <attribute 'encoding' of '_io._TextIOBase' objects> Encoding of the text stream. Subclasses should override.
errors = <attribute 'errors' of '_io._TextIOBase' objects> The error setting of the decoder or encoder. Subclasses should override.
line_buffering = <attribute 'line_buffering' of '_io.StringIO' objects>
newlines = <attribute 'newlines' of '_io.StringIO' objects>
decode_b(encoded)
SEMISPACE = '; '
compat32 = Compat32()
tspecials = re.compile('[ \\(\\)<>@,;:\\\\"/\\[\\]\\?=]')