AI Powered Email Assistant: Automate Your Inbox with n8n & OpenAI

Automate your Gmail inbox with n8n and OpenAI. Build an AI-powered email assistant that summarizes, drafts replies, and sends Telegram notifications.

Table of Contents

Overview

In this tutorial, you will learn how to build an AI-powered email assistant using n8n and OpenAI. This workflow will automatically scan your Gmail inbox, summarize incoming emails, determine if a reply is needed, draft polite replies in HTML format, save them as drafts, and then send you a Telegram notification to approve or disapprove the reply. Once approved, the reply is sent automatically.

This hands-on guide requires no coding experience and leverages n8n’s powerful nodes and OpenAI’s language model to automate your email management efficiently. If you are new to integrating AI with n8n, consider reviewing the AI Automation with n8n and OpenAI lesson for foundational concepts.


Prerequisites

  • An n8n instance (cloud or self-hosted)
  • Gmail account with API access configured in n8n
  • OpenAI API key set up as credentials in n8n
  • Telegram bot and chat ID configured for sending messages and receiving responses

If you haven’t set up OpenAI credentials in n8n yet, refer to the n8n OpenAI credentials documentation before proceeding. For more details on n8n nodes and their configurations, the n8n documentation is an excellent resource.


Step 1: Set Up the Trigger to Run the Workflow

You want your email assistant to check your inbox regularly. For this tutorial, we’ll start with a scheduled trigger and later mention how to switch to a real-time Gmail trigger.

  1. Add a Schedule Trigger node to your workflow.
  2. Configure it to run daily at 8:00 AM:
    • Set Mode to Every Day.
    • Set Hour to 8.
    • Set Minute to 0.
  3. For testing purposes, also add a Manual Trigger node to run the workflow on demand.

Tip: Once you confirm the workflow works as expected, replace the schedule trigger with a Gmail Trigger node that fires whenever a new email arrives, allowing near real-time processing.


Step 2: Fetch Unread Emails from Gmail

Next, you’ll fetch unread emails from your inbox to process them one by one.

  1. Add a Gmail node:
    • Set Resource to Message.
    • Set Operation to Get Many.
    • Set Limit to 1 (for testing; increase later as needed).
    • Set Filters:
      • Read Status: Unread
      • Label Names: Inbox
  2. Disable the Simplify option to get the full email response including the complete body and headers.
  3. Rename this node to Fetch Emails.

When you execute this node, it should return the full content of the latest unread email.


Step 3: Use OpenAI to Summarize and Draft Reply

Now, use the OpenAI node to analyze the email, summarize it, decide if a reply is needed, and draft a polite HTML reply if required.

Configure the OpenAI Node

  1. Add an OpenAI node.
  2. Set Resource to Text and Operation to Chat Completion.
  3. Select your OpenAI credentials.
  4. Choose the model gpt-4o-mini (or any preferred GPT-4 variant).
  5. Set Response Format to JSON to easily parse the AI’s structured response.

Define the System Prompt

Add a system message to instruct the AI on its role:

You are a helpful email assistant. Your job is to read incoming emails, summarize them in one line, and decide whether they need a reply or not. If a reply is needed, draft a polite response based on the email content. Always draft the response in HTML format so it is ready to be sent.

Define the User Prompt with Input Email

Add a user message that includes the actual email body from the Gmail node and specifies the expected JSON output format:

Your task is to summarize the email given below in one line. Tell me if a reply is needed or not. Please respond with a clear "yes" or "no" only. If "yes", suggest a short and polite reply I can send.

Please respond in the following JSON format:

{
  "summary": "One sentence summary of the email",
  "reply_needed": "yes or no",
  "suggested_reply": "If reply is needed, draft the reply here in HTML"
}

Email:
{{ $json["text"] }}

The expression {{ $json["text"] }} dynamically inserts the email body fetched in the previous step.

  1. Rename this node to Summarize Emails and Draft Replies.

For tips on formatting data in your workflows, see the Set Node in n8n lesson, which can help you prepare data before sending it to OpenAI.


Step 4: Check if a Reply is Needed

Add an If node to branch the workflow based on whether the AI determined a reply is required.

  1. Add an If node named Is Reply Needed?.
  2. Set the condition to check if the output field reply_needed equals yes (case insensitive).
    • Expression: {{$json["reply_needed"].toLowerCase()}}
    • Condition: Equals yes
    • Enable Ignore Case to handle case variations.

Step 5: Create a Draft Reply in Gmail

If a reply is needed, create a draft email in Gmail with the AI’s suggested reply.

  1. Add a Gmail node.

  2. Set Resource to Draft.

  3. Set Operation to Create.

  4. Configure the draft:

    • To Email: Use the original sender’s email from the fetch emails node (e.g., {{$node["Fetch Emails"].json["from"]["email"]}}).
    • Subject: Prefix the original email subject with Re: (e.g., Re: {{$node["Fetch Emails"].json["subject"]}}).
    • Body: Use the AI’s suggested reply from the OpenAI node ({{$node["Summarize Emails and Draft Replies"].json["suggested_reply"]}}).
    • Email Type: Select HTML to preserve formatting.
    • Thread ID: Use the original email thread ID to keep the reply in the same conversation ({{$node["Fetch Emails"].json["threadId"]}}).
  5. Rename this node to Create Draft.


Step 6: Notify User on Telegram for Approval

Send a Telegram message to notify you about the new email summary and draft reply, asking for approval or disapproval.

  1. Add a Telegram node.
  2. Set Operation to Send Message.
  3. Select your Telegram credentials.
  4. Enter your Chat ID (the ID of your chat with the bot).
  5. Set the message text using an expression template:
New email alert:

Summary: {{$node["Summarize Emails and Draft Replies"].json["summary"]}}

Reply needed: Yes

Suggested reply: {{$node["Summarize Emails and Draft Replies"].json["suggested_reply"]}}

Review and edit the draft reply in your Gmail, or approve/disapprove below.
  1. Enable Send and Wait for Response.
  2. Set Response Type to Approval.
  3. Add options: Approve and Disapprove.

Rename this node to Request Approval.

This step implements a human-in-the-loop approval process, letting you control whether the AI-generated reply gets sent.


Step 7: Handle Approval Response

Add an If node to check if the user approved the reply.

  1. Add an If node named Is Approved?.
  2. Set the condition to check if the Telegram response contains an approval ({{ $json["approved"] === true }}).

Step 8: Send the Approved Draft Email

Since n8n’s Gmail node does not support sending drafts directly, you will use the HTTP Request node to call Gmail’s API to send the draft.

Fetch the Draft ID

  1. Add a Gmail node to get the draft:
    • Resource: Draft
    • Operation: Get
    • Draft ID: Use the ID from the Create Draft node ({{$node["Create Draft"].json["id"]}}).

Rename this node to Get Draft.

Send the Draft via Gmail API

  1. Add an HTTP Request node:

    • Method: POST
    • URL: https://gmail.googleapis.com/gmail/v1/users/me/drafts/send
    • Authentication: Use the same Gmail OAuth2 credentials.
    • Body Content Type: JSON
    • Body Parameters:
      {
        "id": "{{$node["Get Draft"].json["id"]}}"
      }
      
  2. Rename this node to Send Draft.

  3. Execute the HTTP request node to send the draft email.

For more details on using the Gmail API with n8n, consult the n8n Gmail Node Documentation.

Confirm the Draft is Sent

After sending, the draft should disappear from your Gmail drafts folder, and the recipient will receive your reply.


Step 9: Handle the Disapproval or No Reply Needed

For cases where:

  • The AI decides no reply is needed, or
  • You disapprove the suggested reply,

add a simple No Operation node or leave the workflow branch empty to skip any email sending.


Optional: Replace Schedule Trigger with Gmail Trigger

Once your workflow works reliably, replace the Schedule Trigger with a Gmail Trigger node to process emails as they arrive:

  1. Add a Gmail Trigger node.
  2. Configure it to watch your inbox for new unread emails.
  3. Remove the Schedule Trigger and Manual Trigger nodes.

This change allows your email assistant to operate in real-time.


Common Mistakes and Troubleshooting

  • Simplify Option in Gmail Node: If you only receive email snippets, disable the Simplify option to get the full email body.
  • Incorrect Gmail Credentials: Ensure all Gmail nodes use the same OAuth2 credentials to avoid authorization errors.
  • Telegram Chat ID: Make sure the chat ID corresponds to your bot conversation; otherwise, messages won’t be delivered.
  • OpenAI Prompt Formatting: Clearly specify output format in the prompts; ambiguous instructions lead to unpredictable AI responses.
  • Approval Buttons Missing: Enable Send and Wait for Response and set Response Type to Approval in the Telegram node to get approval buttons.
  • Sending Drafts: n8n currently lacks a direct send draft operation in the Gmail node; use the HTTP node with Gmail API as a workaround.

For additional guidance on working with the OpenAI node, see the OpenAI API documentation.


Quick Reference Cheat Sheet

Step Node Type Key Settings / Expressions
Trigger email processing Schedule Trigger or Gmail Trigger Schedule daily or on new email arrival
Fetch unread emails Gmail Resource: Message, Operation: Get Many, Filters: Unread, Inbox
Summarize & draft reply OpenAI Model: GPT-4o-mini, System prompt + User prompt with JSON output
Check if reply needed If Condition: reply_needed == "yes" (ignore case)
Create draft reply Gmail Resource: Draft, Operation: Create, To, Subject, Body (HTML), Thread ID
Notify user for approval Telegram Send message with buttons, enable Send and Wait for Response
Handle approval response If Condition: approved == true
Get draft to send Gmail Resource: Draft, Operation: Get, Draft ID from create draft node
Send draft via API HTTP Request POST to gmail/v1/users/me/drafts/send, Body: { "id": draftId }
No reply or disapproval No Operation Skip sending email

Additional Resources


By following this tutorial, you will have a fully automated AI-powered email assistant that saves time and streamlines your inbox management using n8n and OpenAI, with human-in-the-loop approval via Telegram. As shown in the video above, this practical workflow demonstrates how AI and automation can be combined without writing any code. For a related project that focuses specifically on automating email replies with AI, check out the AI Email Assistant with n8n lesson.

Frequently Asked Questions

Set the Gmail node's Resource to 'Message', Operation to 'Get Many', limit the number of emails, and filter by 'Read Status' as 'Unread' and 'Label Names' as 'Inbox'.

The OpenAI node summarizes incoming emails, decides if a reply is needed, and drafts polite HTML replies based on the email content.

Integrate a Telegram node to send notifications with the drafted reply, allowing you to approve or disapprove the reply through Telegram responses before automatic sending.

Yes, after testing with a Schedule Trigger, replace it with a Gmail Trigger node to process emails in near real-time as they arrive.

You need an n8n instance, a Gmail account with API access configured in n8n, OpenAI API credentials set up, and a Telegram bot with chat ID for notifications.

Dheeraj Sharma

Dheeraj Sharma

AI Systems Builder
Creator of the n8n Zero to Hero course (42 lessons, 31+ hours). I help solopreneurs build AI systems that grow revenue without growing workload.

Get the n8n Mastery Bundle

All workflows, cheat sheets, and premium resources from the entire course in one package.

Get Premium Resources