<p>显然,如果<code>Content-Transfer-Encoding</code>是<code>8bit</code>,<code>message.get_payload(decode=False)</code>仍将尝试解码以恢复原始字节。另一方面,<code>message.get_payload(decode=True)</code>总是产生<code>bytes</code>,尽管实际解码只有在<code>Content-Transfer-Encoding</code>存在并且是<code>quoted-printable</code>或{<cd8>}时才会发生。在</p>
<p>最后我得到了以下代码。不确定这是否是处理电子邮件的正确方法。在</p>
<pre><code>body = []
if m.preamble is not None:
body.extend(m.preamble.splitlines(keepends=True))
for part in m.walk():
if part.is_multipart():
continue
ctype = part.get_content_type()
cte = part.get_params(header='Content-Transfer-Encoding')
if (ctype is not None and not ctype.startswith('text')) or \
(cte is not None and cte[0][0].lower() == '8bit'):
part_body = part.get_payload(decode=False)
else:
charset = part.get_content_charset()
if charset is None or len(charset) == 0:
charsets = ['ascii', 'utf-8']
else:
charsets = [charset]
part_body = part.get_payload(decode=True)
for enc in charsets:
try:
part_body = part_body.decode(enc)
break
except UnicodeDecodeError as ex:
continue
except LookupError as ex:
continue
else:
part_body = part.get_payload(decode=False)
body.extend(part_body.splitlines(keepends=True))
if m.epilogue is not None:
body.extend(m.epilogue.splitlines(keepends=True))
</code></pre>