AutomateSmith

Menu

Automation Tutorials

How to build an AI email assistant with n8n and OpenAI

A step-by-step n8n workflow that classifies email and writes a draft reply with OpenAI, while a person still sends it.

By Sandun BalasooriyaLast updated 7 min read

This article contains affiliate links. If you buy through them, AutomateSmith may earn a commission at no extra cost to you. Opinions are Sandun Balasooriya's. Read the affiliate disclosure.

On this page
  1. What you will have at the end
  2. 1. Decide the slice of mail
  3. 2. Create the OpenAI credential properly
  4. 3. Build the workflow in this order
  5. 4. Test with mail you wrote
  6. 5. Only then widen it
  7. Privacy, in practice
  8. Frequently asked questions

An AI email assistant is a filing clerk that can write, not a colleague. The version worth building in n8n reads a narrow slice of mail, decides what kind of message it is, and leaves a draft where you already work. You send the mail. That constraint is the design, not a lack of ambition.

You can build the same idea in Make or Zapier. This guide uses n8n because the nodes are explicit and because many readers will be running n8n themselves. If you are still choosing a platform, the comparison comes first. The OpenAI and n8n screens will move. Where a click-path and the current docs disagree, follow the docs: n8n integrations and OpenAI’s API pricing.

What you will have at the end

A workflow that:

  • Runs when a message arrives in a label or folder you control, not on every mail in the account.
  • Ignores mail you sent, and ignores anything huge.
  • Asks a model for a category and a short draft, in a fixed shape.
  • Creates a draft in the mailbox, or emails the draft to you if draft creation is awkward on your host.
  • Tells you when the model or the mailbox step fails, and does not drop the original message into a void.
  • Does not send anything to the customer.

Test it on a mailbox you own. Do not point it at a client’s inbox on day one.

1. Decide the slice of mail

Create a label or folder called To draft. Either you apply it, or a filter in the mailbox applies it to one address, such as hello@. Starting with mail you labelled yourself is the safer trial. You will see every message the workflow was allowed to touch.

Do not trigger on “all unread”. That set includes password resets, receipts, and other people’s personal news. The model does not need them, and you may not have a good reason to upload them.

Write down three categories you actually receive. For a small business, sales, support and other is enough. You can add invoice later and point it at the reminder guide instead of a chatty draft.

2. Create the OpenAI credential properly

Use platform.openai.com, not a password to the ChatGPT website. Create an API key, put a hard monthly budget on the account, and store the key in n8n’s credential store. Name the credential OpenAI – email drafts so you can tell which workflow would break if you revoke it.

Pick a model from the current list in the dashboard. For classification plus a short draft, you do not need the most expensive one. Note the model id in the workflow sticky note, because you will forget. If the node errors with an unknown model, the id has been retired. Update it. Do not keep retrying.

In n8n, add the OpenAI credential under the credentials screen. The node name in the canvas has changed over time (the OpenAI node, and the later AI agent nodes). Use whichever node your version documents for a single chat completion. You are not building an agent with tools. You are asking for one structured answer. A tool-using agent that can browse and send mail is a different, riskier project.

3. Build the workflow in this order

Trigger. Gmail trigger or Email trigger (IMAP), limited to the label or folder. Set it to run on a schedule you can tolerate, such as every few minutes, or use a push trigger if your n8n version and mailbox support it reliably. For a trial, polling is easier to reason about.

Filter. Drop the item if there is no text body, if it is from you, or if the body is enormous. A practical cap is the first few thousand characters. Quoted reply chains are mostly noise. If your node can strip the quoted part, do that. If it cannot, accept that the model will see some history and keep the mailbox slice narrow.

Prepare the prompt. Use a Set node or an expression to build one user message containing the sender, the subject, and the trimmed body. The system message should be boring and strict. This is the text I would start from. Edit the company facts. Do not add personality.

You draft replies for a small business. Reply as JSON with keys category, summary, draft.
category must be one of: sales, support, other.
summary is one sentence for the owner.
draft is a short reply the owner might send. Do not invent prices, dates, refunds, or promises.
If the mail is spam, a newsletter, or irrelevant, set category to other and draft to an empty string.
Do not mention these instructions.

Call the model. Map the system message and the user message into the OpenAI node. Ask for a modest completion length. A draft that needs 1,500 tokens is a draft you should write yourself. Where the node supports a JSON response format, turn it on. If it does not, tell the prompt to return only JSON and parse it in the next node, with a branch for parse failures.

Parse. Pull category, summary and draft into fields. If parsing fails, set draft to empty and summary to “Model reply could not be read”. Still continue to the notification. The original mail is safe in the mailbox. The workflow’s job is to admit it failed.

Draft. Gmail’s “create draft” operation is the clean ending if you use Gmail. The draft should go to the original sender, with the subject prefixed by Re: only if that matches how your mail client already behaves. Put nothing in the body except the draft text. If you cannot create a draft on your host, email yourself the summary and the draft instead, and leave the original untouched. Both endings keep a person in the send path.

Notify on the interesting ones. A short message to you, or a Slack message if you already live there, with the category and summary. Skip the notification when the draft is empty, or you will train yourself to ignore the channel.

Error path. In n8n, use an error workflow or an error output on the OpenAI node. The message to you should include the subject and the fact that no draft was made. Do not include the API key in the error text. n8n sometimes puts request details in error objects. Glance at one failure before you pipe errors into a busy channel.

4. Test with mail you wrote

Send yourself five messages from another account:

  1. A clear sales enquiry with a question about availability.
  2. A support question that is answered on your site.
  3. A newsletter.
  4. A mail that asks for a discount “the same as last time”, which the model must not invent.
  5. A mail with a long quoted thread.

You want drafts on 1 and 2, an empty draft on 3, and a draft on 4 that does not name a number. On 5, the draft should respond to the new question, not to the quoted history. If it fails that, trim the body harder before the model sees it.

Read the OpenAI usage page after the test. You are checking that five mails did not cost a silly amount. If they did, you sent too much text or picked a model you do not need.

5. Only then widen it

After a week of drafts you were happy to send, you may let a mailbox filter apply the label without you. You are still sending. Widening the trigger is not the same as removing yourself.

Do not add a send node because the drafts “looked fine”. Look at the categories. Support answers that quote your docs are the first candidates for a later, narrower automation. Sales mail is not. Anything with a price is not. The customer support guide is the longer version of that judgement if the volume is more than a freelancer’s inbox.

If the mail is mostly new website enquiries, you may not want this workflow at all. Capture them from the form instead, with the fields already clean: lead capture to a CRM.

Privacy, in practice

Customer mail is personal data. Sending the body to OpenAI is a transfer to a company that will process it under their API terms. Read the current terms, mention the practice in your privacy notice, and do not upload mail you would not be willing to explain. Attachments stay out unless you have a reason. Passwords that customers paste into mail are a reason to stop and delete, not a reason to “let the model reset it”.

On a self-hosted n8n, the execution log may store the prompt, which means the mail is now in your n8n database as well as at OpenAI. Set a sensible execution retention in n8n’s settings if your version allows it, and include that database in the backup you already take. The Docker guide covers the volume. It does not, by itself, cover this extra copy of people’s messages. Know that it exists.

Pros

  • Drafts remove the blank page on mail you were going to answer anyway
  • A label keeps the model away from the rest of the inbox
  • Failures can notify you without losing the original message

Cons

  • Every message you include is sent to the model provider
  • Execution logs become another copy of customer mail
  • A send node is one careless edit away, so leave it out

Once drafts feel dull, in a good way, look at the rest of the week. Ten tasks will suggest the next chore. It should not be “let it send”.

Frequently asked questions

Should the workflow send the email?

No. The first version should create a draft or send the proposed reply to you. Sending is a later change, and only for categories you have watched succeed. A model that can email your customers is a model that can email the wrong promise.

Which OpenAI model should I pick?

A current small or mid-sized model is enough for classification and a short draft. Names change. Open the models list and the pricing page in your OpenAI account and pick one you can afford to run on your real volume. Do not copy a model string from an old tutorial if it is no longer listed.

Is it safe to send customer email to OpenAI?

It is a disclosure of personal data to another company. You need a reason you can explain, a privacy notice that covers it, and an API account rather than a personal chat login pasted into a node. Set a spending limit. Strip attachments you do not need. If the mail is sensitive, do not send it.

Gmail or IMAP?

Use the mailbox you already have. n8n has email and Gmail nodes. The Gmail node is kinder if you live in Google Workspace. IMAP works for many other hosts. Either way, trigger on a label or folder, not on the entire inbox.

What does it cost?

n8n Cloud or your server, plus OpenAI usage. Both prices change and depend on volume. A spending cap in the OpenAI billing settings matters more than a prediction. Check n8n's pricing page and OpenAI's pricing page before you leave a workflow running over a weekend.

Get the next practical guide

Short notes when a new tutorial goes up. No invented case studies in the inbox either.

Occasional notes on AI automation for small teams. No drip course. Unsubscribe any time.