Answer the question
In order to leave comments, you need to log in
How to download files from mail attachments?
The code:
from imap_tools import MailBox
import email
mailbox = MailBox(host = 'imap.yandex.com', port = '993', ssl=True)
mailbox.login('[email protected]', 'PASSWORD')
for message in mailbox.fetch():
print(message.from_values['full'])
mail = message.obj
if mail.is_multipart():
for part in mail.walk():
content_type = part.get_content_type()
filename = part.get_filename()
if filename:
with open(part.get_filename(), 'wb') as new_file:
new_file.write(part.get_payload(decode = True))
Answer the question
In order to leave comments, you need to log in
Still, I had to register))
I did it like this:
h=email.header.make_header(email.header.decode_header(filename))
with open(str(h), 'wb') as new_file:
new_file.write(part.get_payload(decode=True))
did you manage to fix it? it is clear that the error is in fs and not in the mail?
for part in msg.walk():
content_type = part.get_content_type()
filename = part.get_filename()
if filename:
# Нам плохого не надо, в письме может быть всякое барахло
with open(part.get_filename(), 'w') as new_file:
f.write(str(part.get_payload(decode=True), encoding='utf-8'))
f.close
<b></b>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question