I have af python script that download attchments from a POP3 mailbox. But I have a little problem with some encoding..
for i in range(self.count_inbox(client))[:20]:
lines = client.retr(i+1)[1]
mstring = string.join(lines, "\n")
# Loop trough part of each mails
for part in email.message_from_string(mstring).walk():
try:
filename = part.get_filename()
except:
filename = None
if filename:
attchments[filename] = part.get_payload(decode=True)
Some of the att files is named:
Good:
191720_15728883_20120207_151138.pdf
Invoice_3565382.pdf
Error:
=?Windows-1252?Q?Factuur_751845.pdf?=
=?Windows-1252?Q?Factuur_751845.pdf?=– this name MIME-encoded word, see: http://en.wikipedia.org/wiki/MIME#Encoded-WordYou can decode it with
email.header.decode_header, see example at:http://docs.python.org/library/email.header.html#email.header.decode_header