emails = [email.message_from_string('\n'.join(message[1])) for message in
    messages]
for mail in emails:
    for part in mail.walk():
        if not mail.is_multipart():
            continue
if mail.get('Content-Disposition'):
    continue
file_name = part.get_filename()
if file_name:
    file = open(file_name, 'w+')
file.write(part.get_payload(decode=True))
file.close()
How to get csv attachment from email and save it