handle mail with bad charset

darcs-hash:20060302204557-86b55-21eff08ec557474d80642547bac4f1193d049c5e.gz
This commit is contained in:
David Rousselie
2006-03-02 21:45:57 +01:00
parent 8e9a420517
commit 26e10a264c

View File

@@ -200,11 +200,11 @@ class MailConnection(object):
def get_decoded_part(self, part, charset_hint): def get_decoded_part(self, part, charset_hint):
content_charset = part.get_content_charset() content_charset = part.get_content_charset()
if content_charset: result = u""
return unicode(part.get_payload(decode=True).decode(content_charset))
else:
result = ""
try: try:
if content_charset:
result = unicode(part.get_payload(decode=True).decode(content_charset))
else:
result = unicode(part.get_payload(decode=True)) result = unicode(part.get_payload(decode=True))
except Exception, e: except Exception, e:
try: try:
@@ -220,6 +220,7 @@ class MailConnection(object):
type, value, stack = sys.exc_info() type, value, stack = sys.exc_info()
print >>sys.stderr, "".join(traceback.format_exception print >>sys.stderr, "".join(traceback.format_exception
(type, value, stack, 5)) (type, value, stack, 5))
return result return result
def format_message(self, email_msg, include_body = True): def format_message(self, email_msg, include_body = True):
@@ -228,11 +229,11 @@ class MailConnection(object):
email_from = u"" email_from = u""
result = u"From : " result = u"From : "
for i in range(len(from_decoded)): for i in range(len(from_decoded)):
try:
if from_decoded[i][1]: if from_decoded[i][1]:
charset_hint = from_decoded[i][1] charset_hint = from_decoded[i][1]
email_from += unicode(from_decoded[i][0].decode(from_decoded[i][1])) email_from += unicode(from_decoded[i][0].decode(from_decoded[i][1]))
else: else:
try:
email_from += unicode(from_decoded[i][0]) email_from += unicode(from_decoded[i][0])
except Exception,e: except Exception,e:
try: try:
@@ -249,11 +250,11 @@ class MailConnection(object):
subject_decoded = email.Header.decode_header(email_msg["Subject"]) subject_decoded = email.Header.decode_header(email_msg["Subject"])
result += u"Subject : " result += u"Subject : "
for i in range(len(subject_decoded)): for i in range(len(subject_decoded)):
try:
if subject_decoded[i][1]: if subject_decoded[i][1]:
charset_hint = subject_decoded[i][1] charset_hint = subject_decoded[i][1]
result += unicode(subject_decoded[i][0].decode(subject_decoded[i][1])) result += unicode(subject_decoded[i][0].decode(subject_decoded[i][1]))
else: else:
try:
result += unicode(subject_decoded[i][0]) result += unicode(subject_decoded[i][0])
except Exception,e: except Exception,e:
try: try: