Build an Automated Email Drafting AI Agent with n8n and OpenAI | Alpha | PandaiTech

Build an Automated Email Drafting AI Agent with n8n and OpenAI

A complete guide to setting up an n8n workflow: connect a Gmail trigger, write system prompts for OpenAI to analyze emails, and automatically create draft replies in Gmail.

Learning Timeline
Key Insights

Difference Between API Keys & ChatGPT Subscriptions

Paying for ChatGPT Plus (USD20/month) does not provide an API Key. API Keys must be obtained separately via the OpenAI Platform portal using a 'pay-as-you-go' system.

Use Draft Mode for Safety

Always use the 'Create Draft' function instead of 'Send Message' during the initial phases. This allows you to review the AI's response quality (human-in-the-loop) before the email is actually sent to the customer.

GPT-4o-mini Model

For simple tasks like replying to emails, the 'gpt-4o-mini' model is the best choice as it is extremely fast and much cheaper than the larger GPT-4 models.
Prompts

System Prompt for Email Agent

Target: OpenAI (System Message)
You are a helpful email response agent. If the sender's message seems urgent, draft the response via Gmail asking for more details or offering assistance. Ensure the response is clear, professional, and prompt.

User Prompt Template

Target: OpenAI (User Message)
Here's the information of the email that needs to be triaged: From: {{ $json.from }} Subject: {{ $json.subject }} Body: {{ $json.snippet }}
Step by Step

Getting Started & Workflow Setup

  1. Click the '+' icon at the top of the screen and select 'Workflow'.
  2. Choose the project you want to use (e.g., 'Personal Project').
  3. Click the '+' button in the middle of the canvas to add your first node.
  4. Search for 'Gmail' in the search bar and select the node.
  5. Select 'On Message Received' as the main Trigger.
  6. Click the pencil icon to connect your Gmail account via 'Sign in with Google'.
  7. Grant the necessary permissions required by Google for n8n access.
  8. Ensure the connection status shows a green check mark.

Configuring the OpenAI Node for Email Analysis

  1. Click the '+' button after the Gmail node and search for 'OpenAI'.
  2. Choose the 'Message a Model' action from the options list.
  3. Click the '+' button under 'Credential for OpenAI API' to enter your API Key.
  4. Select the 'gpt-4o-mini' model (or any other model available in your account).
  5. Change the message type to 'Assistant' to set the System Prompt.
  6. Enter your system instructions in the prompt field.
  7. Change the 'Message' input type from 'Fixed' to 'Expression' to enable dynamic data.
  8. Toggle the data view to 'Schema' format to simplify the drag-and-drop process.
  9. Drag the 'From' (Sender Email), 'Subject' (Subject), and 'Snippet' (Content) data from the Gmail output into the OpenAI User Prompt field.

Automatically Generating Reply Drafts

  1. Click the '+' button after the OpenAI node and select 'Gmail' again.
  2. Set Resource to 'Draft' and Operation to 'Create'.
  3. Use the same Gmail account you connected earlier.
  4. Change the 'Message' setting to 'Expression'.
  5. Drag the 'Content' output from the OpenAI node into the Gmail 'Message' field.
  6. Click 'Add Option' and select 'Thread ID'.
  7. Drag the 'Thread ID' from the original Gmail Trigger into the 'Thread ID' field to keep the reply within the same thread.
  8. Drag the 'Subject' from the original Gmail Trigger into the Gmail Draft subject field.
  9. Click 'Test Step' to test the workflow and check your Gmail Drafts folder.
  10. Activate the workflow by switching the 'Active' toggle in the top right.

More from Build & Deploy Autonomous AI Agents

View All