Automation

Creating an Email AI Agent with n8n's AI Agent Node

Learn how to build an AI-powered email agent using n8n's AI Agent node, combining automated message labeling and drafting features.

5 min read

Learn how to build a powerful AI-driven email agent with n8n's innovative AI Agent node. This guide will walk you through creating a workflow that automatically labels incoming messages, organizes them into specific folders, and drafts replies in a personalized manner.

Prerequisites for Setting Up an AI Agent in n8n

Before diving into building this email AI agent, ensure you have everything set up and ready:

prerequisites

  • n8n Platform: Install n8n locally or have access to its cloud instance.
  • Google Account: Enable the Gmail API for your account.
  • AI Model Access: Obtain API keys for your chosen AI model, such as the OpenAI Chat Model.
  • Basic Knowledge: Familiarity with n8n nodes and expressions is recommended.

Setting Up the Gmail Trigger Node

The Gmail Trigger node is the first step in this workflow. It detects new emails in your inbox to initiate the process.

steps

  1. Open your n8n workflow editor.
  2. Add a Gmail Trigger node to the canvas.
  3. Configure the node:
    • Set Trigger on to On message received.
    • Select your Google account and grant n8n the necessary access.
    • Adjust the Poll Time to define how frequently the node checks for new emails (e.g., every 15 minutes).
  4. Test the node to confirm successful Gmail integration.
bash
# Example command if running n8n locally
n8n start
# Open the browser to access the n8n editor

Building the Email Content Classifier Node

The Email Content Classifier Node uses AI-based text classification to label emails with predefined categories.

steps

  1. Add a Text Classifier node to the canvas.
  2. Rename the node (e.g., Email Content Classifier) to represent its role.
  3. Configure the node:
    • Use an expression in the Text to classify field to extract email previews from the Gmail Trigger node.
    • Add categories for email classification (e.g., "Work", "Personal", "Spam").
    • Add descriptions for each label as necessary.
  4. Enable the When no clear match option to handle uncategorized emails. Set it to output to an "Other" branch.
  5. Add a System Prompt to guide the classification (e.g., "Classify the email content into the provided categories.").

Integrating the AI Model for Email Labeling

This step enhances the classification process by connecting the AI model to the Email Content Classifier.

steps

  1. Attach an AI model (e.g., OpenAI Chat Model) to the Email Content Classifier node.
    • Use your model's API key during configuration.
    • Disable the Use Responses API option for easier output parsing.
  2. Test the node to verify classification results, ensuring the AI outputs meaningful labels.

Applying Labels to Classified Emails

Once emails are classified, assign Gmail labels to organize them effectively.

steps

  1. Add a Gmail node to the output of the Email Content Classifier.
  2. Select the action Add label to message.
  3. Use an expression in the Message ID field to reference the incoming email from the Gmail Trigger node.
  4. Specify the label(s) to apply for each respective classification.
  5. Test this step by sending a sample email and verifying the applied labels in Gmail.

Moving Emails Out of the Inbox

Tidying your inbox involves removing labeled emails from the "Inbox" category.

steps

  1. Add another Gmail node configured with the action Remove label from message.
  2. Set the Label Name (or ID) field to "Inbox".
  3. Attach this node to each branch after applying labels.
  4. Execute the workflow with a sample email and verify that labeled emails are moved out of the inbox.

Adding Drafting Capabilities to the AI Agent

With drafting enabled, the AI agent can create email replies in your preferred tone.

steps

  1. Add an AI Agent node connected to a Gmail tool for the "Draft Reply" feature.
  2. Configure a prompt under the AI Agent node specifying how the draft reply should be written.
    • Use expressions to tailor the message content based on the email context.
  3. Set the AI to read insights from previous email threads you sent by limiting the Gmail tool’s inspiration source.
  4. Attach another Gmail node set to Create a draft, with expressions to assign the correct subject line and email thread.
  5. Test the AI drafting capability by creating and verifying a draft email in your Gmail account.

Final Workflow Execution and Verification

Once the entire workflow is built, verify its functionality by testing each step.

FAQ

How does the n8n AI Agent node help with email management?

The n8n AI Agent node facilitates automated email labeling and drafting. It works with AI models to understand email content, categorize messages, and generate personalized email drafts.

What are some considerations before using the n8n AI Agent node?

You should choose an appropriate AI model, stay aware of token limits, use secure API keys, and carefully craft prompts to avoid issues like hallucinations or prompt injection.

Can the AI Agent automatically send replies?

While technically feasible, it’s recommended only to draft replies. Fully automating email responses could lead to unexpected loops or security vulnerabilities.


Official reference: n8n documentation.