I'm testing with the asyncio module, however I need a hint / suggesstion how to fetch large emails in an async way.
I have a list with usernames and passwords for the mail accounts.
data = [
{'usern': '[email protected]', 'passw': 'x'},
{'usern': '[email protected]', 'passw': 'y'},
{'usern': '[email protected]', 'passw': 'z'} (...)
]
I thought about:
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait([get_attachment(d) for d in data]))
loop.close()
However, the long part is to download the email attachments.
Email:
@asyncio.coroutine
def get_attachment(d):
username = d['usern']
password = d['passw']
connection = imaplib.IMAP4_SSL('imap.bar.de')
connection.login(username, password)
connection.select()
# list all available mails
typ, data = connection.search(None, 'ALL')
for num in data[0].split():
# fetching each mail
typ, data = connection.fetch(num, '(RFC822)')
raw_string = data[0][1].decode('utf-8')
msg = email.message_from_string(raw_string)
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
if part.get_filename():
body = part.get_payload(decode=True)
# do something with the body, async?
connection.close()
connection.logout()
How could I process all (downloading attachments) mails in an async way?