Automated Gmail Reply Drafting using OpenAI Assistants - n8n Workflow

Generate professional Gmail reply drafts automatically using an OpenAI Assistant and an n8n workflow. Triggers on specific labels, inserts the draft into the correct thread, and removes the label.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Users handling large volumes of support or sales emails who need high-quality draft responses.
Automation enthusiasts looking for advanced n8n workflow examples integrating the Gmail API.


  • Teams utilizing OpenAI Assistants for standardized communication that requires human review before sending.

Overview

Handling high-volume or complex email inquiries often requires timely, yet carefully worded responses. This n8n workflow solves the tedious task of initiating replies. By leveraging the power of an OpenAI Assistant, the n8n workflow automatically analyzes incoming email content tagged with a specific label (e.g., "AI_DRAFT") and generates a professionally worded draft response. Once generated, the draft is seamlessly inserted into the corresponding Gmail thread, awaiting human review and final send-off. This powerful n8n template ensures that your human team only focuses on editing and approving, drastically cutting down drafting time and enhancing communication consistency. Every part of the n8n workflow is optimized for reliable execution.

How it Works

This powerful n8n workflow starts with a fixed schedule and ends with a cleanup operation, ensuring efficiency and preventing duplicates:


  1. Scheduled Polling: The central n8n trigger, configured as a Schedule trigger, runs every minute, initiating the check for new emails that need attention.

  2. Targeted Retrieval: The Gmail n8n node searches for email threads that contain the specific, predefined trigger label (e.g., those marked for AI processing).

  3. Looping and Extraction: The n8n workflow iterates through each found thread. It uses another Gmail n8n node to extract the content, subject, and sender information of the core message.

  4. AI Generation: The extracted text is passed to the specialized OpenAI Assistant n8n node, which executes its instructions to generate a structured reply, typically in Markdown format.

  5. Data Preparation and Formatting: A series of Set and Markdown n8n nodes collect the AI output, recipient address, and original subject. The Markdown content is then converted to HTML for professional email delivery.

  6. Email Raw Construction & Encoding: A Code n8n node executes custom JavaScript to construct the raw email message body, headers, and metadata, conforming to strict RFC standards. This raw data is then base64 encoded, a crucial technical step required by the Gmail API.

  7. Draft Insertion: An HTTP Request n8n node communicates directly with the Gmail API, securely inserting the base64 encoded message as a new draft within the original thread using the stored thread ID.

  8. Cleanup: Finally, the Gmail n8n node labeled "Remove AI label from email" removes the triggering label from the thread. This critical cleanup step prevents the n8n workflow from processing the same message during subsequent runs of the n8n trigger.

Installation Guide

To deploy this automated Gmail and OpenAI n8n workflow, follow these steps:


  1. Import the n8n template: Copy the provided workflow JSON code and import it directly into your n8n instance using the 'New -> Import from JSON' feature.

  2. OpenAI Assistant Setup: Ensure you have configured a valid OpenAI credential. In the "Ask OpenAI Assistant" n8n node, select your credential and specify the correct Assistant ID that holds your reply generation logic.

  3. Gmail Credentials: Provide OAuth2 credentials for the Gmail n8n node connections. This connection must have the necessary permissions (scopes) to read emails, manage labels, and create drafts in your mailbox.

  4. Configure Labels: In the "Get threads with specific labels" Gmail n8n node, specify the Label IDs that the n8n trigger should monitor. This label is what designates an email for AI processing.

  5. Activate: Once credentials and configurations are set for every n8n node, set the n8n workflow status to active. The n8n trigger will commence its scheduled checks immediately.

Node Details

Schedule trigger (n8n trigger): The primary n8n trigger for the workflow, set to run every 1 minute, initiating the periodic check for new emails.
Get threads with specific labels (Gmail n8n node): Filters your Gmail inbox efficiently to return only threads that have been marked with the designated AI processing label.
Ask OpenAI Assistant (OpenAI n8n node): The core AI component. It sends the email content to a pre-trained OpenAI Assistant for intelligent and contextual reply generation, returning the output as structured text.
Convert response to HTML (Markdown n8n node): Converts the structured text output (Markdown) from the OpenAI n8n node into reliable HTML format suitable for email body content.
Convert raw to base64 (Code n8n node): A custom scripting n8n node that executes JavaScript to base64 encode the fully constructed raw email string, a technical requirement for interacting with the Gmail API's drafts endpoint.
Add email draft to thread (HTTP Request n8n node): This n8n node uses the specified Gmail OAuth2 credentials to make a POST request to the Gmail API, inserting the encoded AI-generated reply as a draft into the originating email thread.


  • Remove AI label from email (Gmail n8n node): The final action in this n8n workflow. It uses the Gmail n8n node to remove the initial processing label, signaling that the thread has been handled and preventing reprocessing by the next n8n trigger run.

Related n8n Workflows

Free

Nodes: 10 Nodes
Updated: December 26 2025
View all
Created by

Water Engineer, learning to integrate AI, ML, DL, NNA into Water Engineering for LMI Countries.

Featured*