# Copyright (C) 2001-2004 Python Software Foundation
# Contact: email-sig@python.org
"""Basic message object for the email package object model."""
from cStringIO
import StringIO
from email
import Charset
# Regular expression used to split header parameters. BAW: this may be too
# simple. It isn't strictly RFC 2045 (section 5.1) compliant, but it catches
# most headers found in the wild. We may eventually need a full fledged
paramre
= re
.compile(r
'\s*;\s*')
# Regular expression that matches `special' characters in parameters, the
# existance of which force quoting of the parameter value.
tspecials
= re
.compile(r
'[ \(\)<>@,;:\\"/\[\]\?=]')
def _formatparam(param
, value
=None, quote
=True):
"""Convenience function to format and return a key=value pair.
This will quote the value if needed or if quote is true.
if value
is not None and len(value
) > 0:
# A tuple is used for RFC 2231 encoded parameter values where items
# are (charset, language, value). charset is a string, not a Charset
if isinstance(value
, tuple):
value
= Utils
.encode_rfc2231(value
[2], value
[0], value
[1])
# BAW: Please check this. I think that if quote is set it should
# force quoting even if not necessary.
if quote
or tspecials
.search(value
):
return '%s="%s"' % (param
, Utils
.quote(value
))
return '%s=%s' % (param
, value
)
while end
> 0 and s
.count('"', 0, end
) % 2:
end
= s
.find(';', end
+ 1)
f
= f
[:i
].strip().lower() + '=' + f
[i
+1:].strip()
def _unquotevalue(value
):
# This is different than Utils.collapse_rfc2231_value() because it doesn't
# try to convert the value to a unicode. Message.get_param() and
# Message.get_params() are both currently defined to return the tuple in
# the face of RFC 2231 parameters.
if isinstance(value
, tuple):
return value
[0], value
[1], Utils
.unquote(value
[2])
return Utils
.unquote(value
)
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 occurrance 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.
# Defaults for multipart messages
self
.preamble
= self
.epilogue
= None
self
._default
_type
= 'text/plain'
"""Return the entire formatted message as a string.
This includes the headers, body, and envelope header.
return self
.as_string(unixfrom
=True)
def as_string(self
, unixfrom
=False):
"""Return the entire formatted message as a string.
Optional `unixfrom' when True, means include the Unix From_ envelope
This is a convenience method and may not generate the message exactly
as you intend because by default it mangles lines that begin with
"From ". For more flexibility, use the flatten() method of a
from email
.Generator
import Generator
g
.flatten(self
, unixfrom
=unixfrom
)
"""Return True if the message consists of multiple parts."""
return isinstance(self
._payload
, list)
def set_unixfrom(self
, unixfrom
):
self
._unixfrom
= unixfrom
def 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
if self
._payload
is None:
self
._payload
= [payload
]
self
._payload
.append(payload
)
def 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
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
elif not isinstance(self
._payload
, list):
raise TypeError('Expected list, got %s' % type(self
._payload
))
payload
= self
._payload
[i
]
cte
= self
.get('content-transfer-encoding', '').lower()
if cte
== 'quoted-printable':
return Utils
._qdecode
(payload
)
return Utils
._bdecode
(payload
)
elif cte
in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
uu
.decode(StringIO(payload
+'\n'), sfp
)
# Everything else, including encodings with 8bit or 7bit are returned
def 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.
self
.set_charset(charset
)
def 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.
self
.del_param('charset')
if isinstance(charset
, str):
charset
= Charset
.Charset(charset
)
if not isinstance(charset
, Charset
.Charset
):
# BAW: should we accept strings that can serve as arguments to the
if not self
.has_key('MIME-Version'):
self
.add_header('MIME-Version', '1.0')
if not self
.has_key('Content-Type'):
self
.add_header('Content-Type', 'text/plain',
charset
=charset
.get_output_charset())
self
.set_param('charset', charset
.get_output_charset())
if not self
.has_key('Content-Transfer-Encoding'):
cte
= charset
.get_body_encoding()
self
.add_header('Content-Transfer-Encoding', cte
)
"""Return the Charset instance associated with the message's payload.
# MAPPING INTERFACE (partial)
"""Return the total number of headers, including duplicates."""
return len(self
._headers
)
def __getitem__(self
, name
):
Return None if the header is missing instead of raising an exception.
Note that if the header appeared multiple times, exactly which
occurrance gets returned is undefined. Use get_all() to get all
the values matching a header field name.
def __setitem__(self
, name
, val
):
"""Set the value of a header.
Note: this does not overwrite an existing header with the same field
name. Use __delitem__() first to delete any existing headers.
self
._headers
.append((name
, val
))
def __delitem__(self
, name
):
"""Delete all occurrences of a header, if present.
Does not raise an exception if the header is missing.
for k
, v
in self
._headers
:
newheaders
.append((k
, v
))
self
._headers
= newheaders
def __contains__(self
, name
):
return name
.lower() in [k
.lower() for k
, v
in self
._headers
]
"""Return true if the message contains the header."""
return self
.get(name
, missing
) is not missing
"""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
return [k
for k
, v
in self
._headers
]
"""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
return [v
for k
, v
in self
._headers
]
"""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
def get(self
, name
, failobj
=None):
Like __getitem__() but return failobj instead of None when the field
for k
, v
in self
._headers
:
# Additional useful stuff
def 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).
for k
, v
in self
._headers
:
def 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.
msg.add_header('content-disposition', 'attachment', filename='bud.gif')
for k
, v
in _params
.items():
parts
.append(k
.replace('_', '-'))
parts
.append(_formatparam(k
.replace('_', '-'), v
))
self
._headers
.append((_name
, SEMISPACE
.join(parts
)))
def replace_header(self
, _name
, _value
):
Replace the first matching header found in the message, retaining
header order and case. If no matching header was found, a KeyError is
for i
, (k
, v
) in zip(range(len(self
._headers
)), self
._headers
):
self
._headers
[i
] = (k
, _value
)
# Deprecated methods. These will be removed in email 3.1.
def get_type(self
, failobj
=None):
"""Returns the message's content type.
The returned string is coerced to lowercase and returned as a single
string of the form `maintype/subtype'. If there was no Content-Type
header in the message, failobj is returned (defaults to None).
warnings
.warn('get_type() deprecated; use get_content_type()',
value
= self
.get('content-type', missing
)
return paramre
.split(value
)[0].lower().strip()
def get_main_type(self
, failobj
=None):
"""Return the message's main content type if present."""
warnings
.warn('get_main_type() deprecated; use get_content_maintype()',
ctype
= self
.get_type(missing
)
if ctype
.count('/') <> 1:
return ctype
.split('/')[0]
def get_subtype(self
, failobj
=None):
"""Return the message's content subtype if present."""
warnings
.warn('get_subtype() deprecated; use get_content_subtype()',
ctype
= self
.get_type(missing
)
if ctype
.count('/') <> 1:
return ctype
.split('/')[1]
# Use these three methods instead of the three above.
def 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
value
= self
.get('content-type', missing
)
# This should have no parameters
return self
.get_default_type()
ctype
= paramre
.split(value
)[0].lower().strip()
# RFC 2045, section 5.2 says if its invalid, use text/plain
if ctype
.count('/') <> 1:
def get_content_maintype(self
):
"""Return the message's main content type.
This is the `maintype' part of the string returned by
ctype
= self
.get_content_type()
return ctype
.split('/')[0]
def get_content_subtype(self
):
"""Returns the message's sub-content type.
This is the `subtype' part of the string returned by
ctype
= self
.get_content_type()
return ctype
.split('/')[1]
def 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.
return self
._default
_type
def 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
self
._default
_type
= ctype
def _get_params_preserve(self
, failobj
, header
):
# Like get_params() but preserves the quoting of values. BAW:
# should this be part of the public interface?
value
= self
.get(header
, missing
)
for p
in _parseparam(';' + value
):
name
, val
= p
.split('=', 1)
# Must have been a bare attribute
params
.append((name
, val
))
params
= Utils
.decode_params(params
)
def 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.
params
= self
._get
_params
_preserve
(missing
, header
)
return [(k
, _unquotevalue(v
)) for k
, v
in params
]
def get_param(self
, param
, failobj
=None, header
='content-type',
"""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.
Your application should be prepared to deal with 3-tuple return
values, and can convert the parameter to a Unicode string like so:
param = msg.get_param('foo')
if isinstance(param, tuple):
param = unicode(param[2], param[0] or 'us-ascii')
In any case, the parameter value (either the returned string, or the
VALUE item in the 3-tuple) is always unquoted, unless unquote is set
if not self
.has_key(header
):
for k
, v
in self
._get
_params
_preserve
(failobj
, header
):
if k
.lower() == param
.lower():
def set_param(self
, param
, value
, header
='Content-Type', requote
=True,
charset
=None, language
=''):
"""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 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.
if not isinstance(value
, tuple) and charset
:
value
= (charset
, language
, value
)
if not self
.has_key(header
) and header
.lower() == 'content-type':
if not self
.get_param(param
, header
=header
):
ctype
= _formatparam(param
, value
, requote
)
[ctype
, _formatparam(param
, value
, requote
)])
for old_param
, old_value
in self
.get_params(header
=header
,
if old_param
.lower() == param
.lower():
append_param
= _formatparam(param
, value
, requote
)
append_param
= _formatparam(old_param
, old_value
, requote
)
ctype
= SEMISPACE
.join([ctype
, append_param
])
if ctype
<> self
.get(header
):
def 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
if not self
.has_key(header
):
for p
, v
in self
.get_params(header
=header
, unquote
=requote
):
if p
.lower() <> param
.lower():
new_ctype
= _formatparam(p
, v
, requote
)
new_ctype
= SEMISPACE
.join([new_ctype
,
_formatparam(p
, v
, requote
)])
if new_ctype
<> self
.get(header
):
def 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
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
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
# BAW: should we be strict?
if not type.count('/') == 1:
# Set the Content-Type, you get a MIME-Version
if header
.lower() == 'content-type':
self
['MIME-Version'] = '1.0'
if not self
.has_key(header
):
params
= self
.get_params(header
=header
, unquote
=requote
)
# Skip the first param; it's the old type.
self
.set_param(p
, v
, header
, requote
)
def 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.
filename
= self
.get_param('filename', missing
, 'content-disposition')
return Utils
.collapse_rfc2231_value(filename
).strip()
def 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.
boundary
= self
.get_param('boundary', missing
)
# RFC 2046 says that boundaries may begin but not end in w/s
return Utils
.collapse_rfc2231_value(boundary
).rstrip()
def 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.
params
= self
._get
_params
_preserve
(missing
, 'content-type')
# There was no Content-Type header, and we don't know what type
# to set it to, so raise an exception.
raise Errors
.HeaderParseError
, 'No Content-Type header found'
if pk
.lower() == 'boundary':
newparams
.append(('boundary', '"%s"' % boundary
))
newparams
.append((pk
, pv
))
# The original Content-Type header had no boundary attribute.
# Tack one on the end. BAW: should we raise an exception
newparams
.append(('boundary', '"%s"' % boundary
))
# Replace the existing Content-Type header with the new value
for h
, v
in self
._headers
:
if h
.lower() == 'content-type':
parts
.append('%s=%s' % (k
, v
))
newheaders
.append((h
, SEMISPACE
.join(parts
)))
newheaders
.append((h
, v
))
self
._headers
= newheaders
def 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,
charset
= self
.get_param('charset', missing
)
if isinstance(charset
, tuple):
# RFC 2231 encoded, so decode it, and it better end up as ascii.
pcharset
= charset
[0] or 'us-ascii'
charset
= unicode(charset
[2], pcharset
).encode('us-ascii')
# RFC 2046, $4.1.2 says charsets are not case sensitive
def 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
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.
return [part
.get_content_charset(failobj
) for part
in self
.walk()]
# I.e. def walk(self): ...
from email
.Iterators
import walk