Skip to content

Instantly share code, notes, and snippets.

@mkhorasani
Last active May 21, 2021 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhorasani/449e11e52d495be9176d3fdf92379407 to your computer and use it in GitHub Desktop.
Save mkhorasani/449e11e52d495be9176d3fdf92379407 to your computer and use it in GitHub Desktop.
for email_id in data[0].split():
_,data = mail.fetch(email_id, '(RFC822)' )
raw_email = data[0][1]
raw_email = raw_email.decode('utf-8')
email_content = email.message_from_string(raw_email)
if 'Exam Results' in email_content['Subject']: #Change this line to the subject you are searching for
for part in email_content.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
attachement = open('C:/Users/.../Desktop/%s' % (filename), 'wb')
attachement.write(part.get_payload(decode=True))
attachement.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment