How to give an n8n AI agent its own email inbox
Published · Updated
To trigger an n8n workflow from incoming email, give the AI agent its own Mail for Bots inbox, register your n8n Webhook node URL as a webhook for the message.received event, and call the REST API from HTTP Request nodes to read, reply or draft. There is no dedicated n8n node: you use n8n's generic Webhook and HTTP Request nodes, which is all this needs.
If you only need a quick start, start free; the Free plan includes 3 inboxes.
What you need before building the workflow
- A Mail for Bots account and an API key. Create the key in the webmail under Settings, API keys & MCP; it starts with
bm_and is shown once. See API keys and scopes to limit what the workflow can do. - An n8n instance whose webhook URL is public over HTTPS. Mail for Bots only accepts
https://webhook URLs that resolve to a public address, so a locallocalhostn8n will not work without a tunnel. - A Header Auth credential in n8n holding
Authorization: Bearer bm_YOUR_KEY, reused by every HTTP Request node.
Step 1: create an inbox for the agent
Run this once from an HTTP Request node (or curl). It needs the inboxes:manage scope; username and display_name are optional.
{
"method": "POST",
"url": "https://mailforbots.com/api/v1/inboxes",
"authentication": "genericCredentialType",
"genericAuthType": "httpHeaderAuth",
"sendBody": true,
"specifyBody": "json",
"jsonBody": {
"username": "n8n-agent",
"display_name": "n8n agent"
}
}The response contains the new address. Use it, or its id, wherever the next steps say INBOX.
Step 2: use a Webhook node as the email trigger
Add a Webhook node with method POST and respond immediately (any 2xx counts as delivered, and Mail for Bots waits 10 seconds). Copy its production URL, then register it. Creating webhooks needs the webhooks:manage scope and a key that is not restricted to some inboxes.
curl -X POST "https://mailforbots.com/api/v1/webhooks" \
-H "Authorization: Bearer $BOTMAIL_KEY" \
-H "Content-Type: application/json" \
-d '{ "url": "https://n8n.example.com/webhook/mailforbots", "events": ["message.received"], "inbox_id": "agent@mailforbots.com" }'The response includes the signing secret (whsec_...) once, so store it. Each delivery is a JSON body { id, type, created_at, data }. For message.received, data.inbox holds { id, address } and data.message is a summary with id, thread_id, from_address, subject and snippet, but not the body. Delivery is at least once, so deduplicate on the event id. The webhooks docs list every event, header and retry rule.
Step 3: verify the signature
Each request carries a botmail-signature header of the form t=<unix seconds>,v1=<hex HMAC>. v1 is the hex HMAC-SHA256, keyed with your webhook secret, of <t>.<raw request body>, computed over the exact bytes received. In n8n, enable the Webhook node's Raw Body option so you can hash the unparsed body. A Code node can then compute the HMAC with Node's crypto, which self-hosted n8n only exposes if you allow it through NODE_FUNCTION_ALLOW_BUILTIN=crypto (if your instance uses task runners, set it on the task runner instead). Compare in constant time and reject timestamps more than 5 minutes from now.
If that setup is awkward, a simpler defence is to treat the webhook only as a nudge: the next node fetches the message by id with your API key, so a forged call can only point at mail that really exists in your inbox.
Step 4: read the message, then let the AI agent decide
{
"method": "GET",
"url": "https://mailforbots.com/api/v1/messages/{{ $json.body.data.message.id }}",
"authentication": "genericCredentialType",
"genericAuthType": "httpHeaderAuth"
}The response has the full body plus extracted codes and links, and marks the message read. Pass the text to an n8n AI Agent node. Treat that text as untrusted input, since anyone can email your agent; read how to read email safely when agents are exposed to prompt injection.
Step 5: reply, or draft for a human to approve
For low-risk mail, reply in the same thread (needs mail:send). For anything that matters, create a draft instead (needs drafts:write). A person reviews and sends it from the webmail, and a draft.created webhook event can notify you. The pattern is covered in human approval for AI agent email.
{
"method": "POST",
"url": "https://mailforbots.com/api/v1/messages/{{ $('Webhook').item.json.body.data.message.id }}/reply",
"authentication": "genericCredentialType",
"genericAuthType": "httpHeaderAuth",
"sendBody": true,
"specifyBody": "json",
"jsonBody": {
"text": "{{ $json.output }}",
"quote": true
}
}{
"method": "POST",
"url": "https://mailforbots.com/api/v1/drafts",
"authentication": "genericCredentialType",
"genericAuthType": "httpHeaderAuth",
"sendBody": true,
"specifyBody": "json",
"jsonBody": {
"inbox": "agent@mailforbots.com",
"mode": "reply",
"original_message_id": "{{ $('Webhook').item.json.body.data.message.id }}",
"text": "{{ $json.output }}"
}
}Which endpoint does each node call?
| Workflow step | n8n node | Call | Scope |
|---|---|---|---|
| Create inbox | HTTP Request | POST /inboxes | inboxes:manage |
| Receive mail | Webhook | message.received event | webhooks:manage to register |
| Read body | HTTP Request | GET /messages/:id | mail:read |
| Reply | HTTP Request | POST /messages/:id/reply | mail:send |
| Draft for approval | HTTP Request | POST /drafts | drafts:write |
Every call is documented in the REST API reference. If you would rather poll than expose a webhook, GET /inboxes/:inbox/wait blocks up to 300 seconds until a matching message arrives. Sending limits depend on your plan, so check pricing before you go to production.
Keep the workflow safe
- Give the key only the scopes the workflow uses, and restrict it to one inbox where possible. Note that webhook management itself needs an unrestricted key, so register the webhook once with a separate key.
- Respond fast and deduplicate on the event
id; failed deliveries are retried up to six attempts, and a webhook is switched off after 20 consecutive failures. - Start with drafts, and move to direct replies only for mail types you trust.
New to the API? The quickstart takes a few minutes. When you are ready, start free.
Frequently asked questions
Is there an official n8n node for Mail for Bots?
No. This setup uses n8n's generic Webhook and HTTP Request nodes with the Mail for Bots REST API and webhooks.
Which event triggers the n8n workflow on new email?
Subscribe the Webhook node URL to `message.received`. The payload is a summary, so fetch the body with `GET /messages/:id`.
How do I stop the agent sending something wrong?
Create a draft with `POST /drafts` instead of replying. A human reviews and sends it from the webmail.