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.
- Add a
Schedule Triggernode to your workflow. - Configure it to run daily at 8:00 AM:
- Set Mode to
Every Day. - Set Hour to
8. - Set Minute to
0.
- Set Mode to
- For testing purposes, also add a
Manual Triggernode to run the workflow on demand.
Tip: Once you confirm the workflow works as expected, replace the schedule trigger with a
Gmail Triggernode 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.
- Add a
Gmailnode:- 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
- Read Status:
- Set Resource to
- Disable the Simplify option to get the full email response including the complete body and headers.
- 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
- Add an
OpenAInode. - Set Resource to
Textand Operation toChat Completion. - Select your OpenAI credentials.
- Choose the model
gpt-4o-mini(or any preferred GPT-4 variant). - Set Response Format to
JSONto 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.
- 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.
- Add an
Ifnode namedIs Reply Needed?. - Set the condition to check if the output field
reply_neededequalsyes(case insensitive).- Expression:
{{$json["reply_needed"].toLowerCase()}} - Condition: Equals
yes - Enable Ignore Case to handle case variations.
- Expression:
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.
Add a
Gmailnode.Set Resource to
Draft.Set Operation to
Create.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
HTMLto preserve formatting. - Thread ID: Use the original email thread ID to keep the reply in the same conversation (
{{$node["Fetch Emails"].json["threadId"]}}).
- To Email: Use the original sender’s email from the fetch emails node (e.g.,
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.
- Add a
Telegramnode. - Set Operation to
Send Message. - Select your Telegram credentials.
- Enter your Chat ID (the ID of your chat with the bot).
- 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.
- Enable Send and Wait for Response.
- Set Response Type to
Approval. - Add options:
ApproveandDisapprove.
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.
- Add an
Ifnode namedIs Approved?. - 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
- Add a
Gmailnode to get the draft:- Resource:
Draft - Operation:
Get - Draft ID: Use the ID from the
Create Draftnode ({{$node["Create Draft"].json["id"]}}).
- Resource:
Rename this node to Get Draft.
Send the Draft via Gmail API
Add an
HTTP Requestnode:- 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"]}}" }
- Method:
Rename this node to
Send Draft.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:
- Add a
Gmail Triggernode. - Configure it to watch your inbox for new unread emails.
- Remove the
Schedule TriggerandManual Triggernodes.
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
Simplifyoption 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 Responseand setResponse TypetoApprovalin 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.