Last active
April 30, 2019 20:55
-
-
Save Skinner927/361bb20b1c565a7493cdc9d6960dfd2d to your computer and use it in GitHub Desktop.
Delete all emails from email inboxes via IMAP
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
| #!/usr/bin/env python3 | |
| import imaplib | |
| import sys | |
| from itertools import chain | |
| server = 'imap.mail.yahoo.com' | |
| port = 993 | |
| username = 'someone@yahoo.com' | |
| password = 'hunter2' | |
| def query_yes_no(question, default="yes"): | |
| """Ask a yes/no question via raw_input() and return their answer. | |
| "question" is a string that is presented to the user. | |
| "default" is the presumed answer if the user just hits <Enter>. | |
| It must be "yes" (the default), "no" or None (meaning | |
| an answer is required of the user). | |
| The "answer" return value is True for "yes" or False for "no". | |
| https://stackoverflow.com/a/3041990/721519 | |
| """ | |
| valid = {"yes": True, "y": True, "ye": True, | |
| "no": False, "n": False} | |
| if default is None: | |
| prompt = " [y/n] " | |
| elif default == "yes": | |
| prompt = " [Y/n] " | |
| elif default == "no": | |
| prompt = " [y/N] " | |
| else: | |
| raise ValueError("invalid default answer: '%s'" % default) | |
| while True: | |
| sys.stdout.write(question + prompt) | |
| choice = input().lower() | |
| if default is not None and choice == '': | |
| return valid[default] | |
| elif choice in valid: | |
| return valid[choice] | |
| else: | |
| sys.stdout.write("Please respond with 'yes' or 'no' " | |
| "(or 'y' or 'n').\n") | |
| def grouper(iterable, n): | |
| count = 0 | |
| current = [] | |
| for i in iterable: | |
| if count >= n: | |
| count = 0 | |
| yield current | |
| current = [] | |
| count += 1 | |
| current.append(i) | |
| if current: | |
| yield current | |
| m = imaplib.IMAP4_SSL(host=server, port=port) | |
| m.login(username, password) | |
| boxes = [b.decode().split('"')[-2] for b in m.list()[1]] | |
| print('Boxes', boxes) | |
| if not query_yes_no('Continue?'): | |
| sys.exit() | |
| for box in boxes: | |
| print('Delete all from: {} ?'.format(box)) | |
| if not query_yes_no('Process?'): | |
| continue | |
| while True: | |
| m.select(box) | |
| typ, data = m.search(None, 'ALL') | |
| if typ != 'OK': | |
| print('NOT OK! done with this box') | |
| break | |
| print('Got fresh list of mails') | |
| groups = grouper(chain.from_iterable(y.decode().split() for y in data), | |
| 200) | |
| had_any = False | |
| for group in groups: | |
| had_any = True | |
| print('Clobbering {} mails'.format(len(group))) | |
| typ, _ = m.store(','.join(group), '+FLAGS.SILENT', r'(\Deleted)') | |
| if typ != 'OK': | |
| print('NON-OK on store delete. Skipping Box') | |
| break | |
| print('Expunging') | |
| m.expunge() | |
| if not had_any: | |
| print('Done with this box') | |
| break | |
| print('All boxes done') |
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
| $ python3 email.py | |
| Boxes ['Archive', 'Bulk Mail', 'Draft', 'Inbox', 'Saves', 'Sent', 'Trash', 'junk'] | |
| Continue? [Y/n] | |
| Delete all from: Archive ? | |
| Process? [Y/n] n | |
| Delete all from: Bulk Mail ? | |
| Process? [Y/n] n | |
| Delete all from: Draft ? | |
| Process? [Y/n] n | |
| Delete all from: Inbox ? | |
| Process? [Y/n] | |
| Got fresh list of mails | |
| Done with this box | |
| Expunging | |
| Delete all from: Saves ? | |
| Process? [Y/n] n | |
| Delete all from: Sent ? | |
| Process? [Y/n] n | |
| Delete all from: Trash ? | |
| Process? [Y/n] Y | |
| Got fresh list of mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 16 mails | |
| Got fresh list of mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 16 mails | |
| Got fresh list of mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 16 mails | |
| Got fresh list of mails | |
| Done with this box | |
| Expunging | |
| Delete all from: junk ? | |
| Process? [Y/n] | |
| Got fresh list of mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 65 mails | |
| Got fresh list of mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 65 mails | |
| Got fresh list of mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 200 mails | |
| Clobbering 65 mails | |
| Got fresh list of mails | |
| Done with this box | |
| Expunging |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment