An automated tool that uses Grok to identify mailing list emails in Apple Mail, attempts to unsubscribe from them, and provides detailed reports.
- Uses Grok to identify mass emails and newsletters
- Automatically detects and handles unsubscribe methods
- Moves identified emails to a "Suspected Mailing List" folder
- Generates detailed reports of processed emails
- Can run hourly via cron
- Works with multiple email accounts in Apple Mail
- macOS with Apple Mail configured
- Python 3.9 or higher
- uv (Python package installer)
- Grok API key
- Install uv if you haven't already:
curl -LsSf https://astral.sh/uv/install.sh | sh- Clone the repository:
git clone https://github.com/yourusername/spamfilter.git
cd spamfilter- Set up the virtual environment and install dependencies:
uv venv # you might need --python 3.11.6
source .venv/bin/activate
uv pip install -r requirements.txt- Create a .env file with your OpenAI API key:
echo "GROK_API_KEY=your-key-here" > .env- Make the cron script executable:
chmod +x run_spamfilter.shAdd this to your ~/.zshrc:
# Set up spamfilter cron job if it doesn't exist
if ! crontab -l | grep -q "run_spamfilter.sh"; then
(crontab -l 2>/dev/null; echo "0 * * * * /path/to/spamfilter/run_spamfilter.sh >> /path/to/spamfilter/cron.log 2>&1") | crontab -
fi
# Function to manage spamfilter cron
spamfilter_cron() {
case $1 in
"start")
(crontab -l 2>/dev/null; echo "0 * * * * /path/to/spamfilter/run_spamfilter.sh >> /path/to/spamfilter/cron.log 2>&1") | crontab -
echo "Spamfilter cron job started"
;;
"stop")
crontab -l | grep -v "run_spamfilter.sh" | crontab -
echo "Spamfilter cron job stopped"
;;
"status")
if crontab -l | grep -q "run_spamfilter.sh"; then
echo "Spamfilter cron job is running"
else
echo "Spamfilter cron job is not running"
fi
;;
*)
echo "Usage: spamfilter_cron [start|stop|status]"
;;
esac
}Remember to replace /path/to/spamfilter with your actual path to the repository.
# From the project directory
source .venv/bin/activate
python -m spamfilter.processorStart the hourly checks:
spamfilter_cron startStop the checks:
spamfilter_cron stopCheck status:
spamfilter_cron statusThe script generates a timestamped digest file (mail_list_digest_YYYYMMDD_HHMMSS.txt) containing:
- List of processed emails by account
- Unsubscribe attempt results
- Statistics on successful/failed/manual unsubscribes
- List of unique senders
spamfilter/
├── .env # Your API keys
├── .gitignore # Git ignore file
├── requirements.txt # Python dependencies
├── README.md # This file
├── run_spamfilter.sh # Cron execution script
└── spamfilter/ # Python package directory
├── __init__.py # Makes it a package
└── processor.py # Main code
- Checks all Mail.app inboxes for unread emails
- Uses GPT-4 to analyze each email's content and determine if it's a mailing list
- Attempts to unsubscribe using:
- List-Unsubscribe headers
- Unsubscribe links in email content
- Automated unsubscribe emails
- Moves identified emails to a "Suspected Mailing List" folder
- Generates a detailed report
View the cron job logs:
tail -f /path/to/spamfilter/cron.logAdjust these parameters in processor.py:
- GPT confidence threshold (default: 0.7)
- Folder name for mailing list emails (default: "Suspected Mailing List")
- Content length limit for GPT analysis (default: 4000 characters)
To modify the code:
- Activate the virtual environment:
source .venv/bin/activate- Make your changes
- Test manually by running:
python -m spamfilter.processor- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
MIT
This tool interacts with your email and makes automated decisions. While it's designed to be safe, always monitor its behavior initially and adjust as needed.# spamfilter