I have the below piece of code which works fine for downloading CSV files. But I'm trying to download a file without any extension name where this is failing. The part.get_filename()
is not fetching anything and hence the code is failing with error NameError: name 'fileName' is not defined
. The search is working correctly and identifying the particular email.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
import os
import imaplib
import email
#from email.header import decode_header
#import webbrowser
import os
import datetime
import time
import glob
import shutil
today = datetime.date.today()
yday = today - datetime.timedelta(days=5)
# account credentials
username = "[email protected]"
with open(r'C:\Users\xyz\Downloads\Google sheet key\gmail_app_pwd.txt','r') as pwd:
password=pwd.read()
# create an IMAP4 class with SSL
mailBox = imaplib.IMAP4_SSL("imap.gmail.com")
# authenticate
mailBox.login(username, password)
svdir = r'C:\Users\xyz\Downloads\Work'
boxList = mailBox.list()
# print(boxList)
mailBox.select()
searchQuery = '(SUBJECT "Mailer as on ' + str(yday) +'")'
result, data = mailBox.uid('search', None, searchQuery)
ids = data[0]
# list of uids
id_list = ids.split()
i = len(id_list)
#x=0
for x in range(i):
latest_email_uid = id_list[x]
# fetch the email body (RFC822) for the given ID
result, email_data = mailBox.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = email_data[0][1]
# converts byte literal to string removing b''
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
# downloading attachments
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()
if bool(fileName):
filePath = os.path.join(svdir, fileName)
if not os.path.isfile(filePath) :
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
subject = str(email_message).split("Subject: ", 1)[1].split("\nTo:", 1)[0]
print('Downloaded "{file}" from email titled "{subject}" with UID {uid}.'.format(file=fileName, subject=subject, uid=latest_email_uid.decode('utf-8')))
mailBox.close()
mailBox.logout()
It worked after I removed the checks. Updated code:
for x in range(i):
latest_email_uid = id_list[x]
# fetch the email body (RFC822) for the given ID
result, email_data = mailBox.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = email_data[0][1]
# converts byte literal to string removing b''
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
# downloading attachments
for part in email_message.walk():
**fileName = "file.csv"**
if bool(fileName):
filePath = os.path.join(svdir, fileName)
if not os.path.isfile(filePath) :
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()