Receive Email in Node-RED: Run a Flow on Every New Email
Start a Node-RED flow on every new email with an http in node. A small helper opens each Mailcycle message and posts the plain from, subject and text to it.

Node-RED is at its best when something happens and a few nodes react: a sensor reading, a message on a broker, a button press. An email can be that something too. A sign-up code arrives and a flow copies it where it is needed. An order confirmation lands and a flow files it. A reply comes in and a flow marks the ticket. The usual route is the email nodes, which poll a mailbox over IMAP and keep a password in your flow.
There is a shorter path. Give the flow an address of its own and let each new email start it. In this tutorial you’ll set that up with a Mailcycle address, a webhook and the Mailcycle decrypt helper, a single small file that you run in your own Cloudflare account. Mail is encrypted the moment it arrives. The helper opens each message with the key for that one address and posts the plain message to an http in node: sender, subject, text, HTML and attachments.
Everything below was run on Node-RED 5.0.10 with real messages on a Mailcycle test account, and the screenshots come from that run. The helper ran locally under Node, and because Mailcycle’s webhook cannot reach a local port, the message.received event was signed and posted by a script using the documented Mailcycle-Signature scheme; every step after it is the real path.
What you’ll build
One flow tab with two halves. The push half starts with an http in node. A function node checks a token and skips repeats, an http response node answers the helper, and a second function node reads the sign-up code out of the message and hands it to a file node and a debug node. The pull half starts with an inject node on a timer, and an http request node asks the helper for a message by id.
This is what msg.payload holds in the push half after one new email. The helper added eventId; the rest is the message:
{
"eventId": "evt_demo0001",
"id": "msg_i3XiHk3Oh3QD",
"inboxId": "ibx_yuhto9gmv0je",
"keyEpoch": 0,
"receivedAt": "2026-11-27T22:56:22.044Z",
"from": "bouncy-lake@birchpier.com",
"fromName": "Acme Sign-up",
"to": [],
"subject": "Your Acme verification code (push)",
"text": "Welcome to Acme.\n\nYour verification code is 685157. It expires in 10 minutes.\n\nIf you did not ask for it, ignore this mail.\n",
"html": "",
"attachments": []
}
The run used the helper before a fix that now also returns the recipient in to; with the current helper, to lists the address the mail was sent to.
The flow turns that into one line in a log file, and shows the code in the node’s status:
2026-11-27T23:03:52.203Z push msg_i3XiHk3Oh3QD bouncy-lake@birchpier.com "Your Acme verification code (push)" code=685157
How the helper and Node-RED fit together
Node-RED can receive an HTTP request and call a URL. What it needs in between is something that opens the encrypted message. The decrypt helper is that piece: one file, worker.js, that runs as a Cloudflare Worker in your account.
The path for one email:
- A sender writes to your Mailcycle address. The message is encrypted on arrival.
- Mailcycle sends a signed
message.receivedevent to the helper’s URL. It carries the message id and the address id. - The helper checks the signature, fetches the message with your API key, opens it with the address key and posts the plain JSON to the
FORWARD_URLyou set. - Your
http innode receives that JSON and the flow runs.
The helper waits for a 2xx answer, so your flow should answer first and work second. Step 4 below does exactly that.
Prerequisites
- An Operator or Scale plan. Webhooks and API keys start on Operator, which is $15 a month. Choose Scale, at $19 a month, when the address is on your own domain. See the pricing page for the full list.
- Node-RED 5.0 on Node.js 22.9 or newer. The run used Node-RED 5.0.10 and Node.js 22.23.4.
- A free Cloudflare account and
wrangler, which deploys the helper. The free Workers plan is enough. - An
https://address for Node-RED that the helper can reach. A reverse proxy, a tunnel or a hosted Node-RED all work, and the Raspberry Pi section below covers a home setup. - An address on your Mailcycle account to receive the mail.
Step 0: Set up the helper once
The setup that every no-code tool shares lives in one place, the decrypt helper guide. Follow it for these four parts, then come back here:
- Export the address key in the web app (https://app.mailcycle.email), under the address’s Manage menu.
- Create an API key on the API page and name it after the job, for example “Node-RED helper”.
- Deploy the helper with
wranglerand storeMAILCYCLE_API_KEYandADDRESS_KEYSas secrets. - Add a Mailcycle webhook for
message.receivedthat points at the helper, and store itswhsec_signing secret asWEBHOOK_SECRET.
The only Node-RED specific value is the last one, FORWARD_URL, which you get in step 5 below. Keep the helper’s https:// URL handy for the webhook.
Step 1: Serve Node-RED over HTTPS
The helper refuses a FORWARD_URL that starts with http://, so Node-RED has to answer on https://. If your Node-RED already sits behind a reverse proxy with a certificate, skip this step. For a direct setup, add an https block to settings.js:
const fs = require('fs');
module.exports = {
uiHost: '127.0.0.1',
uiPort: 1880,
https: { key: fs.readFileSync(process.env.NR_SSL_KEY), cert: fs.readFileSync(process.env.NR_SSL_CERT) },
requireHttps: true,
flowFile: 'flows.json',
};
The run used a self-signed certificate for localhost, created with openssl req -x509, and started the helper with NODE_EXTRA_CA_CERTS pointing at that certificate so that Node trusts it. With a certificate from a real authority, you don’t need that variable. If your Node-RED folder has "type": "module" in a parent package.json, name the settings file settings.cjs so require works.
Node-RED 5.0 shows a welcome tour and an update prompt in a fresh user folder. Both can be switched off in the same file with editorTheme: { tours: false } and telemetry: { updateNotification: false }.
Step 2: Add the http in node
Drag an http in node onto the tab and set:
| Field | Value |
|---|---|
| Method | POST |
| URL | /mailcycle-mail |
The full address of the node is your Node-RED address plus that path, for example https://nodered.example.com/mailcycle-mail. Unlike many catch-hook tools, the http in node gives you the message at the top level: msg.payload.subject, not msg.payload.body.subject. The query string of the request is in msg.req.query, which the next step uses.
Wire a debug node straight to the http in output while you build, set to show msg.payload. In the run it was called Received from helper, and it showed every request that reached the endpoint, including one that was rejected a moment later. That makes it a good first node to look at when a delivery goes missing.
Step 3: Check a token and skip repeats in a function node
Anyone who learns your endpoint’s address could post to it, so give the helper a second secret to carry. Put a long random value in the forward URL as a query parameter, and have the flow check it:
https://nodered.example.com/mailcycle-mail?token=<FORWARD_TOKEN>
The http in node does not check anything itself, so the first function node does. Name it Check token, skip repeats, set Outputs to 2 and add this code:
// The helper already checked Mailcycle-Signature against WEBHOOK_SECRET before it forwarded.
// This node checks the second thing only we know: the token in the forward URL, then skips repeats.
const token = env.get('FORWARD_TOKEN');
if (!token || (msg.req.query || {}).token !== token) {
msg.statusCode = 401;
msg.payload = { error: 'unauthorized' };
return [null, msg];
}
const m = msg.payload || {};
const seen = context.get('seen') || [];
if (seen.includes(m.eventId)) {
msg.statusCode = 200;
msg.payload = { ok: true, repeat: true };
return [null, msg];
}
context.set('seen', [m.eventId, ...seen].slice(0, 200));
msg.source = 'push';
msg.statusCode = 200;
return [msg, null];
Two things in that code are Node-RED idioms worth keeping. A function node with two outputs is a tidy way to split “answer and stop” from “go on”: output 1 continues the flow, output 2 goes straight to a response node. And context.get and context.set give the node its own memory, here a list of the last 200 event ids. Webhooks are retried, and eventId is the same on every try, so a list like this turns a second delivery into a quick 200.
The secret never appears in the flow. env.get('FORWARD_TOKEN') reads it from the process environment, so the exported flow JSON carries no token and is safe to share or keep in a repository.
Step 4: Answer the helper with an http response node
Every http in node needs a matching http response node, or the request hangs until it times out. Add two:
- One named
401 or repeat, wired to output 2 of the function node. It sends back whatevermsg.statusCodeandmsg.payloadthe function set. - One named
200, reached through a small function node calledAnswer the helper:
msg.payload = { ok: true };
msg.statusCode = 200;
return msg;
Wire output 1 of the check node to both Answer the helper and the next step, so that the answer goes back at once and the work continues in parallel. The helper waits for a 2xx before it counts the delivery as done, and a flow that answers before the slow part never makes it retry.
Step 5: Find the code, write it to a file, show it
The sign-up code is in msg.payload.text. Add a function node named Find sign-up code:
// Look for a sign-up code in the message the helper opened.
const m = msg.payload;
const text = String((m && m.text) || '');
const hit = text.match(/(?:code|PIN|OTP)\D{0,20}(\d{4,8})/i) || text.match(/\b(\d{6})\b/);
if (!hit) {
node.warn('No code in ' + (m && m.id));
return null;
}
msg.code = hit[1];
msg.message = m;
msg.payload = [new Date().toISOString(), msg.source, m.id, m.from, JSON.stringify(m.subject), 'code=' + hit[1]].join(' ') + '\n';
msg.filename = env.get('CODES_FILE');
return msg;
Then wire two nodes after it:
- A file node named
Append to codes.log. Set Filename tomsg.filename, the action to append to file, and tick Create directory if it doesn’t exist. Turn off “add newline” because the function already ends the line. - A debug node named
Code, set tomsg.payload. Under its settings, tick to node status and usemsg.code, and the code shows as a small label under the node while you work.
A line like code=685157 is only the example. The same two nodes work for any value you can match in the text: an order number, a tracking link, a one-time password. Swap the regular expression and the rest stays.
Step 6: Point the helper at your flow
Deploy the flow with the Deploy button. Then give the helper the full address of the http in node, token included:
npx wrangler secret put FORWARD_URL
Paste https://nodered.example.com/mailcycle-mail?token=<FORWARD_TOKEN> when asked. A forward URL with a token in it works like a password for the flow, so a Wrangler secret is the right place for it. Set the same token in the environment of the process that runs Node-RED:
export FORWARD_TOKEN="a-long-random-value"
export CODES_FILE="/home/pi/codes.log"
Restart Node-RED so that env.get can see them. If Node-RED runs under systemd or Docker, set the variables in the unit file or the container settings instead.
Step 7: Send a test email and watch the debug pane
Send a message to the address from any mail account. A moment later the http in node has fired. The run for this tutorial added one more check on the helper’s side. A delivery signed with the wrong secret came back as:
-> 401 {"error":"bad_signature"}
and a message.received event for a real message, signed by the run’s script, came back as:
-> 200 {"ok":true}
In the editor, Received from helper showed the message in the debug pane, Code showed 685157 under the node, and codes.log got its line. Then the run posted the same event again, as a webhook retry would. The helper answered 200, the flow answered 200, and codes.log still held one push line, because Check token, skip repeats stopped the repeat. Last, a POST straight to the Node-RED endpoint without the token came back 401.
Import the push half
You can import these nodes instead of building them. This is the push half as Node-RED exports it, with the secrets left out because the function nodes read them from the environment:
[
{
"id": "in1",
"type": "http in",
"name": "",
"url": "/mailcycle-mail",
"method": "post",
"upload": false,
"swaggerDoc": "",
"x": 150,
"y": 100,
"wires": [
[
"chk1"
]
]
},
{
"id": "chk1",
"type": "function",
"name": "Check token, skip repeats",
"func": "// The helper already checked Mailcycle-Signature against WEBHOOK_SECRET before it forwarded.\n// This node checks the second thing only we know: the token in the forward URL, then skips repeats.\nconst token = env.get('FORWARD_TOKEN');\nif (!token || (msg.req.query || {}).token !== token) {\n msg.statusCode = 401;\n msg.payload = { error: 'unauthorized' };\n return [null, msg];\n}\nconst m = msg.payload || {};\nconst seen = context.get('seen') || [];\nif (seen.includes(m.eventId)) {\n msg.statusCode = 200;\n msg.payload = { ok: true, repeat: true };\n return [null, msg];\n}\ncontext.set('seen', [m.eventId, ...seen].slice(0, 200));\nmsg.source = 'push';\nmsg.statusCode = 200;\nreturn [msg, null];",
"outputs": 2,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 400,
"y": 120,
"wires": [
[
"rep1",
"find1"
],
[
"res2"
]
]
},
{
"id": "rep1",
"type": "function",
"name": "Answer the helper",
"func": "msg.payload = { ok: true };\nmsg.statusCode = 200;\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 640,
"y": 100,
"wires": [
[
"res1"
]
]
},
{
"id": "res1",
"type": "http response",
"name": "200",
"statusCode": "",
"headers": {},
"x": 800,
"y": 100,
"wires": []
},
{
"id": "res2",
"type": "http response",
"name": "401 or repeat",
"statusCode": "",
"headers": {},
"x": 660,
"y": 160,
"wires": []
},
{
"id": "find1",
"type": "function",
"name": "Find sign-up code",
"func": "// Look for a sign-up code in the message the helper opened.\nconst m = msg.payload;\nconst text = String((m && m.text) || '');\nconst hit = text.match(/(?:code|PIN|OTP)\\D{0,20}(\\d{4,8})/i) || text.match(/\\b(\\d{6})\\b/);\nif (!hit) {\n node.warn('No code in ' + (m && m.id));\n return null;\n}\nmsg.code = hit[1];\nmsg.message = m;\nmsg.payload = [new Date().toISOString(), msg.source, m.id, m.from, JSON.stringify(m.subject), 'code=' + hit[1]].join(' ') + '\\n';\nmsg.filename = env.get('CODES_FILE');\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 650,
"y": 220,
"wires": [
[
"file1",
"dbg1"
]
]
},
{
"id": "file1",
"type": "file",
"name": "Append to codes.log",
"filename": "filename",
"filenameType": "msg",
"appendNewline": false,
"createDir": true,
"overwriteFile": "false",
"encoding": "utf8",
"x": 900,
"y": 200,
"wires": [
[]
]
},
{
"id": "dbg1",
"type": "debug",
"name": "Code",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"statusVal": "code",
"statusType": "msg",
"x": 880,
"y": 250,
"wires": []
}
]
Open the menu, choose Import, paste the JSON and press Deploy. Add your own debug node on the http in output if you want the first look at each delivery.
Pull with an inject node
Push starts a flow when mail arrives. Pull starts it when you ask, which suits a flow that already has its own trigger, such as a schedule. Set a PULL_TOKEN secret on the helper:
npx wrangler secret put PULL_TOKEN
The helper then answers GET /message/<message id> with the opened message as JSON, and the token goes in the Authorization header. This is the real request from the run and its answer, without the empty html field:
curl -s -H "Authorization: Bearer $PULL_TOKEN" https://mailcycle-decrypt.your-name.workers.dev/message/msg_xzXfnZvuRJTw
{
"id": "msg_xzXfnZvuRJTw",
"inboxId": "ibx_c4dkn8o6frxe",
"keyEpoch": 0,
"receivedAt": "2026-11-27T22:57:29.799Z",
"from": "aosp-oms8@birchpier.com",
"fromName": "Acme Sign-up",
"to": [],
"subject": "Your Acme verification code (pull)",
"text": "Welcome to Acme.\n\nYour verification code is 244404. It expires in 10 minutes.\n\nIf you did not ask for it, ignore this mail.\n",
"attachments": []
}
Without the token, the same request returned 401. In Node-RED the pull half is four nodes:
-
An inject node named
Every 30 s. Set the payload to an env variable namedMESSAGE_IDand Repeat to an interval of 30 seconds. The helper only answers by message id, so the run gave the timer one id. In your own flow the id comes from wherever you learn about the message. -
A function node named
Add token and URL:msg.headers = { authorization: 'Bearer ' + env.get('PULL_TOKEN') }; msg.url = env.get('HELPER_URL') + '/message/' + msg.payload; msg.source = 'pull'; return msg; -
An http request node named
GET /message/<id>, methodGET, with the URL field left empty becausemsg.urlsupplies it, and Return set to a parsed JSON object. That setting is what makesmsg.payload.textthe body. With the default,msg.payloadis a string. -
A function node named
Skip repeatsthat stops anything but a 200 and ignores an id it has already seen, then the sameFind sign-up code,Append to codes.logandCodenodes as in the push half.
In the run, the first timer tick wrote code=244404 to codes.log and the Pulled message debug node showed “Your Acme verification code (pull)”. The later ticks asked for the same id, so Skip repeats let none through. The helper log shows two answers of 200 and then the 401 of the check without a token.
The output of the http request node is the message itself, with no wrapper, so the next node reads msg.payload.subject in pull mode and in push mode alike. That is the same shape in both halves, which is why one Find sign-up code function body serves both.
Run it on a Raspberry Pi or a home server
Node-RED is a common choice for a Raspberry Pi or a small home server, and the push half fits there with two conditions.
- The Pi needs Node.js 22.9 or newer. That is the requirement of Node-RED 5.0. Install it the way Node-RED’s own documentation describes for your Pi, and run Node-RED as a service so that it starts at boot and restarts after a power cut.
- The helper needs an
https://address it can post to. A Pi at home has a private address, so put a reverse proxy or a tunnel in front of port 1880 and use its public HTTPS address inFORWARD_URL. A tunnel that terminates TLS means Node-RED itself can stay on plain HTTP behind it.
Set FORWARD_TOKEN, PULL_TOKEN, HELPER_URL and CODES_FILE as environment variables of the Node-RED service, not in the flow. With systemd that means Environment= lines or an EnvironmentFile= in the unit file. After a change, restart the service so that env.get sees the new values.
A few uses that fit a small always-on box well:
- Sign-up codes to a file or a dashboard. The flow above already does the first half. Add a Node-RED Dashboard text node to show the latest code on a screen.
- Notifications. Send
msg.message.subjectand the sender to a chat or push node when mail arrives at a dedicated address. - Logging to a database or a spreadsheet. Map
from,subjectandreceivedAtto columns. - Triggers for other flows. A link node can hand the opened message to a flow that controls something else, so a particular subject line starts a particular job.
Keep every action one your own flow is allowed to take for the mail you receive, and see Mailcycle’s acceptable use policy for the rest.
An address on your own domain
Everything above works for an address on a shared Mailcycle domain. For an address on your own domain, such as orders@yourcompany.com, use the Scale plan, export that address’s key the same way and add it to the helper’s ADDRESS_KEYS. The flow does not change. The role addresses tutorial shows how to set up the domain and the addresses, and the decrypt guide covers several addresses and rotated keys in one ADDRESS_KEYS value.
Troubleshooting
Nothing reaches the flow.
Open the webhook’s delivery log in the app (API, then Webhooks) and look at the status and error of the latest delivery. Then put a debug node on the http in output: if the request shows there, the problem is after the endpoint, and if it does not, it is the FORWARD_URL.
The helper answers 401.
The WEBHOOK_SECRET does not match the webhook’s signing secret, or the request is older than five minutes. Copy the whsec_ value again and set the secret.
The flow answers 401.
The token in the forward URL does not match FORWARD_TOKEN in the Node-RED process. Check that you restarted Node-RED after setting the variable, and that the ?token= value in FORWARD_URL is the same string.
FORWARD_URL must use https.
The helper only forwards to an address that starts with https://. Use the settings in step 1 or a proxy.
The helper does not trust a self-signed certificate.
Give the helper process NODE_EXTRA_CA_CERTS pointing at the certificate. A deployed Worker needs a certificate from a normal authority, so for a deployed helper use a proxy or tunnel with one.
msg.payload.text is undefined in pull mode.
Set the http request node’s Return field to a parsed JSON object.
The http in request hangs.
Every http in path needs a path to an http response node. Check that both outputs of the check node end in one.
The helper answers 422.
A listed address has no key for the message’s epoch, or the key is not the one for this address. Export the address key again and update ADDRESS_KEYS.
A message arrives twice.
Retries do that. The seen list in step 3 handles it. The list lives in the function node’s context and is cleared when Node-RED restarts, so use a file or a database for a longer memory.
Security and best practices
The helper holds two things. The address key opens that one address’s mail and nothing else. The API key can do what any API key does, so name it for the job and revoke it when the flow ends.
Two checks guard the endpoint. The helper checks Mailcycle-Signature before it forwards anything, and your first function node checks the token in the URL. A request to the endpoint without the token got 401 in the run.
The flow sees the mail. Node-RED receives the opened message, so use an address for mail you intend to automate. A separate address per flow keeps each one to its own mail.
Mail is data. Text in an email is written by the sender. Match on it with a pattern as the flow above does, and decide how it is treated before you pass it to a shell node, a template or an AI node.
Keep the flow file clean. Because env.get reads secrets from the environment, you can keep the exported flow in version control. Never paste a token into a node’s field.
Stop cleanly. To end the flow, delete the webhook in the app, revoke the helper’s API key on the API keys tab, disable the flow tab in Node-RED and delete the Worker if you no longer need it. To stop the old key opening new mail, choose Manage, then Give this address new keys, and export the key again to reconnect the helper.
Which pattern to use
| Pattern | Use it when |
|---|---|
This tutorial: http in and the decrypt helper |
Each email should start a Node-RED flow, with no code of your own to host. The plan is Operator, or Scale for your own domain. |
| n8n Webhook trigger and the helper | The same idea in n8n, with the Webhook trigger and Edit Fields nodes. |
| Your own webhook endpoint | You run the receiving code yourself, in any language, and want the SDK to open the message. |
What you’ve built
Each new email to your address now starts a Node-RED flow with the plain message: who sent it, the subject, the text, the HTML and the attachments list. A function node guards the endpoint with a token and skips repeats, an http response node answers the helper first, and the code in the mail ends up in a file and in the debug pane. You also have an inject-based pull half that asks for a message by id.
From here, connect the flow to the job you wanted it for. Add inbound email to your SaaS turns the same events into tickets, Receive email in Node.js reads mail in code, and Receive email with a webhook shows the signature check in your own server. The n8n workflow tutorial builds the same thing with a Webhook trigger, and Send email from any language covers the sending side.
Read more in the decrypt helper guide, the webhooks guide and the developer overview.
FAQ
How do I start a Node-RED flow when a new email arrives?
Add an http in node with the method POST and a path such as /mailcycle-mail. Deploy the Mailcycle decrypt helper and set its FORWARD_URL to that node's full https address. Add a Mailcycle webhook for message.received that points at the helper. Each new email then reaches the node as one message.
Do I need the email nodes or IMAP in Node-RED?
No. The flow is started by an HTTP request, so there is no mailbox to poll and no password in your flow. The only secrets are two short tokens that your function nodes read from environment variables.
Where is the email in the msg object?
In msg.payload. The http in node puts the JSON body there directly, so the subject is msg.payload.subject and the sender is msg.payload.from. The query string of the URL is in msg.req.query.
Which plan do I need?
Operator, because webhooks and API keys start there. Choose Scale when the address is on your own domain.
Why does the helper refuse my http FORWARD_URL?
The helper only forwards to an https:// address. Serve Node-RED over HTTPS with the https setting in settings.js, or put it behind a reverse proxy or tunnel that provides HTTPS.
Why does the helper wait for an answer from my flow?
It waits for a 2xx status to know the message was delivered. Put an http response node right after the http in node and do the slow work after it, so the answer goes back at once.
How do I read a sign-up or verification code from an email in Node-RED?
Add a function node after the trigger that matches the digits that follow the word code in msg.payload.text. The flow in this tutorial finds 685157 in a test mail and appends one line to a log file with the file node.
How do I skip a message that arrives twice?
Webhooks are retried, and eventId is the same on every try. Keep a short list of the ids you have handled in flow context and answer 200 without running the steps when the id is already in it.
Can I use an inject node instead of a webhook?
Yes. Set a PULL_TOKEN on the helper, then an inject node, a function node that adds the Authorization header and an http request node can fetch the opened message with GET /message/<id>. The helper answers by message id.
Can I run this on a Raspberry Pi or a home server?
Yes, when Node-RED runs on Node.js 22.9 or newer. The helper runs in your Cloudflare account and needs an https:// address it can post to, so give the Pi a public HTTPS address through a reverse proxy or a tunnel.
How do I read the mail of an address on my own domain?
Use the Scale plan for the domain, export the key of that address and add it to the helper's ADDRESS_KEYS. The flow is unchanged.
How do I stop the flow receiving mail?
Delete the webhook in the app (API, then the Webhooks tab), revoke the helper's API key on the API keys tab and disable the http in node or the flow tab in Node-RED.