I am using python imaplib to download and save attachments in email. But when there is an email with an attached file in the email, x.get_payload () has Nonetype. I think these types of emails are sent using some email clients. Since the file name is missing, I tried to change the file name in the header and then "Content-Disposition". The renamed file opens and when I try to write this file using
fp.write(part.get_payload(decode=True))
he says a line or buffer is expected, but Nonetype is found.
>>>x.get_payload()
[<email.message.Message instance at 0x7f834eefa0e0>]
>>>type(part.get_payload())
<type 'list'>
>>>type(part.get_payload(decode=True))
<type 'NoneType'>
I deleted decode = True and I got a list of objects
x.get_payload()[0]
<email.message.Message instance at 0x7f834eefa0e0>
I tried editing the file name in case the email was found as an attachment.
if part.get('Content-Disposition'):
attachment = str(part.get_filename())
if attachment == 'None':
attachment = 'somename.mail'
attachment = self.autorename(attachment)
x.add_header('Content-Disposition', 'attachment', filename=attachment)
attachedmail = 1
if attachedmail == 1:
fp.write(str(x.get_payload()))
else:
fp.write(x.get_payload(decode=True))
,
[ < email.message.Message instance at 0x7fe5e09aa248 > ]
?