Gmail Automation with CrewAI is an intelligent email management system that uses AI agents to categorize, organize, respond to, and clean up your Gmail inbox automatically.
- π Email Categorization: Automatically categorizes emails into specific types (newsletters, promotions, personal, etc.)
- π Priority Assignment: Assigns priority levels (HIGH, MEDIUM, LOW) based on content and sender with strict classification rules
- π·οΈ Smart Organization: Applies Gmail labels and stars based on categories and priorities
- π¬ Automated Responses: Generates draft responses for important emails that need replies
- π± Slack Notifications: Sends creative notifications for high-priority emails
- π§Ή Intelligent Cleanup: Safely deletes low-priority emails based on age and category
- π¬ YouTube Content Protection: Special handling for YouTube-related emails
- ποΈ Trash Management: Automatically empties trash to free up storage space
- 𧡠Thread Awareness: Recognizes and properly handles email threads
# Clone the repository
git clone https://github.com/tonykipkemboi/crewai-gmail-automation.git
cd crewai-gmail-automation
# Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
crewai install
- Create a
.env
file in the root directory with the following variables:
# Choose your LLM provider
# OpenAI (Recommended)
MODEL=openai/gpt-4o-mini
OPENAI_API_KEY=your_openai_api_key
# Or Gemini
# MODEL=gemini/gemini-2.0-flash
# GEMINI_API_KEY=your_gemini_api_key
# Or Ollama (Note: May have compatibility issues with tool calling)
# Download the model from https://ollama.com/library
# MODEL=ollama/llama3-groq-tool-use # use ones that have tool calling capabilities
# Gmail credentials
EMAIL_ADDRESS=your_email@gmail.com
APP_PASSWORD=your_app_password
# Optional: Slack notifications
SLACK_WEBHOOK_URL=your_slack_webhook_url
π How to create a Gmail App Password
- Go to your Google Account settings at myaccount.google.com
- Select Security from the left navigation panel
- Under "Signing in to Google," find and select 2-Step Verification (enable it if not already enabled)
- Scroll to the bottom and find App passwords
- Select Mail from the "Select app" dropdown
- Select Other (Custom name) from the "Select device" dropdown
- Enter
Gmail CrewAI
as the name - Click Generate
- Copy the 16-character password that appears (spaces will be removed automatically)
- Paste this password in your
.env
file as theAPP_PASSWORD
value - Click Done
Note: App passwords can only be created if you have 2-Step Verification enabled on your Google account.
π How to create a Slack Webhook URL
- Go to api.slack.com/apps
- Click Create New App
- Select From scratch
- Enter
Gmail Notifications
as the app name - Select your workspace and click Create App
- In the left sidebar, find and click on Incoming Webhooks
- Toggle the switch to Activate Incoming Webhooks
- Click Add New Webhook to Workspace
- Select the channel where you want to receive notifications
- Click Allow
- Find the Webhook URL section and copy the URL that begins with
https://hooks.slack.com/services/
- Paste this URL in your
.env
file as theSLACK_WEBHOOK_URL
value
Customizing your Slack app (optional):
- Go to Basic Information in the left sidebar
- Scroll down to Display Information
- Add an app icon and description
- Click Save Changes
Note: You need admin permissions or the ability to install apps in your Slack workspace.
This application uses the IMAP (Internet Message Access Protocol) to securely connect to your Gmail account and manage your emails. Here's how it works:
π IMAP Connection Process
-
Secure Connection: The application establishes a secure SSL connection to Gmail's IMAP server (
imap.gmail.com
). -
Authentication: It authenticates using your email address and app password (not your regular Google password).
-
Mailbox Access: Once authenticated, it can access your inbox and other mailboxes to:
- Read unread emails
- Apply labels
- Move emails to trash
- Save draft responses
-
Safe Disconnection: After each operation, the connection is properly closed to maintain security.
IMAP allows the application to work with your emails while they remain on Google's servers, unlike POP3 which would download them to your device. This means you can still access all emails through the regular Gmail interface.
Security Note: Your credentials are only stored locally in your .env
file and are never shared with any external services.
Run the application with:
crewai run
You'll be prompted to enter the number of emails to process (default is 5).
The application will:
- π₯ Fetch your unread emails
- π Categorize them by type and priority
- β Apply appropriate labels and stars
- βοΈ Generate draft responses for important emails
- π Send Slack notifications for high-priority items
- ποΈ Clean up low-priority emails based on age
- π§Ή Empty the trash to free up storage space
-
π Smart Deletion Rules:
- Promotions older than 2 days are automatically deleted
- Newsletters older than 7 days (unless HIGH priority) are deleted
- Shutterfly emails are always deleted regardless of age
- Receipts and important documents are archived instead of deleted
-
π¬ YouTube Protection: All YouTube-related emails are preserved and marked as READ_ONLY (you'll respond directly on YouTube)
-
βοΈ Smart Response Generation: Responses are tailored to the email context and include proper formatting
-
π‘ Creative Slack Notifications: Fun, attention-grabbing notifications for important emails
-
𧡠Thread Handling: Properly tracks and manages email threads to maintain conversation context
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.