|
| 1 | +# Gmail Automation Python Project |
| 2 | + |
| 3 | +This project automates Gmail workflows using Python. It fetches emails, applies user-defined rules from `rules.json`, and saves results to PostgreSQL. |
| 4 | + |
| 5 | +## Features |
| 6 | +- Gmail API authentication (OAuth2) |
| 7 | +- Fetch unread emails |
| 8 | +- Parse sender, subject, and snippet |
| 9 | +- Apply rules (label, mark as read, etc.) from `rules.json` |
| 10 | +- Save results to PostgreSQL |
| 11 | +- Unit tests for all major modules |
| 12 | + |
| 13 | +## Project Structure |
| 14 | +``` |
| 15 | +app/ |
| 16 | + actions.py # Gmail actions (mark as read, label) |
| 17 | + auth.py # Gmail API authentication |
| 18 | + db.py # PostgreSQL save logic |
| 19 | + fetch_emails.py # Fetch and parse emails |
| 20 | + main.py # Example orchestrator |
| 21 | + rules_engine.py # Rule loading and application |
| 22 | + example_gmail_automation.py # Full workflow example |
| 23 | +
|
| 24 | +tests/ |
| 25 | + test_fetch_emails.py |
| 26 | + test_rules_engine.py |
| 27 | + test_db.py |
| 28 | +
|
| 29 | +rules.json # Example rules |
| 30 | +requirements.txt # Python dependencies |
| 31 | +``` |
| 32 | + |
| 33 | +## Setup |
| 34 | +1. Install dependencies: |
| 35 | + ``` |
| 36 | + pip install -r requirements.txt |
| 37 | + ``` |
| 38 | +2. Set up Gmail API credentials: |
| 39 | + - Download `credentials.json` from Google Cloud Console. |
| 40 | + - Place it in the project root. |
| 41 | +3. Set up PostgreSQL and create the `emails` table: |
| 42 | + ```sql |
| 43 | + CREATE TABLE emails ( |
| 44 | + id TEXT PRIMARY KEY, |
| 45 | + from_address TEXT, |
| 46 | + subject TEXT, |
| 47 | + snippet TEXT, |
| 48 | + timestamp TIMESTAMP, |
| 49 | + actions JSONB |
| 50 | + ); |
| 51 | + ``` |
| 52 | +4. Edit `rules.json` to define your rules. |
| 53 | + |
| 54 | +## Running |
| 55 | +- Run the main workflow: |
| 56 | + ``` |
| 57 | + python -m app.main |
| 58 | + ``` |
| 59 | +- Or run the full example: |
| 60 | + ``` |
| 61 | + python app/example_gmail_automation.py |
| 62 | + ``` |
| 63 | + |
| 64 | +## Testing |
| 65 | +Run all tests: |
| 66 | +``` |
| 67 | +python -m unittest discover tests |
| 68 | +``` |
| 69 | + |
| 70 | +## Example `rules.json` |
| 71 | +``` |
| 72 | +[ |
| 73 | + { |
| 74 | + "criteria": {"from": "newsletter@example.com"}, |
| 75 | + "actions": ["applyLabel:NEWSLETTERS"] |
| 76 | + }, |
| 77 | + { |
| 78 | + "criteria": {"subject": "Invoice"}, |
| 79 | + "actions": ["markRead"] |
| 80 | + } |
| 81 | +] |
| 82 | +``` |
0 commit comments