Skip to content

Instantly share code, notes, and snippets.

@skittleson
Last active June 25, 2024 17:20
Show Gist options
  • Save skittleson/2c6e7a436229f558c86f3c67cefb2ee3 to your computer and use it in GitHub Desktop.
Save skittleson/2c6e7a436229f558c86f3c67cefb2ee3 to your computer and use it in GitHub Desktop.
@staticmethod
def mark_for_deletions(from_email_to_delete: str):
client = __get_client()
client.select_folder("INBOX")
action_on_uids = []
for i in range(1, 3):
start = (datetime.now() + timedelta(days=-(30 * i))
).strftime('%d-%b-%Y')
end = (datetime.now() + timedelta(days=-((30 * i)-30))
).strftime('%d-%b-%Y')
search = (f'SINCE {start} BEFORE {end}').encode('utf-8')
print(search)
messages = client.search(search)
items = client.fetch(messages, "RFC822.HEADER").items()
for msgid, data in items:
envelope = email.message_from_bytes(data[b'RFC822.HEADER'])
from_address = envelope.get("From")
name, email_address = parseaddr(from_address)
if from_email_to_delete in email_address:
action_on_uids.append(msgid)
print(action_on_uids)
if len(action_on_uids) > 0:
client.delete_messages(action_on_uids)
client.logout()
return len(action_on_uids)
from imapclient import IMAPClient
import email
import csv
from py_linq import Enumerable
import os
from datetime import datetime, timedelta
from email.utils import parseaddr
from pprint import pprint
@staticmethod
def who_has_email_the_most():
from collections import Counter
HOST = "REDACTED"
USERNAME = os.environ.get("EMAIL")
PASSWORD = os.environ.get("EMAIL_PASSWORD")
if PASSWORD is None:
raise Exception("Must provide validate password")
client = IMAPClient(HOST, 993)
client.login(USERNAME, PASSWORD)
client.select_folder("INBOX")
from_counts = Counter()
for i in range(1, 11):
start = (datetime.now() + timedelta(days=-(30 * i))
).strftime('%d-%b-%Y')
end = (datetime.now() + timedelta(days=-((30 * i)-30))
).strftime('%d-%b-%Y')
search = (f'SINCE {start} BEFORE {end}').encode('utf-8')
print(search)
messages = client.search(search)
items = client.fetch(messages, "RFC822.HEADER").items()
for msgid, data in items:
envelope = email.message_from_bytes(data[b'RFC822.HEADER'])
from_address = envelope.get("From")
name, email_address = parseaddr(from_address)
from_counts[email_address] += 1
top_20_from = from_counts.most_common(20)
client.logout()
return top_20_from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment