
The daily flood of emails is relentless. Between newsletters, notifications, and genuine correspondence, there’s a constant stream of spam and sophisticated phishing attempts trying to trick me, I don’t even know why they bother to be honest 🙄 . Standard email filters are good, but they’re reactive. I asked myself: what if I had a personal, intelligent guardian that didn’t just filter my email, but actively defended my inbox?
That question led me to create the SmartMail Guardian, an autonomous email defense agent I built from the ground up.
This isn’t just another spam filter. It’s a complete, event-driven system that connects securely to my email accounts, analyses incoming mail in real-time using multiple AI models, and takes autonomous action on high-confidence threats without me ever having to see them.
What Does the SmartMail Guardian Do?

At its core, the SmartMail Guardian is a multi-layered defense system with a real-time dashboard for oversight.
Multi-Account Monitoring: It securely connects to multiple email accounts (Gmail, Outlook, etc.) using the IMAP protocol, constantly watching for new arrivals. User credentials are encrypted and stored safely in a PostgreSQL database.
AI-Powered Threat Analysis: Every new email is subjected to a two-stage AI analysis:
- Spam Classification: A fine-tuned BERT model provides a rapid, highly accurate classification of whether an email is legitimate (‘ham’) or spam, complete with a confidence score.
- Phishing Detection: For more nuanced threats, the email content is passed to a locally-run Large Language Model (Mistral 7B). The LLM is prompted to analyse the text for phishing clues like false urgency, suspicious links, and impersonation, providing a clear reason for its decision.
Autonomous Action: This is where the “guardian” comes alive. Based on a simple rule—if the BERT model flags an email as spam with over 95% confidence—the system doesn’t wait for my input. It immediately logs back into the email server and deletes the threat. The junk never even reaches my eyes.
Real-Time Dashboard: For everything else, there’s a command center. I built a responsive frontend with React that uses WebSockets to display a live feed of all incoming emails that weren’t auto-deleted. From this dashboard, I can see the AI’s analysis and manually delete or mark emails as junk with a single click.
The Technical Architecture: How It All Works
Building a system like this involves orchestrating several moving parts:
- Frontend: A responsive dashboard built with React and styled with Tailwind CSS. It maintains a persistent WebSocket connection to the backend for a live, real-time feel.
- Backend API: A high-performance API built with FastAPI (Python) serves as the central hub. It handles requests from the frontend, manages database interactions, and adds new email processing jobs to a queue.
- Background Task Processor: Celery and Redis form the asynchronous backbone of the system. When a new email arrives, the API offloads the time-consuming AI analysis to a Celery worker, ensuring the API and UI remain fast and responsive.
- The AI Brain: The Celery worker is where the magic happens. It uses the transformers library for the BERT model and makes an HTTP request to a local Ollama server running the Mistral 7B LLM.
- Data & Services: A PostgreSQL database stores account credentials and email metadata, while Redis acts as the message broker for Celery.
The entire flow is a beautiful, asynchronous dance: the Email Fetcher finds an email, the API receives it, Celery analyzes it, and based on the AI’s verdict, it either takes autonomous action or pushes the result to the React dashboard for manual review.
This project was an incredible journey into building a full-stack, AI-powered application. It demonstrates how modern tools and LLMs can be orchestrated to create truly personalised and proactive software. I thoroughly enjoyed working on this and cannot wait to create more agents, stay tuned Bafwethu! 😎

