I want to automatically download the latest email attachment from gmail based on subject. i have daily data with same subject in email, and i want automatically download it everyday, can u guys help complete my code?
Here's my code :
typ, msgs = mail.search(None, '(FROM "Jordan Chai" SUBJECT "TEST")')
msgs = msgs[0].split()
for num in msgs:
resp, data = mail.fetch(num, "(RFC822)")
email_body = data[0][1]
m = email.message_from_bytes(email_body)
# downloading attachments
if m.get_content_maintype() != 'multipart':
continue
for part in m.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
if filename is not None:
sv_path = os.path.join(svdir, filename)
if not os.path.isfile(sv_path):
fp = open(sv_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
subject = str(m).split("Subject: ", 1)[1].split("\nTo:", 1)[0]
print('Downloaded "{file}" successfully to "{path}".'.format(file=filename, path=sv_path))
So, if there are email with subject "TEST", it will download the latest email with subject TEST and i want this script automatically works everyday, can u guys help?