Practical Tutorials#11

Building an Automated Email Assistant with OpenClaw

Create an AI assistant that can automatically read, categorize, and reply to emails.

12 min read2026-02-08
emailGmailautomation

Project Overview

In this tutorial, we'll build an AI assistant that can read, categorize, and respond to emails automatically using OpenClaw and Gmail integration.

Prerequisites

  • OpenClaw installed and running
  • Google Cloud project with Gmail API enabled
  • OAuth 2.0 credentials configured

Step 1: Configure Gmail MCP Server

// config.js
module.exports = {
  mcp: {
    servers: [
      {
        name: 'gmail',
        type: 'stdio',
        command: 'npx',
        args: ['@mcp-servers/gmail'],
        env: {
          GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
          GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET
        }
      }
    ]
  }
}

Step 2: Define Email Workflow in AGENTS.md

# Email Management Workflow

## When checking email:
1. Fetch unread emails from inbox
2. Categorize each email:
   - URGENT: From known contacts about deadlines
   - ACTION: Requires response or task
   - INFO: Newsletters, notifications
   - SPAM: Promotional, unsubscribe
3. Present summary to user by category
4. Wait for instructions before any action

## When drafting responses:
1. Match tone of original email
2. Keep responses concise
3. Always show draft before sending
4. Ask for confirmation before send

Step 3: Implement Email Checking

// User message: "Check my email"

// Agent executes:
const emails = await gmail.listMessages({
  query: 'is:unread',
  maxResults: 20
});

// Categorize and summarize
const summary = await categorizeEmails(emails);

// Response:
"📬 You have 12 unread emails:
- 🔴 URGENT (2): Meeting tomorrow, Contract review
- 📝 ACTION (5): Reply needed from team
- ℹ️ INFO (4): News updates
- 🗑️ SPAM (1): Promo email"

Step 4: Auto-Draft Responses

// User: "Draft replies to the urgent emails"

// Agent process:
for (const email of urgentEmails) {
  const draft = await generateDraft(email);
  await gmail.createDraft({
    to: email.from,
    subject: `Re: ${email.subject}`,
    body: draft
  });
}

// Response:
"✍️ Created 2 draft responses:
1. Re: Meeting tomorrow - Confirmed 2pm
2. Re: Contract review - Requested extension

Would you like to review these drafts?"

Step 5: Smart Filtering

# In USER.md - customize filtering

## Email Priorities
- Priority contacts: [email protected], [email protected]
- Auto-archive: newsletters@*, promo@*
- Never auto-reply: legal@*, hr@*

## Response Templates
- Meeting confirmations: Brief, confirm time/location
- Quick questions: Direct, helpful
- Formal requests: Professional tone

Security Considerations

  • Never auto-send without confirmation
  • Redact sensitive info in logs
  • Limit OAuth scopes to necessary permissions

Conclusion

You now have an intelligent email assistant that can manage your inbox while keeping you in control of all actions.