Label emails through Gmail with AI
This connects to your Gmail inbox, reads your unread emails, and automatically applies labels (that you can choose) based on the subject and other factors. It uses Google's Gmail API and an LLM running with Ollama to classify each message. Labels are created if they don't already exist.
- Automatic Gmail authentication via OAuth2
- Reads messages for classification
- AI-powered email classification using a local LLM, which, by default, is Llama 3.1 8B with Ollama
- Creates labels if missing and applies them
- Category recommendations: Analyze your untagged emails and get AI-suggested new categories
- Customizable categories (see model_wrapper.py)
- Runs fully locally (no external LLM API required)
- Docker support for easy deployment
- Python
- Gmail API (google-auth, google-auth-oauthlib, google-api-python-client)
- Ollama
- Llama 3.1 8B (default)
- BeautifulSoup (HTML parsing)
- Docker (optional)
- Authenticate with Gmail using OAuth2.
- Read unread emails from your inbox.
- Extract subject, body, and sender from message(s)
- Classify each email using local LLM.
- Create a Gmail label if needed and apply it to the message.
- Repeat for all unread messages.
- Go to Google Cloud Console and create a project.
- Enable the Gmail API for that project.
- Configure OAuth consent screen:
- User Type: "External"
- Add scopes as needed (probably needs
.../auth/gmail.modify)
- Create OAuth 2.0 Client ID:
- Application type: "Desktop app"
- Download the client credentials JSON.
- Save the file as
credentials.jsonin the project's root directory.
- Install Ollama from https://ollama.com
- Start the Ollama service (should run on http://localhost:11434).
- Pull the model used by this project:
ollama pull llama3.1:8b
- You can use any model you'd like as long as you make sure to edit
model_wrapper.py
- (Optional) Test that the model runs:
ollama run llama3.1:8b "Classify this email about a bank statement."
- Requires Python 3.10+
Clone the repository:
git clone https://github.com/mintoleda/GmailAIOrganizer.git
cd GmailAIOrganizerCreate and activate a virtual environment:
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activateInstall dependencies:
pip install -r requirements.txtNote: The app expects Ollama to be running locally at http://localhost:11434. If you run Ollama elsewhere, set OLLAMA_HOST accordingly, e.g.:
export OLLAMA_HOST=http://127.0.0.1:11434- On the first run, the app will detect
credentials.jsonand start a local OAuth flow:- After you approve, the app locally stores a
token.jsonfile for future runs.
- After you approve, the app locally stores a
If you ever need to re-authenticate, delete token.json and run the app again.
Run the classifier with default settings (checks 10 emails, classifies up to 10):
python read_emails.pyWith custom limits:
python read_emails.py --check 20 --classify 15| Flag | Description | Default |
|---|---|---|
--check N |
Total number of unread messages to check | 10 |
--classify N |
Number of messages to classify | 10 |
--recommend |
Recommend new categories based on untagged emails | - |
Analyze your unread, untagged emails and get AI-suggested categories:
python read_emails.py --recommendThis will:
- Fetch up to 50 unread emails that don't have any user labels
- Analyze them with the LLM
- Suggest up to 5 new categories (or confirm your current ones are sufficient)
Example output:
🔍 Analyzing unread, untagged emails for category recommendations...
Found 42 untagged emails to analyze.
💡 Suggested new categories:
• Travel
• Shopping
• Newsletters
📝 To add these, edit the CATEGORIES list in model_wrapper.py
Build and run using Docker Compose:
# Build the image
docker compose build
# Run with default settings
docker compose run gmail-organizer
# Run with arguments
docker compose run gmail-organizer --recommend
docker compose run gmail-organizer --check 20 --classify 15Note: Docker uses host.docker.internal to connect to Ollama running on your host machine. If Ollama is running elsewhere, modify the OLLAMA_HOST in docker-compose.yml.
- The classification categories and prompts can be adjusted in
model_wrapper.py. You can add, remove, or rename categories as needed.
- Your Gmail credentials (
credentials.json) and tokens (token.json) stay on your machine. - The LLM runs locally; no email content is sent to external LLM services.
- The app only creates/applies labels; it does not delete messages.
- Missing or invalid credentials:
- Ensure
credentials.jsonis in the project root. - Delete
token.jsonto re-authenticate if needed.
- Ensure
- Ollama/model not available:
- Make sure the Ollama service is running (
ollama serveif you run it manually). - Pull the model:
ollama pull llama3.1:8b. - If running on a non-default host/port, set
OLLAMA_HOST.
- Make sure the Ollama service is running (
- Gmail API permission errors:
- Ensure the OAuth consent screen is configured and the Gmail API is enabled.
- Confirm the app has
gmail.modifyscope if it applies labels.