0

如何通过更正我的代码来解码电子邮件的主题?

例如 =?iso-8859-1?q?this=20is=20some=20text?=但需要得到 这是一些文本

def get_text(msg):
text = ""
if msg.is_multipart():
for part in msg.get_payload():
    if part.get_content_charset() is None:
    charset = chardet.detect(str(part))['encoding']
    else:
    charset = part.get_content_charset()
    if part.get_content_type() == 'text/plain':
    text = unicode(part.get_payload(decode=True),str(charset),"ignore")
    if part.get_content_type() == 'text/html':
    html = unicode(part.get_payload(decode=True),str(charset),"ignore")
if html is None:
    return text.strip()
else:
    return html.strip()
else:
text = unicode(msg.get_payload(decode=True),msg.get_content_charset(),'ignore')
return text.strip()

标头电子邮件

Content-Type: application/ms-tnef; name="winmail.dat"
Content-Transfer-Encoding: binary
From: 
To: 
Subject:
=?iso-8859-1?q?this=20is=20some=20text?= 
Thread-Topic:
=?iso-8859-1?q?this=20is=20some=20text?=
4

0 回答 0