Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2007 Python Software Foundation |
| 2 | # Author: Barry Warsaw |
| 3 | # Contact: email-sig@python.org |
| 4 | |
| 5 | """Basic message object for the email package object model.""" |
| 6 | |
| 7 | __all__ = ['Message'] |
| 8 | |
| 9 | import re |
| 10 | import uu |
R David Murray | 95a8dfb | 2014-03-23 14:18:44 -0400 | [diff] [blame] | 11 | import quopri |
R David Murray | 8a97896 | 2014-09-20 18:05:28 -0400 | [diff] [blame] | 12 | import warnings |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 13 | from io import BytesIO, StringIO |
| 14 | |
| 15 | # Intrapackage imports |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 16 | from email import utils |
| 17 | from email import errors |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 18 | from email._policybase import compat32 |
R. David Murray | 9253214 | 2011-01-07 23:25:30 +0000 | [diff] [blame] | 19 | from email import charset as _charset |
R David Murray | 80e0aee | 2012-05-27 21:23:34 -0400 | [diff] [blame] | 20 | from email._encoded_words import decode_b |
R. David Murray | 9253214 | 2011-01-07 23:25:30 +0000 | [diff] [blame] | 21 | Charset = _charset.Charset |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 22 | |
| 23 | SEMISPACE = '; ' |
| 24 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 25 | # Regular expression that matches `special' characters in parameters, the |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 26 | # existence of which force quoting of the parameter value. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 27 | tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]') |
| 28 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 29 | |
Benjamin Peterson | 4cd6a95 | 2008-08-17 20:23:46 +0000 | [diff] [blame] | 30 | def _splitparam(param): |
| 31 | # Split header parameters. BAW: this may be too simple. It isn't |
| 32 | # strictly RFC 2045 (section 5.1) compliant, but it catches most headers |
R David Murray | a215023 | 2011-03-16 21:11:23 -0400 | [diff] [blame] | 33 | # found in the wild. We may eventually need a full fledged parser. |
| 34 | # RDM: we might have a Header here; for now just stringify it. |
| 35 | a, sep, b = str(param).partition(';') |
Benjamin Peterson | 4cd6a95 | 2008-08-17 20:23:46 +0000 | [diff] [blame] | 36 | if not sep: |
| 37 | return a.strip(), None |
| 38 | return a.strip(), b.strip() |
| 39 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 40 | def _formatparam(param, value=None, quote=True): |
| 41 | """Convenience function to format and return a key=value pair. |
| 42 | |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 43 | This will quote the value if needed or if quote is true. If value is a |
| 44 | three tuple (charset, language, value), it will be encoded according |
| 45 | to RFC2231 rules. If it contains non-ascii characters it will likewise |
| 46 | be encoded according to RFC2231 rules, using the utf-8 charset and |
| 47 | a null language. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 48 | """ |
| 49 | if value is not None and len(value) > 0: |
| 50 | # A tuple is used for RFC 2231 encoded parameter values where items |
| 51 | # are (charset, language, value). charset is a string, not a Charset |
R. David Murray | dfd7eb0 | 2010-12-24 22:36:49 +0000 | [diff] [blame] | 52 | # instance. RFC 2231 encoded values are never quoted, per RFC. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 53 | if isinstance(value, tuple): |
| 54 | # Encode as per RFC 2231 |
| 55 | param += '*' |
| 56 | value = utils.encode_rfc2231(value[2], value[0], value[1]) |
R. David Murray | dfd7eb0 | 2010-12-24 22:36:49 +0000 | [diff] [blame] | 57 | return '%s=%s' % (param, value) |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 58 | else: |
| 59 | try: |
| 60 | value.encode('ascii') |
| 61 | except UnicodeEncodeError: |
| 62 | param += '*' |
| 63 | value = utils.encode_rfc2231(value, 'utf-8', '') |
R. David Murray | dfd7eb0 | 2010-12-24 22:36:49 +0000 | [diff] [blame] | 64 | return '%s=%s' % (param, value) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 65 | # BAW: Please check this. I think that if quote is set it should |
| 66 | # force quoting even if not necessary. |
| 67 | if quote or tspecials.search(value): |
| 68 | return '%s="%s"' % (param, utils.quote(value)) |
| 69 | else: |
| 70 | return '%s=%s' % (param, value) |
| 71 | else: |
| 72 | return param |
| 73 | |
| 74 | def _parseparam(s): |
R David Murray | a215023 | 2011-03-16 21:11:23 -0400 | [diff] [blame] | 75 | # RDM This might be a Header, so for now stringify it. |
| 76 | s = ';' + str(s) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 77 | plist = [] |
| 78 | while s[:1] == ';': |
| 79 | s = s[1:] |
| 80 | end = s.find(';') |
R. David Murray | d48739f | 2010-04-14 18:59:18 +0000 | [diff] [blame] | 81 | while end > 0 and (s.count('"', 0, end) - s.count('\\"', 0, end)) % 2: |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 82 | end = s.find(';', end + 1) |
| 83 | if end < 0: |
| 84 | end = len(s) |
| 85 | f = s[:end] |
| 86 | if '=' in f: |
| 87 | i = f.index('=') |
| 88 | f = f[:i].strip().lower() + '=' + f[i+1:].strip() |
| 89 | plist.append(f.strip()) |
| 90 | s = s[end:] |
| 91 | return plist |
| 92 | |
| 93 | |
| 94 | def _unquotevalue(value): |
| 95 | # This is different than utils.collapse_rfc2231_value() because it doesn't |
| 96 | # try to convert the value to a unicode. Message.get_param() and |
| 97 | # Message.get_params() are both currently defined to return the tuple in |
| 98 | # the face of RFC 2231 parameters. |
| 99 | if isinstance(value, tuple): |
| 100 | return value[0], value[1], utils.unquote(value[2]) |
| 101 | else: |
| 102 | return utils.unquote(value) |
| 103 | |
| 104 | |
| 105 | |
| 106 | class Message: |
| 107 | """Basic message object. |
| 108 | |
| 109 | A message object is defined as something that has a bunch of RFC 2822 |
| 110 | headers and a payload. It may optionally have an envelope header |
| 111 | (a.k.a. Unix-From or From_ header). If the message is a container (i.e. a |
| 112 | multipart or a message/rfc822), then the payload is a list of Message |
| 113 | objects, otherwise it is a string. |
| 114 | |
| 115 | Message objects implement part of the `mapping' interface, which assumes |
R. David Murray | d2c310f | 2010-10-01 02:08:02 +0000 | [diff] [blame] | 116 | there is exactly one occurrence of the header per message. Some headers |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 117 | do in fact appear multiple times (e.g. Received) and for those headers, |
| 118 | you must use the explicit API to set or get all the headers. Not all of |
| 119 | the mapping methods are implemented. |
| 120 | """ |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 121 | def __init__(self, policy=compat32): |
| 122 | self.policy = policy |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 123 | self._headers = [] |
| 124 | self._unixfrom = None |
| 125 | self._payload = None |
| 126 | self._charset = None |
| 127 | # Defaults for multipart messages |
| 128 | self.preamble = self.epilogue = None |
| 129 | self.defects = [] |
| 130 | # Default content type |
| 131 | self._default_type = 'text/plain' |
| 132 | |
| 133 | def __str__(self): |
| 134 | """Return the entire formatted message as a string. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 135 | """ |
| 136 | return self.as_string() |
| 137 | |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 138 | def as_string(self, unixfrom=False, maxheaderlen=0, policy=None): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 139 | """Return the entire formatted message as a string. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 140 | |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 141 | Optional 'unixfrom', when true, means include the Unix From_ envelope |
| 142 | header. For backward compatibility reasons, if maxheaderlen is |
| 143 | not specified it defaults to 0, so you must override it explicitly |
| 144 | if you want a different maxheaderlen. 'policy' is passed to the |
| 145 | Generator instance used to serialize the mesasge; if it is not |
| 146 | specified the policy associated with the message instance is used. |
| 147 | |
| 148 | If the message object contains binary data that is not encoded |
| 149 | according to RFC standards, the non-compliant data will be replaced by |
| 150 | unicode "unknown character" code points. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 151 | """ |
| 152 | from email.generator import Generator |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 153 | policy = self.policy if policy is None else policy |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 154 | fp = StringIO() |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 155 | g = Generator(fp, |
| 156 | mangle_from_=False, |
| 157 | maxheaderlen=maxheaderlen, |
| 158 | policy=policy) |
| 159 | g.flatten(self, unixfrom=unixfrom) |
| 160 | return fp.getvalue() |
| 161 | |
| 162 | def __bytes__(self): |
| 163 | """Return the entire formatted message as a bytes object. |
| 164 | """ |
| 165 | return self.as_bytes() |
| 166 | |
| 167 | def as_bytes(self, unixfrom=False, policy=None): |
| 168 | """Return the entire formatted message as a bytes object. |
| 169 | |
| 170 | Optional 'unixfrom', when true, means include the Unix From_ envelope |
| 171 | header. 'policy' is passed to the BytesGenerator instance used to |
| 172 | serialize the message; if not specified the policy associated with |
| 173 | the message instance is used. |
| 174 | """ |
| 175 | from email.generator import BytesGenerator |
| 176 | policy = self.policy if policy is None else policy |
| 177 | fp = BytesIO() |
| 178 | g = BytesGenerator(fp, mangle_from_=False, policy=policy) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 179 | g.flatten(self, unixfrom=unixfrom) |
| 180 | return fp.getvalue() |
| 181 | |
| 182 | def is_multipart(self): |
| 183 | """Return True if the message consists of multiple parts.""" |
| 184 | return isinstance(self._payload, list) |
| 185 | |
| 186 | # |
| 187 | # Unix From_ line |
| 188 | # |
| 189 | def set_unixfrom(self, unixfrom): |
| 190 | self._unixfrom = unixfrom |
| 191 | |
| 192 | def get_unixfrom(self): |
| 193 | return self._unixfrom |
| 194 | |
| 195 | # |
| 196 | # Payload manipulation. |
| 197 | # |
| 198 | def attach(self, payload): |
| 199 | """Add the given payload to the current payload. |
| 200 | |
| 201 | The current payload will always be a list of objects after this method |
| 202 | is called. If you want to set the payload to a scalar object, use |
| 203 | set_payload() instead. |
| 204 | """ |
| 205 | if self._payload is None: |
| 206 | self._payload = [payload] |
| 207 | else: |
R David Murray | 5dda124 | 2014-03-06 11:44:17 -0500 | [diff] [blame] | 208 | try: |
| 209 | self._payload.append(payload) |
| 210 | except AttributeError: |
| 211 | raise TypeError("Attach is not valid on a message with a" |
| 212 | " non-multipart payload") |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 213 | |
| 214 | def get_payload(self, i=None, decode=False): |
| 215 | """Return a reference to the payload. |
| 216 | |
| 217 | The payload will either be a list object or a string. If you mutate |
| 218 | the list object, you modify the message's payload in place. Optional |
| 219 | i returns that index into the payload. |
| 220 | |
| 221 | Optional decode is a flag indicating whether the payload should be |
| 222 | decoded or not, according to the Content-Transfer-Encoding header |
| 223 | (default is False). |
| 224 | |
| 225 | When True and the message is not a multipart, the payload will be |
| 226 | decoded if this header's value is `quoted-printable' or `base64'. If |
| 227 | some other encoding is used, or the header is missing, or if the |
| 228 | payload has bogus data (i.e. bogus base64 or uuencoded data), the |
| 229 | payload is returned as-is. |
| 230 | |
| 231 | If the message is a multipart and the decode flag is True, then None |
| 232 | is returned. |
| 233 | """ |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 234 | # Here is the logic table for this code, based on the email5.0.0 code: |
| 235 | # i decode is_multipart result |
| 236 | # ------ ------ ------------ ------------------------------ |
| 237 | # None True True None |
| 238 | # i True True None |
| 239 | # None False True _payload (a list) |
| 240 | # i False True _payload element i (a Message) |
| 241 | # i False False error (not a list) |
| 242 | # i True False error (not a list) |
| 243 | # None False False _payload |
| 244 | # None True False _payload decoded (bytes) |
| 245 | # Note that Barry planned to factor out the 'decode' case, but that |
| 246 | # isn't so easy now that we handle the 8 bit data, which needs to be |
| 247 | # converted in both the decode and non-decode path. |
| 248 | if self.is_multipart(): |
| 249 | if decode: |
| 250 | return None |
| 251 | if i is None: |
| 252 | return self._payload |
| 253 | else: |
| 254 | return self._payload[i] |
| 255 | # For backward compatibility, Use isinstance and this error message |
| 256 | # instead of the more logical is_multipart test. |
| 257 | if i is not None and not isinstance(self._payload, list): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 258 | raise TypeError('Expected list, got %s' % type(self._payload)) |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 259 | payload = self._payload |
R David Murray | a215023 | 2011-03-16 21:11:23 -0400 | [diff] [blame] | 260 | # cte might be a Header, so for now stringify it. |
| 261 | cte = str(self.get('content-transfer-encoding', '')).lower() |
R David Murray | 106f8e3 | 2011-03-15 12:48:41 -0400 | [diff] [blame] | 262 | # payload may be bytes here. |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 263 | if isinstance(payload, str): |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 264 | if utils._has_surrogates(payload): |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 265 | bpayload = payload.encode('ascii', 'surrogateescape') |
| 266 | if not decode: |
| 267 | try: |
| 268 | payload = bpayload.decode(self.get_param('charset', 'ascii'), 'replace') |
| 269 | except LookupError: |
| 270 | payload = bpayload.decode('ascii', 'replace') |
| 271 | elif decode: |
| 272 | try: |
| 273 | bpayload = payload.encode('ascii') |
| 274 | except UnicodeError: |
| 275 | # This won't happen for RFC compliant messages (messages |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 276 | # containing only ASCII code points in the unicode input). |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 277 | # If it does happen, turn the string into bytes in a way |
| 278 | # guaranteed not to fail. |
| 279 | bpayload = payload.encode('raw-unicode-escape') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 280 | if not decode: |
| 281 | return payload |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 282 | if cte == 'quoted-printable': |
R David Murray | 95a8dfb | 2014-03-23 14:18:44 -0400 | [diff] [blame] | 283 | return quopri.decodestring(bpayload) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 284 | elif cte == 'base64': |
R David Murray | 80e0aee | 2012-05-27 21:23:34 -0400 | [diff] [blame] | 285 | # XXX: this is a bit of a hack; decode_b should probably be factored |
| 286 | # out somewhere, but I haven't figured out where yet. |
| 287 | value, defects = decode_b(b''.join(bpayload.splitlines())) |
| 288 | for defect in defects: |
| 289 | self.policy.handle_defect(self, defect) |
| 290 | return value |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 291 | elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'): |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 292 | in_file = BytesIO(bpayload) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 293 | out_file = BytesIO() |
| 294 | try: |
| 295 | uu.decode(in_file, out_file, quiet=True) |
| 296 | return out_file.getvalue() |
| 297 | except uu.Error: |
| 298 | # Some decoding problem |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 299 | return bpayload |
Barry Warsaw | 8b2af27 | 2007-08-31 03:04:26 +0000 | [diff] [blame] | 300 | if isinstance(payload, str): |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 301 | return bpayload |
Barry Warsaw | 8b2af27 | 2007-08-31 03:04:26 +0000 | [diff] [blame] | 302 | return payload |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 303 | |
| 304 | def set_payload(self, payload, charset=None): |
| 305 | """Set the payload to the given value. |
| 306 | |
| 307 | Optional charset sets the message's default character set. See |
| 308 | set_charset() for details. |
| 309 | """ |
R David Murray | 15a693a | 2014-02-07 12:46:17 -0500 | [diff] [blame] | 310 | if hasattr(payload, 'encode'): |
| 311 | if charset is None: |
R David Murray | 15a693a | 2014-02-07 12:46:17 -0500 | [diff] [blame] | 312 | self._payload = payload |
| 313 | return |
| 314 | if not isinstance(charset, Charset): |
| 315 | charset = Charset(charset) |
| 316 | payload = payload.encode(charset.output_charset) |
| 317 | if hasattr(payload, 'decode'): |
| 318 | self._payload = payload.decode('ascii', 'surrogateescape') |
| 319 | else: |
| 320 | self._payload = payload |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 321 | if charset is not None: |
| 322 | self.set_charset(charset) |
| 323 | |
| 324 | def set_charset(self, charset): |
| 325 | """Set the charset of the payload to a given character set. |
| 326 | |
| 327 | charset can be a Charset instance, a string naming a character set, or |
| 328 | None. If it is a string it will be converted to a Charset instance. |
| 329 | If charset is None, the charset parameter will be removed from the |
| 330 | Content-Type field. Anything else will generate a TypeError. |
| 331 | |
| 332 | The message will be assumed to be of type text/* encoded with |
| 333 | charset.input_charset. It will be converted to charset.output_charset |
| 334 | and encoded properly, if needed, when generating the plain text |
| 335 | representation of the message. MIME headers (MIME-Version, |
| 336 | Content-Type, Content-Transfer-Encoding) will be added as needed. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 337 | """ |
| 338 | if charset is None: |
| 339 | self.del_param('charset') |
| 340 | self._charset = None |
| 341 | return |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 342 | if not isinstance(charset, Charset): |
| 343 | charset = Charset(charset) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 344 | self._charset = charset |
| 345 | if 'MIME-Version' not in self: |
| 346 | self.add_header('MIME-Version', '1.0') |
| 347 | if 'Content-Type' not in self: |
| 348 | self.add_header('Content-Type', 'text/plain', |
| 349 | charset=charset.get_output_charset()) |
| 350 | else: |
| 351 | self.set_param('charset', charset.get_output_charset()) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 352 | if charset != charset.get_output_charset(): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 353 | self._payload = charset.body_encode(self._payload) |
| 354 | if 'Content-Transfer-Encoding' not in self: |
| 355 | cte = charset.get_body_encoding() |
| 356 | try: |
| 357 | cte(self) |
| 358 | except TypeError: |
R David Murray | fcc0072 | 2014-02-07 13:03:08 -0500 | [diff] [blame] | 359 | # This 'if' is for backward compatibility, it allows unicode |
| 360 | # through even though that won't work correctly if the |
| 361 | # message is serialized. |
R David Murray | 15a693a | 2014-02-07 12:46:17 -0500 | [diff] [blame] | 362 | payload = self._payload |
| 363 | if payload: |
| 364 | try: |
| 365 | payload = payload.encode('ascii', 'surrogateescape') |
| 366 | except UnicodeError: |
| 367 | payload = payload.encode(charset.output_charset) |
| 368 | self._payload = charset.body_encode(payload) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 369 | self.add_header('Content-Transfer-Encoding', cte) |
| 370 | |
| 371 | def get_charset(self): |
| 372 | """Return the Charset instance associated with the message's payload. |
| 373 | """ |
| 374 | return self._charset |
| 375 | |
| 376 | # |
| 377 | # MAPPING INTERFACE (partial) |
| 378 | # |
| 379 | def __len__(self): |
| 380 | """Return the total number of headers, including duplicates.""" |
| 381 | return len(self._headers) |
| 382 | |
| 383 | def __getitem__(self, name): |
| 384 | """Get a header value. |
| 385 | |
| 386 | Return None if the header is missing instead of raising an exception. |
| 387 | |
| 388 | Note that if the header appeared multiple times, exactly which |
R. David Murray | d2c310f | 2010-10-01 02:08:02 +0000 | [diff] [blame] | 389 | occurrence gets returned is undefined. Use get_all() to get all |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 390 | the values matching a header field name. |
| 391 | """ |
| 392 | return self.get(name) |
| 393 | |
| 394 | def __setitem__(self, name, val): |
| 395 | """Set the value of a header. |
| 396 | |
| 397 | Note: this does not overwrite an existing header with the same field |
| 398 | name. Use __delitem__() first to delete any existing headers. |
| 399 | """ |
R David Murray | abfc374 | 2012-05-29 09:14:44 -0400 | [diff] [blame] | 400 | max_count = self.policy.header_max_count(name) |
| 401 | if max_count: |
| 402 | lname = name.lower() |
| 403 | found = 0 |
| 404 | for k, v in self._headers: |
| 405 | if k.lower() == lname: |
| 406 | found += 1 |
| 407 | if found >= max_count: |
| 408 | raise ValueError("There may be at most {} {} headers " |
| 409 | "in a message".format(max_count, name)) |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 410 | self._headers.append(self.policy.header_store_parse(name, val)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 411 | |
| 412 | def __delitem__(self, name): |
| 413 | """Delete all occurrences of a header, if present. |
| 414 | |
| 415 | Does not raise an exception if the header is missing. |
| 416 | """ |
| 417 | name = name.lower() |
| 418 | newheaders = [] |
| 419 | for k, v in self._headers: |
| 420 | if k.lower() != name: |
| 421 | newheaders.append((k, v)) |
| 422 | self._headers = newheaders |
| 423 | |
| 424 | def __contains__(self, name): |
| 425 | return name.lower() in [k.lower() for k, v in self._headers] |
| 426 | |
| 427 | def __iter__(self): |
| 428 | for field, value in self._headers: |
| 429 | yield field |
| 430 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 431 | def keys(self): |
| 432 | """Return a list of all the message's header field names. |
| 433 | |
| 434 | These will be sorted in the order they appeared in the original |
| 435 | message, or were added to the message, and may contain duplicates. |
| 436 | Any fields deleted and re-inserted are always appended to the header |
| 437 | list. |
| 438 | """ |
| 439 | return [k for k, v in self._headers] |
| 440 | |
| 441 | def values(self): |
| 442 | """Return a list of all the message's header values. |
| 443 | |
| 444 | These will be sorted in the order they appeared in the original |
| 445 | message, or were added to the message, and may contain duplicates. |
| 446 | Any fields deleted and re-inserted are always appended to the header |
| 447 | list. |
| 448 | """ |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 449 | return [self.policy.header_fetch_parse(k, v) |
| 450 | for k, v in self._headers] |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 451 | |
| 452 | def items(self): |
| 453 | """Get all the message's header fields and values. |
| 454 | |
| 455 | These will be sorted in the order they appeared in the original |
| 456 | message, or were added to the message, and may contain duplicates. |
| 457 | Any fields deleted and re-inserted are always appended to the header |
| 458 | list. |
| 459 | """ |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 460 | return [(k, self.policy.header_fetch_parse(k, v)) |
| 461 | for k, v in self._headers] |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 462 | |
| 463 | def get(self, name, failobj=None): |
| 464 | """Get a header value. |
| 465 | |
| 466 | Like __getitem__() but return failobj instead of None when the field |
| 467 | is missing. |
| 468 | """ |
| 469 | name = name.lower() |
| 470 | for k, v in self._headers: |
| 471 | if k.lower() == name: |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 472 | return self.policy.header_fetch_parse(k, v) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 473 | return failobj |
| 474 | |
| 475 | # |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 476 | # "Internal" methods (public API, but only intended for use by a parser |
| 477 | # or generator, not normal application code. |
| 478 | # |
| 479 | |
| 480 | def set_raw(self, name, value): |
| 481 | """Store name and value in the model without modification. |
| 482 | |
| 483 | This is an "internal" API, intended only for use by a parser. |
| 484 | """ |
| 485 | self._headers.append((name, value)) |
| 486 | |
| 487 | def raw_items(self): |
| 488 | """Return the (name, value) header pairs without modification. |
| 489 | |
| 490 | This is an "internal" API, intended only for use by a generator. |
| 491 | """ |
| 492 | return iter(self._headers.copy()) |
| 493 | |
| 494 | # |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 495 | # Additional useful stuff |
| 496 | # |
| 497 | |
| 498 | def get_all(self, name, failobj=None): |
| 499 | """Return a list of all the values for the named field. |
| 500 | |
| 501 | These will be sorted in the order they appeared in the original |
| 502 | message, and may contain duplicates. Any fields deleted and |
| 503 | re-inserted are always appended to the header list. |
| 504 | |
| 505 | If no such fields exist, failobj is returned (defaults to None). |
| 506 | """ |
| 507 | values = [] |
| 508 | name = name.lower() |
| 509 | for k, v in self._headers: |
| 510 | if k.lower() == name: |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 511 | values.append(self.policy.header_fetch_parse(k, v)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 512 | if not values: |
| 513 | return failobj |
| 514 | return values |
| 515 | |
| 516 | def add_header(self, _name, _value, **_params): |
| 517 | """Extended header setting. |
| 518 | |
| 519 | name is the header field to add. keyword arguments can be used to set |
| 520 | additional parameters for the header field, with underscores converted |
| 521 | to dashes. Normally the parameter will be added as key="value" unless |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 522 | value is None, in which case only the key will be added. If a |
| 523 | parameter value contains non-ASCII characters it can be specified as a |
| 524 | three-tuple of (charset, language, value), in which case it will be |
| 525 | encoded according to RFC2231 rules. Otherwise it will be encoded using |
| 526 | the utf-8 charset and a language of ''. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 527 | |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 528 | Examples: |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 529 | |
| 530 | msg.add_header('content-disposition', 'attachment', filename='bud.gif') |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 531 | msg.add_header('content-disposition', 'attachment', |
| 532 | filename=('utf-8', '', Fußballer.ppt')) |
| 533 | msg.add_header('content-disposition', 'attachment', |
| 534 | filename='Fußballer.ppt')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 535 | """ |
| 536 | parts = [] |
| 537 | for k, v in _params.items(): |
| 538 | if v is None: |
| 539 | parts.append(k.replace('_', '-')) |
| 540 | else: |
| 541 | parts.append(_formatparam(k.replace('_', '-'), v)) |
| 542 | if _value is not None: |
| 543 | parts.insert(0, _value) |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 544 | self[_name] = SEMISPACE.join(parts) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 545 | |
| 546 | def replace_header(self, _name, _value): |
| 547 | """Replace a header. |
| 548 | |
| 549 | Replace the first matching header found in the message, retaining |
| 550 | header order and case. If no matching header was found, a KeyError is |
| 551 | raised. |
| 552 | """ |
| 553 | _name = _name.lower() |
| 554 | for i, (k, v) in zip(range(len(self._headers)), self._headers): |
| 555 | if k.lower() == _name: |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 556 | self._headers[i] = self.policy.header_store_parse(k, _value) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 557 | break |
| 558 | else: |
| 559 | raise KeyError(_name) |
| 560 | |
| 561 | # |
| 562 | # Use these three methods instead of the three above. |
| 563 | # |
| 564 | |
| 565 | def get_content_type(self): |
| 566 | """Return the message's content type. |
| 567 | |
| 568 | The returned string is coerced to lower case of the form |
| 569 | `maintype/subtype'. If there was no Content-Type header in the |
| 570 | message, the default type as given by get_default_type() will be |
| 571 | returned. Since according to RFC 2045, messages always have a default |
| 572 | type this will always return a value. |
| 573 | |
| 574 | RFC 2045 defines a message's default type to be text/plain unless it |
| 575 | appears inside a multipart/digest container, in which case it would be |
| 576 | message/rfc822. |
| 577 | """ |
| 578 | missing = object() |
| 579 | value = self.get('content-type', missing) |
| 580 | if value is missing: |
| 581 | # This should have no parameters |
| 582 | return self.get_default_type() |
Benjamin Peterson | 4cd6a95 | 2008-08-17 20:23:46 +0000 | [diff] [blame] | 583 | ctype = _splitparam(value)[0].lower() |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 584 | # RFC 2045, section 5.2 says if its invalid, use text/plain |
| 585 | if ctype.count('/') != 1: |
| 586 | return 'text/plain' |
| 587 | return ctype |
| 588 | |
| 589 | def get_content_maintype(self): |
| 590 | """Return the message's main content type. |
| 591 | |
| 592 | This is the `maintype' part of the string returned by |
| 593 | get_content_type(). |
| 594 | """ |
| 595 | ctype = self.get_content_type() |
| 596 | return ctype.split('/')[0] |
| 597 | |
| 598 | def get_content_subtype(self): |
| 599 | """Returns the message's sub-content type. |
| 600 | |
| 601 | This is the `subtype' part of the string returned by |
| 602 | get_content_type(). |
| 603 | """ |
| 604 | ctype = self.get_content_type() |
| 605 | return ctype.split('/')[1] |
| 606 | |
| 607 | def get_default_type(self): |
| 608 | """Return the `default' content type. |
| 609 | |
| 610 | Most messages have a default content type of text/plain, except for |
| 611 | messages that are subparts of multipart/digest containers. Such |
| 612 | subparts have a default content type of message/rfc822. |
| 613 | """ |
| 614 | return self._default_type |
| 615 | |
| 616 | def set_default_type(self, ctype): |
| 617 | """Set the `default' content type. |
| 618 | |
| 619 | ctype should be either "text/plain" or "message/rfc822", although this |
| 620 | is not enforced. The default content type is not stored in the |
| 621 | Content-Type header. |
| 622 | """ |
| 623 | self._default_type = ctype |
| 624 | |
| 625 | def _get_params_preserve(self, failobj, header): |
| 626 | # Like get_params() but preserves the quoting of values. BAW: |
| 627 | # should this be part of the public interface? |
| 628 | missing = object() |
| 629 | value = self.get(header, missing) |
| 630 | if value is missing: |
| 631 | return failobj |
| 632 | params = [] |
R David Murray | a215023 | 2011-03-16 21:11:23 -0400 | [diff] [blame] | 633 | for p in _parseparam(value): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 634 | try: |
| 635 | name, val = p.split('=', 1) |
| 636 | name = name.strip() |
| 637 | val = val.strip() |
| 638 | except ValueError: |
| 639 | # Must have been a bare attribute |
| 640 | name = p.strip() |
| 641 | val = '' |
| 642 | params.append((name, val)) |
| 643 | params = utils.decode_params(params) |
| 644 | return params |
| 645 | |
| 646 | def get_params(self, failobj=None, header='content-type', unquote=True): |
| 647 | """Return the message's Content-Type parameters, as a list. |
| 648 | |
| 649 | The elements of the returned list are 2-tuples of key/value pairs, as |
| 650 | split on the `=' sign. The left hand side of the `=' is the key, |
| 651 | while the right hand side is the value. If there is no `=' sign in |
| 652 | the parameter the value is the empty string. The value is as |
| 653 | described in the get_param() method. |
| 654 | |
| 655 | Optional failobj is the object to return if there is no Content-Type |
| 656 | header. Optional header is the header to search instead of |
| 657 | Content-Type. If unquote is True, the value is unquoted. |
| 658 | """ |
| 659 | missing = object() |
| 660 | params = self._get_params_preserve(missing, header) |
| 661 | if params is missing: |
| 662 | return failobj |
| 663 | if unquote: |
| 664 | return [(k, _unquotevalue(v)) for k, v in params] |
| 665 | else: |
| 666 | return params |
| 667 | |
| 668 | def get_param(self, param, failobj=None, header='content-type', |
| 669 | unquote=True): |
| 670 | """Return the parameter value if found in the Content-Type header. |
| 671 | |
| 672 | Optional failobj is the object to return if there is no Content-Type |
| 673 | header, or the Content-Type header has no such parameter. Optional |
| 674 | header is the header to search instead of Content-Type. |
| 675 | |
| 676 | Parameter keys are always compared case insensitively. The return |
| 677 | value can either be a string, or a 3-tuple if the parameter was RFC |
| 678 | 2231 encoded. When it's a 3-tuple, the elements of the value are of |
| 679 | the form (CHARSET, LANGUAGE, VALUE). Note that both CHARSET and |
| 680 | LANGUAGE can be None, in which case you should consider VALUE to be |
| 681 | encoded in the us-ascii charset. You can usually ignore LANGUAGE. |
R David Murray | 3ac8c78 | 2012-06-17 15:26:35 -0400 | [diff] [blame] | 682 | The parameter value (either the returned string, or the VALUE item in |
| 683 | the 3-tuple) is always unquoted, unless unquote is set to False. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 684 | |
R David Murray | 3ac8c78 | 2012-06-17 15:26:35 -0400 | [diff] [blame] | 685 | If your application doesn't care whether the parameter was RFC 2231 |
| 686 | encoded, it can turn the return value into a string as follows: |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 687 | |
R David Murray | 0de4d3e | 2013-11-03 12:23:23 -0500 | [diff] [blame] | 688 | rawparam = msg.get_param('foo') |
R David Murray | 3ac8c78 | 2012-06-17 15:26:35 -0400 | [diff] [blame] | 689 | param = email.utils.collapse_rfc2231_value(rawparam) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 690 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 691 | """ |
| 692 | if header not in self: |
| 693 | return failobj |
| 694 | for k, v in self._get_params_preserve(failobj, header): |
| 695 | if k.lower() == param.lower(): |
| 696 | if unquote: |
| 697 | return _unquotevalue(v) |
| 698 | else: |
| 699 | return v |
| 700 | return failobj |
| 701 | |
| 702 | def set_param(self, param, value, header='Content-Type', requote=True, |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 703 | charset=None, language='', replace=False): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 704 | """Set a parameter in the Content-Type header. |
| 705 | |
| 706 | If the parameter already exists in the header, its value will be |
| 707 | replaced with the new value. |
| 708 | |
| 709 | If header is Content-Type and has not yet been defined for this |
| 710 | message, it will be set to "text/plain" and the new parameter and |
| 711 | value will be appended as per RFC 2045. |
| 712 | |
| 713 | An alternate header can specified in the header argument, and all |
| 714 | parameters will be quoted as necessary unless requote is False. |
| 715 | |
| 716 | If charset is specified, the parameter will be encoded according to RFC |
| 717 | 2231. Optional language specifies the RFC 2231 language, defaulting |
| 718 | to the empty string. Both charset and language should be strings. |
| 719 | """ |
| 720 | if not isinstance(value, tuple) and charset: |
| 721 | value = (charset, language, value) |
| 722 | |
| 723 | if header not in self and header.lower() == 'content-type': |
| 724 | ctype = 'text/plain' |
| 725 | else: |
| 726 | ctype = self.get(header) |
| 727 | if not self.get_param(param, header=header): |
| 728 | if not ctype: |
| 729 | ctype = _formatparam(param, value, requote) |
| 730 | else: |
| 731 | ctype = SEMISPACE.join( |
| 732 | [ctype, _formatparam(param, value, requote)]) |
| 733 | else: |
| 734 | ctype = '' |
| 735 | for old_param, old_value in self.get_params(header=header, |
| 736 | unquote=requote): |
| 737 | append_param = '' |
| 738 | if old_param.lower() == param.lower(): |
| 739 | append_param = _formatparam(param, value, requote) |
| 740 | else: |
| 741 | append_param = _formatparam(old_param, old_value, requote) |
| 742 | if not ctype: |
| 743 | ctype = append_param |
| 744 | else: |
| 745 | ctype = SEMISPACE.join([ctype, append_param]) |
| 746 | if ctype != self.get(header): |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 747 | if replace: |
| 748 | self.replace_header(header, ctype) |
| 749 | else: |
| 750 | del self[header] |
| 751 | self[header] = ctype |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 752 | |
| 753 | def del_param(self, param, header='content-type', requote=True): |
| 754 | """Remove the given parameter completely from the Content-Type header. |
| 755 | |
| 756 | The header will be re-written in place without the parameter or its |
| 757 | value. All values will be quoted as necessary unless requote is |
| 758 | False. Optional header specifies an alternative to the Content-Type |
| 759 | header. |
| 760 | """ |
| 761 | if header not in self: |
| 762 | return |
| 763 | new_ctype = '' |
| 764 | for p, v in self.get_params(header=header, unquote=requote): |
| 765 | if p.lower() != param.lower(): |
| 766 | if not new_ctype: |
| 767 | new_ctype = _formatparam(p, v, requote) |
| 768 | else: |
| 769 | new_ctype = SEMISPACE.join([new_ctype, |
| 770 | _formatparam(p, v, requote)]) |
| 771 | if new_ctype != self.get(header): |
| 772 | del self[header] |
| 773 | self[header] = new_ctype |
| 774 | |
| 775 | def set_type(self, type, header='Content-Type', requote=True): |
| 776 | """Set the main type and subtype for the Content-Type header. |
| 777 | |
| 778 | type must be a string in the form "maintype/subtype", otherwise a |
| 779 | ValueError is raised. |
| 780 | |
| 781 | This method replaces the Content-Type header, keeping all the |
| 782 | parameters in place. If requote is False, this leaves the existing |
| 783 | header's quoting as is. Otherwise, the parameters will be quoted (the |
| 784 | default). |
| 785 | |
| 786 | An alternative header can be specified in the header argument. When |
| 787 | the Content-Type header is set, we'll always also add a MIME-Version |
| 788 | header. |
| 789 | """ |
| 790 | # BAW: should we be strict? |
| 791 | if not type.count('/') == 1: |
| 792 | raise ValueError |
| 793 | # Set the Content-Type, you get a MIME-Version |
| 794 | if header.lower() == 'content-type': |
| 795 | del self['mime-version'] |
| 796 | self['MIME-Version'] = '1.0' |
| 797 | if header not in self: |
| 798 | self[header] = type |
| 799 | return |
| 800 | params = self.get_params(header=header, unquote=requote) |
| 801 | del self[header] |
| 802 | self[header] = type |
| 803 | # Skip the first param; it's the old type. |
| 804 | for p, v in params[1:]: |
| 805 | self.set_param(p, v, header, requote) |
| 806 | |
| 807 | def get_filename(self, failobj=None): |
| 808 | """Return the filename associated with the payload if present. |
| 809 | |
| 810 | The filename is extracted from the Content-Disposition header's |
| 811 | `filename' parameter, and it is unquoted. If that header is missing |
| 812 | the `filename' parameter, this method falls back to looking for the |
| 813 | `name' parameter. |
| 814 | """ |
| 815 | missing = object() |
| 816 | filename = self.get_param('filename', missing, 'content-disposition') |
| 817 | if filename is missing: |
R. David Murray | bf2e0aa | 2009-10-10 00:13:32 +0000 | [diff] [blame] | 818 | filename = self.get_param('name', missing, 'content-type') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 819 | if filename is missing: |
| 820 | return failobj |
| 821 | return utils.collapse_rfc2231_value(filename).strip() |
| 822 | |
| 823 | def get_boundary(self, failobj=None): |
| 824 | """Return the boundary associated with the payload if present. |
| 825 | |
| 826 | The boundary is extracted from the Content-Type header's `boundary' |
| 827 | parameter, and it is unquoted. |
| 828 | """ |
| 829 | missing = object() |
| 830 | boundary = self.get_param('boundary', missing) |
| 831 | if boundary is missing: |
| 832 | return failobj |
| 833 | # RFC 2046 says that boundaries may begin but not end in w/s |
| 834 | return utils.collapse_rfc2231_value(boundary).rstrip() |
| 835 | |
| 836 | def set_boundary(self, boundary): |
| 837 | """Set the boundary parameter in Content-Type to 'boundary'. |
| 838 | |
| 839 | This is subtly different than deleting the Content-Type header and |
| 840 | adding a new one with a new boundary parameter via add_header(). The |
| 841 | main difference is that using the set_boundary() method preserves the |
| 842 | order of the Content-Type header in the original message. |
| 843 | |
| 844 | HeaderParseError is raised if the message has no Content-Type header. |
| 845 | """ |
| 846 | missing = object() |
| 847 | params = self._get_params_preserve(missing, 'content-type') |
| 848 | if params is missing: |
| 849 | # There was no Content-Type header, and we don't know what type |
| 850 | # to set it to, so raise an exception. |
| 851 | raise errors.HeaderParseError('No Content-Type header found') |
| 852 | newparams = [] |
| 853 | foundp = False |
| 854 | for pk, pv in params: |
| 855 | if pk.lower() == 'boundary': |
| 856 | newparams.append(('boundary', '"%s"' % boundary)) |
| 857 | foundp = True |
| 858 | else: |
| 859 | newparams.append((pk, pv)) |
| 860 | if not foundp: |
| 861 | # The original Content-Type header had no boundary attribute. |
| 862 | # Tack one on the end. BAW: should we raise an exception |
| 863 | # instead??? |
| 864 | newparams.append(('boundary', '"%s"' % boundary)) |
| 865 | # Replace the existing Content-Type header with the new value |
| 866 | newheaders = [] |
| 867 | for h, v in self._headers: |
| 868 | if h.lower() == 'content-type': |
| 869 | parts = [] |
| 870 | for k, v in newparams: |
| 871 | if v == '': |
| 872 | parts.append(k) |
| 873 | else: |
| 874 | parts.append('%s=%s' % (k, v)) |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 875 | val = SEMISPACE.join(parts) |
| 876 | newheaders.append(self.policy.header_store_parse(h, val)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 877 | |
| 878 | else: |
| 879 | newheaders.append((h, v)) |
| 880 | self._headers = newheaders |
| 881 | |
| 882 | def get_content_charset(self, failobj=None): |
| 883 | """Return the charset parameter of the Content-Type header. |
| 884 | |
| 885 | The returned string is always coerced to lower case. If there is no |
| 886 | Content-Type header, or if that header has no charset parameter, |
| 887 | failobj is returned. |
| 888 | """ |
| 889 | missing = object() |
| 890 | charset = self.get_param('charset', missing) |
| 891 | if charset is missing: |
| 892 | return failobj |
| 893 | if isinstance(charset, tuple): |
| 894 | # RFC 2231 encoded, so decode it, and it better end up as ascii. |
| 895 | pcharset = charset[0] or 'us-ascii' |
| 896 | try: |
| 897 | # LookupError will be raised if the charset isn't known to |
| 898 | # Python. UnicodeError will be raised if the encoded text |
| 899 | # contains a character not in the charset. |
Barry Warsaw | 2cc1f6d | 2007-08-30 14:28:55 +0000 | [diff] [blame] | 900 | as_bytes = charset[2].encode('raw-unicode-escape') |
| 901 | charset = str(as_bytes, pcharset) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 902 | except (LookupError, UnicodeError): |
| 903 | charset = charset[2] |
| 904 | # charset characters must be in us-ascii range |
| 905 | try: |
| 906 | charset.encode('us-ascii') |
| 907 | except UnicodeError: |
| 908 | return failobj |
| 909 | # RFC 2046, $4.1.2 says charsets are not case sensitive |
| 910 | return charset.lower() |
| 911 | |
| 912 | def get_charsets(self, failobj=None): |
| 913 | """Return a list containing the charset(s) used in this message. |
| 914 | |
| 915 | The returned list of items describes the Content-Type headers' |
| 916 | charset parameter for this message and all the subparts in its |
| 917 | payload. |
| 918 | |
| 919 | Each item will either be a string (the value of the charset parameter |
| 920 | in the Content-Type header of that part) or the value of the |
| 921 | 'failobj' parameter (defaults to None), if the part does not have a |
| 922 | main MIME type of "text", or the charset is not defined. |
| 923 | |
| 924 | The list will contain one string for each part of the message, plus |
| 925 | one for the container message (i.e. self), so that a non-multipart |
| 926 | message will still return a list of length 1. |
| 927 | """ |
| 928 | return [part.get_content_charset(failobj) for part in self.walk()] |
| 929 | |
R David Murray | b744f3a | 2015-05-16 15:41:07 -0400 | [diff] [blame] | 930 | def get_content_disposition(self): |
| 931 | """Return the message's content-disposition if it exists, or None. |
| 932 | |
| 933 | The return values can be either 'inline', 'attachment' or None |
| 934 | according to the rfc2183. |
| 935 | """ |
| 936 | value = self.get('content-disposition') |
| 937 | if value is None: |
| 938 | return None |
| 939 | c_d = _splitparam(value)[0].lower() |
| 940 | return c_d |
| 941 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 942 | # I.e. def walk(self): ... |
| 943 | from email.iterators import walk |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 944 | |
| 945 | |
| 946 | class MIMEPart(Message): |
| 947 | |
| 948 | def __init__(self, policy=None): |
| 949 | if policy is None: |
| 950 | from email.policy import default |
| 951 | policy = default |
| 952 | Message.__init__(self, policy) |
| 953 | |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 954 | def is_attachment(self): |
| 955 | c_d = self.get('content-disposition') |
R David Murray | 97dfad7 | 2014-09-20 17:44:53 -0400 | [diff] [blame] | 956 | return False if c_d is None else c_d.content_disposition == 'attachment' |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 957 | |
| 958 | def _find_body(self, part, preferencelist): |
R David Murray | 8a97896 | 2014-09-20 18:05:28 -0400 | [diff] [blame] | 959 | if part.is_attachment(): |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 960 | return |
| 961 | maintype, subtype = part.get_content_type().split('/') |
| 962 | if maintype == 'text': |
| 963 | if subtype in preferencelist: |
| 964 | yield (preferencelist.index(subtype), part) |
| 965 | return |
| 966 | if maintype != 'multipart': |
| 967 | return |
| 968 | if subtype != 'related': |
| 969 | for subpart in part.iter_parts(): |
| 970 | yield from self._find_body(subpart, preferencelist) |
| 971 | return |
| 972 | if 'related' in preferencelist: |
| 973 | yield (preferencelist.index('related'), part) |
| 974 | candidate = None |
| 975 | start = part.get_param('start') |
| 976 | if start: |
| 977 | for subpart in part.iter_parts(): |
| 978 | if subpart['content-id'] == start: |
| 979 | candidate = subpart |
| 980 | break |
| 981 | if candidate is None: |
| 982 | subparts = part.get_payload() |
| 983 | candidate = subparts[0] if subparts else None |
| 984 | if candidate is not None: |
| 985 | yield from self._find_body(candidate, preferencelist) |
| 986 | |
| 987 | def get_body(self, preferencelist=('related', 'html', 'plain')): |
| 988 | """Return best candidate mime part for display as 'body' of message. |
| 989 | |
| 990 | Do a depth first search, starting with self, looking for the first part |
| 991 | matching each of the items in preferencelist, and return the part |
| 992 | corresponding to the first item that has a match, or None if no items |
| 993 | have a match. If 'related' is not included in preferencelist, consider |
| 994 | the root part of any multipart/related encountered as a candidate |
| 995 | match. Ignore parts with 'Content-Disposition: attachment'. |
| 996 | """ |
| 997 | best_prio = len(preferencelist) |
| 998 | body = None |
| 999 | for prio, part in self._find_body(self, preferencelist): |
| 1000 | if prio < best_prio: |
| 1001 | best_prio = prio |
| 1002 | body = part |
| 1003 | if prio == 0: |
| 1004 | break |
| 1005 | return body |
| 1006 | |
| 1007 | _body_types = {('text', 'plain'), |
| 1008 | ('text', 'html'), |
| 1009 | ('multipart', 'related'), |
| 1010 | ('multipart', 'alternative')} |
| 1011 | def iter_attachments(self): |
| 1012 | """Return an iterator over the non-main parts of a multipart. |
| 1013 | |
| 1014 | Skip the first of each occurrence of text/plain, text/html, |
| 1015 | multipart/related, or multipart/alternative in the multipart (unless |
| 1016 | they have a 'Content-Disposition: attachment' header) and include all |
| 1017 | remaining subparts in the returned iterator. When applied to a |
| 1018 | multipart/related, return all parts except the root part. Return an |
| 1019 | empty iterator when applied to a multipart/alternative or a |
| 1020 | non-multipart. |
| 1021 | """ |
| 1022 | maintype, subtype = self.get_content_type().split('/') |
| 1023 | if maintype != 'multipart' or subtype == 'alternative': |
| 1024 | return |
| 1025 | parts = self.get_payload() |
| 1026 | if maintype == 'multipart' and subtype == 'related': |
| 1027 | # For related, we treat everything but the root as an attachment. |
| 1028 | # The root may be indicated by 'start'; if there's no start or we |
| 1029 | # can't find the named start, treat the first subpart as the root. |
| 1030 | start = self.get_param('start') |
| 1031 | if start: |
| 1032 | found = False |
| 1033 | attachments = [] |
| 1034 | for part in parts: |
| 1035 | if part.get('content-id') == start: |
| 1036 | found = True |
| 1037 | else: |
| 1038 | attachments.append(part) |
| 1039 | if found: |
| 1040 | yield from attachments |
| 1041 | return |
| 1042 | parts.pop(0) |
| 1043 | yield from parts |
| 1044 | return |
| 1045 | # Otherwise we more or less invert the remaining logic in get_body. |
| 1046 | # This only really works in edge cases (ex: non-text relateds or |
| 1047 | # alternatives) if the sending agent sets content-disposition. |
| 1048 | seen = [] # Only skip the first example of each candidate type. |
| 1049 | for part in parts: |
| 1050 | maintype, subtype = part.get_content_type().split('/') |
| 1051 | if ((maintype, subtype) in self._body_types and |
R David Murray | 8a97896 | 2014-09-20 18:05:28 -0400 | [diff] [blame] | 1052 | not part.is_attachment() and subtype not in seen): |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 1053 | seen.append(subtype) |
| 1054 | continue |
| 1055 | yield part |
| 1056 | |
| 1057 | def iter_parts(self): |
| 1058 | """Return an iterator over all immediate subparts of a multipart. |
| 1059 | |
| 1060 | Return an empty iterator for a non-multipart. |
| 1061 | """ |
| 1062 | if self.get_content_maintype() == 'multipart': |
| 1063 | yield from self.get_payload() |
| 1064 | |
| 1065 | def get_content(self, *args, content_manager=None, **kw): |
| 1066 | if content_manager is None: |
| 1067 | content_manager = self.policy.content_manager |
| 1068 | return content_manager.get_content(self, *args, **kw) |
| 1069 | |
| 1070 | def set_content(self, *args, content_manager=None, **kw): |
| 1071 | if content_manager is None: |
| 1072 | content_manager = self.policy.content_manager |
| 1073 | content_manager.set_content(self, *args, **kw) |
| 1074 | |
| 1075 | def _make_multipart(self, subtype, disallowed_subtypes, boundary): |
| 1076 | if self.get_content_maintype() == 'multipart': |
| 1077 | existing_subtype = self.get_content_subtype() |
| 1078 | disallowed_subtypes = disallowed_subtypes + (subtype,) |
| 1079 | if existing_subtype in disallowed_subtypes: |
| 1080 | raise ValueError("Cannot convert {} to {}".format( |
| 1081 | existing_subtype, subtype)) |
| 1082 | keep_headers = [] |
| 1083 | part_headers = [] |
| 1084 | for name, value in self._headers: |
| 1085 | if name.lower().startswith('content-'): |
| 1086 | part_headers.append((name, value)) |
| 1087 | else: |
| 1088 | keep_headers.append((name, value)) |
| 1089 | if part_headers: |
| 1090 | # There is existing content, move it to the first subpart. |
| 1091 | part = type(self)(policy=self.policy) |
| 1092 | part._headers = part_headers |
| 1093 | part._payload = self._payload |
| 1094 | self._payload = [part] |
| 1095 | else: |
| 1096 | self._payload = [] |
| 1097 | self._headers = keep_headers |
| 1098 | self['Content-Type'] = 'multipart/' + subtype |
| 1099 | if boundary is not None: |
| 1100 | self.set_param('boundary', boundary) |
| 1101 | |
| 1102 | def make_related(self, boundary=None): |
| 1103 | self._make_multipart('related', ('alternative', 'mixed'), boundary) |
| 1104 | |
| 1105 | def make_alternative(self, boundary=None): |
| 1106 | self._make_multipart('alternative', ('mixed',), boundary) |
| 1107 | |
| 1108 | def make_mixed(self, boundary=None): |
| 1109 | self._make_multipart('mixed', (), boundary) |
| 1110 | |
| 1111 | def _add_multipart(self, _subtype, *args, _disp=None, **kw): |
| 1112 | if (self.get_content_maintype() != 'multipart' or |
| 1113 | self.get_content_subtype() != _subtype): |
| 1114 | getattr(self, 'make_' + _subtype)() |
| 1115 | part = type(self)(policy=self.policy) |
| 1116 | part.set_content(*args, **kw) |
| 1117 | if _disp and 'content-disposition' not in part: |
| 1118 | part['Content-Disposition'] = _disp |
| 1119 | self.attach(part) |
| 1120 | |
| 1121 | def add_related(self, *args, **kw): |
| 1122 | self._add_multipart('related', *args, _disp='inline', **kw) |
| 1123 | |
| 1124 | def add_alternative(self, *args, **kw): |
| 1125 | self._add_multipart('alternative', *args, **kw) |
| 1126 | |
| 1127 | def add_attachment(self, *args, **kw): |
| 1128 | self._add_multipart('mixed', *args, _disp='attachment', **kw) |
| 1129 | |
| 1130 | def clear(self): |
| 1131 | self._headers = [] |
| 1132 | self._payload = None |
| 1133 | |
| 1134 | def clear_content(self): |
| 1135 | self._headers = [(n, v) for n, v in self._headers |
| 1136 | if not n.lower().startswith('content-')] |
| 1137 | self._payload = None |
| 1138 | |
| 1139 | |
| 1140 | class EmailMessage(MIMEPart): |
| 1141 | |
| 1142 | def set_content(self, *args, **kw): |
| 1143 | super().set_content(*args, **kw) |
| 1144 | if 'MIME-Version' not in self: |
| 1145 | self['MIME-Version'] = '1.0' |