GitHunt
TO

tonykipkemboi/crewai-gmail-automation

Multi-agent system that automates Gmail inbox management: categorizes emails, applies labels, and generates contextual responses using CrewAI

Gmail Automation with CrewAI ๐Ÿ“งโœจ

YouTube Channel Subscribers
GitHub followers
Twitter Follow
LinkedIn

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.

Gmail Automation

โœจ Features

Stars
Last Commit
Status

  • ๐Ÿ“‹ 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

๐Ÿš€ Installation

# 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

โš™๏ธ Configuration

  1. 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
  1. Go to your Google Account settings at myaccount.google.com
  2. Select Security from the left navigation panel
  3. Under "Signing in to Google," find and select 2-Step Verification (enable it if not already enabled)
  4. Scroll to the bottom and find App passwords
  5. Select Mail from the "Select app" dropdown
  6. Select Other (Custom name) from the "Select device" dropdown
  7. Enter Gmail CrewAI as the name
  8. Click Generate
  9. Copy the 16-character password that appears (spaces will be removed automatically)
  10. Paste this password in your .env file as the APP_PASSWORD value
  11. 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
  1. Go to api.slack.com/apps
  2. Click Create New App
  3. Select From scratch
  4. Enter Gmail Notifications as the app name
  5. Select your workspace and click Create App
  6. In the left sidebar, find and click on Incoming Webhooks
  7. Toggle the switch to Activate Incoming Webhooks
  8. Click Add New Webhook to Workspace
  9. Select the channel where you want to receive notifications
  10. Click Allow
  11. Find the Webhook URL section and copy the URL that begins with https://hooks.slack.com/services/
  12. Paste this URL in your .env file as the SLACK_WEBHOOK_URL value

Customizing your Slack app (optional):

  1. Go to Basic Information in the left sidebar
  2. Scroll down to Display Information
  3. Add an app icon and description
  4. Click Save Changes

Note: You need admin permissions or the ability to install apps in your Slack workspace.

๐Ÿ“ง How It Works

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
  1. Secure Connection: The application establishes a secure SSL connection to Gmail's IMAP server (imap.gmail.com).

  2. Authentication: It authenticates using your email address and app password (not your regular Google password).

  3. 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
  4. 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.

๐Ÿ” Usage

Run the application with:

crewai run

You'll be prompted to enter the number of emails to process (default is 5).

The application will:

  1. ๐Ÿ“ฅ Fetch your unread emails
  2. ๐Ÿ”Ž Categorize them by type and priority
  3. โญ Apply appropriate labels and stars
  4. โœ๏ธ Generate draft responses for important emails
  5. ๐Ÿ”” Send Slack notifications for high-priority items
  6. ๐Ÿ—‘๏ธ Clean up low-priority emails based on age
  7. ๐Ÿงน Empty the trash to free up storage space

๐ŸŒŸ Special Features

  • ๐Ÿ“… 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

๐Ÿ‘ฅ Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ“š References