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 payl
(self, i=None, decode=False)
| 247 | " non-multipart payload") |
| 248 | |
| 249 | def get_payload(self, i=None, decode=False): |
| 250 | """Return a reference to the payload. |
| 251 | |
| 252 | The payload will either be a list object or a string. If you mutate |
| 253 | the list object, you modify the message's payload in place. Optional |
| 254 | i returns that index into the payload. |
| 255 | |
| 256 | Optional decode is a flag indicating whether the payload should be |
| 257 | decoded or not, according to the Content-Transfer-Encoding header |
| 258 | (default is False). |
| 259 | |
| 260 | When True and the message is not a multipart, the payload will be |
| 261 | decoded if this header's value is `quoted-printable' or `base64'. If |
| 262 | some other encoding is used, or the header is missing, or if the |
| 263 | payload has bogus data (i.e. bogus base64 or uuencoded data), the |
| 264 | payload is returned as-is. |
| 265 | |
| 266 | If the message is a multipart and the decode flag is True, then None |
| 267 | is returned. |
| 268 | """ |
| 269 | # Here is the logic table for this code, based on the email5.0.0 code: |
| 270 | # i decode is_multipart result |
| 271 | # ------ ------ ------------ ------------------------------ |
| 272 | # None True True None |
| 273 | # i True True None |
| 274 | # None False True _payload (a list) |
| 275 | # i False True _payload element i (a Message) |
| 276 | # i False False error (not a list) |
| 277 | # i True False error (not a list) |
| 278 | # None False False _payload |
| 279 | # None True False _payload decoded (bytes) |
| 280 | # Note that Barry planned to factor out the 'decode' case, but that |
| 281 | # isn't so easy now that we handle the 8 bit data, which needs to be |
| 282 | # converted in both the decode and non-decode path. |
| 283 | if self.is_multipart(): |
| 284 | if decode: |
| 285 | return None |
| 286 | if i is None: |
| 287 | return self._payload |
| 288 | else: |
| 289 | return self._payload[i] |
| 290 | # For backward compatibility, Use isinstance and this error message |
| 291 | # instead of the more logical is_multipart test. |
| 292 | if i is not None and not isinstance(self._payload, list): |
| 293 | raise TypeError('Expected list, got %s' % type(self._payload)) |
| 294 | payload = self._payload |
| 295 | cte = self.get('content-transfer-encoding', '') |
| 296 | if hasattr(cte, 'cte'): |
| 297 | cte = cte.cte |
| 298 | else: |
| 299 | # cte might be a Header, so for now stringify it. |
| 300 | cte = str(cte).strip().lower() |
| 301 | # payload may be bytes here. |
| 302 | if not decode: |
| 303 | if isinstance(payload, str) and utils._has_surrogates(payload): |
| 304 | try: |
| 305 | bpayload = payload.encode('ascii', 'surrogateescape') |
| 306 | try: |