MailTag is a Python-based email automation tool that classifies and organizes your emails. It supports both IMAP and Gmail, allowing you to connect to your email account, classify emails using a local AI model (via Ollama), and automatically move them to a specified folder or label.
The src/main.py script provides a command-line interface to:
- Connect to Your Email: It can connect to an IMAP server or your Gmail account.
- Fetch Emails: It fetches emails from your inbox, with optional filters for subject, sender, and status.
- Classify Emails: For each email, it uses the configured Ollama model to classify it into a category based on your
classification_schema.yml. - Move Emails: It moves classified emails to a specified destination folder (for IMAP) or label (for Gmail).
- Generate Filters: It can also generate a
mailfilter.xmlfile from your classification database, which can be imported into Gmail.
- Python 3.12+
- Ollama with the model specified in
config.tomlpulled (e.g.,ollama pull gemma3)
-
Clone the repository:
git clone https://github.com/fjacquet/mailtag.git cd mailtag -
Install the dependencies:
uv pip install -e ".[dev]"To enable Gmail integration, install the optional Google API libraries:
uv pip install -e ".[gmail]" # or with pip pip install "mailtag[gmail]"
The application is configured via the config.toml file.
general.ollama_model: The name of the Ollama model to use for classification.logging.level: The logging level (e.g.,INFO,DEBUG).logging.file: The path to the log file.
To use the IMAP provider, add an [imap] section to your config.toml:
[imap]
host = "your-imap-server.com"
user = "[email protected]"You also need to set the IMAP_PASSWORD environment variable. You can do this by creating a .env file in the project root:
IMAP_PASSWORD="your-password"
To use the Gmail provider, you need to enable the Gmail API and get a credentials.json file.
Gmail support requires extra dependencies. Ensure you've installed them via pip install "mailtag[gmail]" or uv pip install -e ".[gmail]" before continuing.
-
Enable the Gmail API:
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- In the navigation menu, go to APIs & Services > Library.
- Search for "Gmail API" and enable it.
-
Create OAuth 2.0 Credentials:
- Go to APIs & Services > Credentials.
- Click Create Credentials > OAuth client ID.
- Select Desktop app as the application type.
- Click Create.
- Download the JSON file and save it as
credentials.jsonin the project root.
Once you have your credentials.json file, add a [gmail] section to your config.toml:
[gmail]
credentials_file = "credentials.json"
token_file = "token.json"The first time you run the script with the gmail provider, you will be prompted to authorize the application. A token.json file will be created to store your credentials for future runs.
The script is run from the command line.
python src/main.py --provider <imap|gmail> [options]Arguments:
--provider: The email provider to use (imaporgmail).--destination: The destination folder (for IMAP) or label (for Gmail) to move emails to. Defaults toProcessed.--subject: Filter emails by subject (case-insensitive).--sender: Filter emails by sender (case-insensitive).--status: Filter emails by status (SEENorUNSEEN).
Example:
python src/main.py --provider gmail --destination "My Label" --subject "Invoice"To generate a mailfilter.xml file from your classification database:
python src/main.py --generate-filtersdata/classification_schema.yml: Defines the categories for email classification.db/sender_classification_db.json: Stores the classification history for each sender.