ZackAkil
AI-got-this-gmail-delegator
JavaScript

๐Ÿค– AI-powered Gmail assistant that automatically analyzes emails and drafts responses using Google's Gemini AI

Last updated May 6, 2026
23
Stars
4
Forks
0
Issues
0
Stars/day
Attention Score
1
Language breakdown
No language data available.
โ–ธ Files click to expand
README

AI-got-this-gmail-delegator ๐Ÿค–

AI-powered Gmail assistant that automatically analyzes emails and drafts responses using Google's Gemini AI

Google Apps Script Gemini AI

AI-Got-this-Diagram.png

๐ŸŽฏ What This Does

This Google Apps Script transforms your Gmail into an AI-powered email assistant that:

  • ๐Ÿ” Automatically analyzes incoming emails using Google's Gemini AI
  • ๐Ÿค– Generates smart draft replies for emails that need responses
  • ๐Ÿท๏ธ Intelligently categorizes emails (AI-drafted, manual review, no reply needed)
  • ๐Ÿ“Š Logs everything to Google Sheets for tracking and analytics
  • ๐Ÿงน Manages Gmail labels automatically to keep your inbox organized

โœจ Key Features

  • ๐ŸŽฏ Smart Decision Making: AI determines if emails need replies, can be auto-drafted, or require manual attention
  • ๐Ÿ“ Custom Writing Style: Uses your personal writing style and knowledge base from Google Docs
  • ๐Ÿ”„ Dual AI Support: Works with both AI Studio API (API key) and Vertex AI (service account)
  • ๐Ÿ“ˆ Comprehensive Logging: Tracks all decisions and actions in Google Sheets
  • ๐Ÿท๏ธ Label Management: Automatic Gmail label organization with cleanup
  • ๐Ÿ›ก๏ธ Error Handling: Robust error handling and retry logic
  • โšก Performance Optimized: Lazy loading and caching for efficiency

๐Ÿš€ Quick Start

Prerequisites

  • Google Workspace account
  • Access to Google Apps Script
  • Gemini AI credentials (API key or service account)
  • Google Doc for knowledge base/context
  • Google Sheet for logging

1. ๐Ÿ“‹ Setup Gmail Labels

Create these labels in your Gmail:

  • ๐Ÿ†• New-Email - For emails to be processed
  • ๐Ÿค– AI-Drafted - For emails with AI-generated drafts
  • โœ๏ธ Needs-Manual-Reply - For emails requiring manual review
  • โ›”๏ธ No-Reply-Needed - For emails that don't need responses

2. ๐Ÿ”„ Create Gmail Filter

Create a Gmail filter to automatically assign the "๐Ÿ†• New-Email" label to incoming messages:

  • Open Gmail and click the gear icon โ†’ "See all settings"
  • Go to "Filters and Blocked Addresses" tab
  • Click "Create a new filter"
  • Set the filter criteria:
- From: -youremail@gmail.com (replace with your actual Gmail address) - Has the words: Leave empty - Doesn't have: Leave empty
  • Click "Create filter"
  • Select these actions:
- โœ… Apply the label: Choose "๐Ÿ†• New-Email" - โœ… Never send it to Spam (recommended) - โœ… Always mark it as important (optional)
  • Click "Create filter"
Gmail Filter Setup

๐ŸŽฏ Pro Tip: You can also create more specific filters for different types of emails (e.g., only work emails, only emails from certain domains, etc.) by adjusting the "From" field criteria.

3. ๐Ÿ”ง Configure the Script

  • Open Google Apps Script and create a new project
  • Copy the files from this repository:
- Code.gs - Main application logic - Auth.gs - Authentication configuration - Gemini.gs - AI integration - appsscript.json - Project configuration
  • Update configuration in Code.gs:
const CONTEXTDOCID = "your-google-doc-id"; // Knowledge base document
   const LOGGINGSHEETID = "your-google-sheet-id"; // Logging spreadsheet
   const MYWRITINGSTYLE = "Your personal writing style here...";
  • Configure AI credentials in Auth.gs:
// Option A: AI Studio API Key
   const CREDENTIALS = "your-api-key-here";

// Option B: Vertex AI Service Account const CREDENTIALS = { type: "service_account", project_id: "your-gcp-project-id", // ... rest of service account JSON };

4. ๐ŸŽฏ Set Up Your Knowledge Base

Create a Google Doc with:

  • Your role and responsibilities
  • Common email scenarios and responses
  • Company policies and procedures
  • Personal preferences and writing style
  • Any specific context the AI should know

5. ๐Ÿ“Š Create Logging Spreadsheet

Create a Google Sheet with these columns:

  • Timestamp
  • Email Link
  • Subject
  • Sender
  • Label
  • Reasoning
  • Thread ID

6. โšก Set Up Triggers

  • Manual trigger: Run processNewEmails() to test
  • Automatic trigger: Set up a time-based trigger to run every 60 minutes, or however frequently you wish.

๐Ÿ”ง Configuration Options

AI Model Settings

const GEMINI_MODEL = "gemini-2.0-flash"; // or "gemini-2.5-flash"

Writing Style

const MYWRITINGSTYLE = 
Write in a clear, concise, and helpful tone. 
Be professional and friendly. 
Sign off with "Cheers, Zack".
;

Label Names

const NEWEMAILLABEL_NAME = "๐Ÿ†• New-Email";
const DRAFTEDLABELNAME = "๐Ÿค– AI-Drafted";
const MANUALREPLYLABEL_NAME = "โœ๏ธ Needs-Manual-Reply";
const NOREPLYNEEDEDLABELNAME = "โ›”๏ธ No-Reply-Needed";

๐Ÿ“– How It Works

1. ๐Ÿ“ง Email Detection

  • Monitors Gmail for emails with the "๐Ÿ†• New-Email" label
  • Processes the most recent message in each thread

2. ๐Ÿง  AI Analysis

  • Loads your knowledge base from Google Docs
  • Sends email content to Gemini AI for analysis
  • AI determines if the email needs a response

3. ๐ŸŽฏ Decision Making

  • AI Draft: Generates a draft reply using your writing style
  • Manual Review: Flags for your attention
  • No Reply: Marks as not needing a response

4. ๐Ÿท๏ธ Label Management

  • Applies appropriate labels based on AI decision
  • Removes the "New-Email" label
  • Cleans up any existing process labels

5. ๐Ÿ“Š Logging

  • Records all decisions and actions to Google Sheets
  • Includes reasoning and metadata for tracking

๐Ÿ› ๏ธ Available Functions

Main Functions

  • processNewEmails() - Main function to process all new emails
  • testGemini() - Test the AI integration
  • reset() - Reset authorization state

Utility Functions

  • getCachedContext() - Load knowledge base with caching
  • analyzeEmailWithAI() - AI analysis of email content
  • createDraftReply() - Generate Gmail draft
  • logEmailProcessing() - Log to spreadsheet

๐Ÿ” Troubleshooting

Common Issues

โŒ "Label does not exist"

  • Create the required Gmail labels manually
  • Check label names match configuration
โŒ "Could not retrieve context"
  • Verify Google Doc ID is correct
  • Check document permissions
โŒ "AI Studio API error"
  • Verify API key is valid
  • Check API quotas and limits
โŒ "Service account access error"
  • Verify service account permissions
  • Check GCP project configuration

Debug Mode

The script includes comprehensive logging with emojis:

  • ๐Ÿš€ Process start
  • ๐Ÿ“ง Email analysis
  • โœ… Success actions
  • โŒ Error conditions
  • ๐Ÿงน Cleanup operations

๐Ÿ“ˆ Monitoring & Analytics

Google Sheets Log

Track email processing with columns:

  • Timestamp: When the email was processed
  • Email Link: Direct link to the email
  • Subject: Email subject line
  • Sender: Who sent the email
  • Label: What action was taken
  • Reasoning: AI's reasoning for the decision
  • Thread ID: Gmail thread identifier

Performance Metrics

  • Processing time per email
  • Success/failure rates
  • AI decision distribution
  • Label usage statistics

๐Ÿ”’ Security & Privacy

  • No data storage: Emails are processed in memory only
  • Google's security: Runs on Google's secure infrastructure
  • API security: Uses official Google APIs with proper authentication
  • Logging control: You control what gets logged to Sheets

๐Ÿค Contributing

  • Fork the repository
  • Create a feature branch
  • Make your changes
  • Test thoroughly
  • Submit a pull request

๐Ÿ™ Acknowledgments

  • Google Apps Script for the platform
  • Google Gemini AI for the intelligence
  • Gmail API for email integration
  • Google Sheets API for logging

๐Ÿ“ž Support

If you encounter issues:

  • Check the troubleshooting section
  • Review the Google Apps Script logs
  • Verify your configuration
  • Test with testGemini() function

๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท ZackAkil/AI-got-this-gmail-delegator ยท Updated daily from GitHub