Automatically reads your inbox (via IMAP), flags or deletes spam-like emails, and summarizes important unread ones every hour.
Feature | Details |
---|---|
Autonomy | Runs on a schedule (e.g., hourly) |
Perception | Reads inbox via IMAP |
Reasoning | Applies simple rules (e.g., keywords, sender) |
Action | Flags or deletes emails, creates a summary |
Output | Sends summary to a Telegram channel or logs it |
Tools Used
- Python
imaplib
– Email readingemail
– Parsing messagesschedule
orcron
– Time-based automationtelebot
orsmtplib
– For output/sending- (Optional) LangChain or OpenAI API – For smart summary
import imaplib
import email
import schedule
import time
EMAIL = '[email protected]'
PASSWORD = 'your_password'
IMAP_SERVER = 'imap.example.com'
def process_inbox():
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
mail.login(EMAIL, PASSWORD)
mail.select("inbox")
typ, data = mail.search(None, 'UNSEEN')
mail_ids = data[0].split()
for num in mail_ids:
typ, msg_data = mail.fetch(num, '(RFC822)')
msg = email.message_from_bytes(msg_data[0][1])
subject = msg.get('subject')
from_addr = msg.get('from')
if "unsubscribe" in subject.lower() or "offer" in subject.lower():
print(f"Spam Detected: {subject} from {from_addr}")
# mail.store(num, '+FLAGS', '\\Deleted') # Uncomment to delete
else:
print(f"Important: {subject} from {from_addr}")
# Summarize or notify
mail.logout()
schedule.every(1).hours.do(process_inbox)
while True:
schedule.run_pending()
time.sleep(1)
How to Try It Now
- Enable IMAP in Gmail or your provider.
- Use an app password if using Gmail.
- Save the script above as
agent.py
and run. - Watch the console logs – you’ll see classified email subjects.
- Schedule it as a cron job or keep it running.