-
-
Save shailendra9292/9c47f929533dc5366828f8e1f6ff6454 to your computer and use it in GitHub Desktop.
Python script to download all gmail attachments.(python3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import email | |
| import getpass, imaplib | |
| import os | |
| import sys | |
| detach_dir = '.' | |
| if 'attachments' not in os.listdir(detach_dir): | |
| os.mkdir('attachments') | |
| userName = input('Enter your GMail username:') | |
| passwd = getpass.getpass('Enter your password: ') | |
| try: | |
| imapSession = imaplib.IMAP4_SSL('imap.gmail.com') | |
| typ, accountDetails = imapSession.login(userName, passwd) | |
| if typ != 'OK': | |
| print('Not able to sign in!') | |
| raise | |
| imapSession.select('[Gmail]/All Mail') | |
| typ, data = imapSession.search(None, 'ALL') | |
| if typ != 'OK': | |
| print('Error searching Inbox.') | |
| raise | |
| # Iterating over all emails | |
| for msgId in data[0].split(): | |
| typ, messageParts = imapSession.fetch(msgId, '(RFC822)') | |
| if typ != 'OK': | |
| print('Error fetching mail.') | |
| raise | |
| emailBody = messageParts[0][1] | |
| mail = email.message_from_string(emailBody) | |
| for part in mail.walk(): | |
| if part.get_content_maintype() == 'multipart': | |
| # print part.as_string() | |
| continue | |
| if part.get('Content-Disposition') is None: | |
| # print part.as_string() | |
| continue | |
| fileName = part.get_filename() | |
| if bool(fileName): | |
| filePath = os.path.join(detach_dir, 'attachments', fileName) | |
| if not os.path.isfile(filePath) : | |
| print(fileName) | |
| fp = open(filePath, 'wb') | |
| fp.write(part.get_payload(decode=True)) | |
| fp.close() | |
| imapSession.close() | |
| imapSession.logout() | |
| except : | |
| print('Not able to download all attachments.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment