I am loggin into a server with my credentials and I search for the latest email's attached file. I could read all information without any issues, such as 'FROM', 'TO', 'SUBJECT' and so on. One thing I could not understand is that file name of attached file could not be parsed correctly. The file name is should looks like '13079.xls', but it return a string like '=?utf-8?B?MTMwNzKueGxz?='.
Any suggestion?
import time
import imaplib
import base64
import os
import email
import glob
import csv
email_user = 'xxxxxxx@pangeare.com'
email_pass = 'xxxxxxxxxx'
host='imap.gmail.com'
port=993
mail = imaplib.IMAP4_SSL(host,port)
mail.login(email_user, email_pass)
mail.select('Inbox')
type, data = mail.search(None, 'ALL')
for num in data[0].split()[::-1]:
typ, data = mail.fetch(num, '(RFC822)' )
raw_email = data[0][1]
raw_email_string = raw_email.decode('ascii')
email_message = email.message_from_string(raw_email_string)
print( email_message['To'])
print( email_message['Subject'])
print (email.utils.parseaddr(email_message['From']))
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
print(filename)