Skip to content
Free Agents force Blog
Menu
  • Home
  • Agentforce
    • Agentforce 2.0
    • Agentforce 3.0
  • Data Cloud
  • Python
    • Autonomous Agents
    • Mini Apps
    • Utility
Menu

Email Cleanup Agent

Posted on July 4, 2025

Automatically reads your inbox (via IMAP), flags or deletes spam-like emails, and summarizes important unread ones every hour.

FeatureDetails
AutonomyRuns on a schedule (e.g., hourly)
PerceptionReads inbox via IMAP
ReasoningApplies simple rules (e.g., keywords, sender)
ActionFlags or deletes emails, creates a summary
OutputSends summary to a Telegram channel or logs it

Tools Used

  • Python
  • imaplib – Email reading
  • email – Parsing messages
  • schedule or cron – Time-based automation
  • telebot or smtplib – 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

  1. Enable IMAP in Gmail or your provider.
  2. Use an app password if using Gmail.
  3. Save the script above as agent.py and run.
  4. Watch the console logs – you’ll see classified email subjects.
  5. Schedule it as a cron job or keep it running.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Agentforce
  • Agentforce 2.0
  • Autonomous Agents
  • Python
  • Salesforce

Latest Post

  • Resume Filter Agent
  • Volunteer Response Agent
  • Email Cleanup Agent
©2025 Free Agents force Blog | Design: Newspaperly WordPress Theme