What this covers
The question behind every n8n IMAP download attachment job is the same: how do I keep the original file name, why does the write only work in a test execution, why is only the first attachment saved. This page is that mechanics — mailbox in, files out. Turning the files into a ledger is a different guide.
Everything below was run on n8n 2.44.4 against a local test mailbox: five messages, four
PDF invoices of which two were deliberately both named invoice.pdf, one text
file that should not be written, one rasterised PDF. Five files landed, byte for byte
identical to what was mailed; the text file landed nowhere.
Step 1 — what the trigger hands you
The node is Email Trigger (IMAP), and it is a trigger: it runs when mail arrives, so the workflow has to be published, not executed once. 1
- Mailbox Name —
INBOXunless a mail rule moves supplier invoices elsewhere. If it does, this is the setting that goes wrong quietly: the trigger keeps working and stops seeing invoices. - Action — Mark as Read (the default) or Nothing. The documentation still calls the second option None; on 2.44.4 the node says Nothing.
- Download Attachments — off by default, and only visible when Format is Simple. The docs say why: it increases processing. For this job it has to be on.
- Format — Simple is the default; Resolved returns the whole message with attachments as binary data.
- Property Prefix Name —
attachment_. The attachments become binary propertiesattachment_0,attachment_1and so on. This is the thing to know: one mail is one item, and N attachments are N binary properties on that item.
In the run, a two-attachment mail arrived as one item with exactly two binary properties,
each carrying a fileName, mimeType and fileSize, plus
attributes.uid for the mail itself.
One execution can hold several mails. In our run the
third execution received two messages at once — three attachments across two items.
A workflow written as "write $json.attachment_0" saves one file from a mail that
had two, and nothing at all from the second mail. Anything that walks attachments has to start
from $input.all().
Step 2 — an attachment is binary data, not a file
The PDF is n8n binary data. It is not on your disk yet and not something you can read in an
expression. n8n stores the bytes itself, in the mode its configuration names, and hands the
item a reference: on the test instance every binary property carried an id
starting filesystem-v2:, n8n's own storage under its user folder — a copy of your
invoices inside the n8n directory.2
The node that puts bytes on disk is Read/Write Files from Disk, the successor of the old Write Binary File node (whose fields were File Name and Property Name; on 2.44.4 it is still installed but hidden, so it no longer shows in the panel). Two parameters matter here:3
- File Path and Name — the full path including the extension. Absolute paths are what n8n's documentation recommends, and the next section is about why a relative one is fragile.
- Input Binary Field — the binary property holding the file, default
data. That default is why the split step renames the property instead of teaching the expressionattachment_1. - Options → Append is off by default. Leave it off: appended PDFs are files no reader can open.
Step 3 — the folder n8n refuses to write to
This is the failure that looks like a bug and is not. Since n8n 2.0,
N8N_RESTRICT_FILE_ACCESS_TO defaults to ~/.n8n-files, and the file
nodes may only read and write inside the directories it names.
4 Our first run left the variable unset and pointed the write at a
directory under /tmp: the execution ended in error and the node said,
in full, Access to the file is not allowed. No file, no partial file, no hint
about which setting to change.
The second run set the variable to that same directory, and the same workflow wrote five
files without complaint. In docker-compose.yml it looks like this — the path is
the container's, so the directory has to be mounted too:
services:
n8n:
image: n8nio/n8n
environment:
- N8N_RESTRICT_FILE_ACCESS_TO=/invoices
volumes:
- ./invoices:/invoices
Two details from the same settings page:
- The value is a semicolon-separated list, and
~expands to the home directory of the user running n8n. An empty value turns the restriction off — it works, and it is the wrong default for a box that runs invoices. N8N_BLOCK_FILE_ACCESS_TO_N8N_FILESdefaults totrueand keeps the.n8ndirectory blocked even when it sits inside an allowed path.
One documentation wrinkle, since this site promises checkable claims: the Read/Write Files
page says both that self-hosted n8n reaches any path by default and that the variable
defaults to ~/.n8n-files since 2.0. On 2.44.4 it blocks; before 2.0 the first
sentence applies. On n8n Cloud the node is limited to /home/node/, and the Cloud
filesystem is documented as not surviving a redeploy. Either way it is the same n8n IMAP
download attachment job; only the allowed folder differs.
Step 4 — one item per attachment, and a name that cannot collide
Two problems, one node. The trigger gives you an item per mail; the write node
works on an item per file. And the original name is not unique: two suppliers both
send invoice.pdf, and the second overwrites the first without an error — in our
mail set two of the four invoices were named invoice.pdf on purpose.
A Code node set to Run Once for All Items fixes both: it walks every input
item, every binary property on each, and emits one item per attachment with the binary
property renamed to data and a path built from the mail UID, the attachment index
and the original name.
const OUT_DIR = '/invoices/inbox';
const results = [];
for (const item of $input.all()) {
const uid = item.json.attributes?.uid ?? 'nouid';
let index = 0;
for (const [key, file] of Object.entries(item.binary ?? {})) {
const originalName = file.fileName ?? key + '.bin';
const mimeType = file.mimeType ?? '';
const isPdf = mimeType === 'application/pdf' || /\.pdf$/i.test(originalName);
const safeName = String(originalName)
.replace(/[\/\\:*?"<>|\u0000-\u001f]/g, '_') // no path separators, no control characters
.replace(/^\.+/, '')
.slice(0, 120) || 'attachment.pdf';
results.push({
json: {
uid, index, originalName, mimeType, isPdf,
fileName: `${OUT_DIR}/${uid}-${index}-${safeName}`,
},
binary: { data: file },
});
index++;
}
}
return results;
Why the UID: it is unique inside a mailbox and stays with the message, so a file can be
traced back to its mail and a re-run overwrites its own files instead of making copies. The
index separates attachments inside one mail — two can share a name and a size and still be
different documents. The sanitising line matters too: a file name is data from outside, and a
name containing / or .. is a path, not a name.
Then an If node on {{ $json.isPdf }}, true branch to the
write node, false branch to No Operation. The false branch is where the
delivery note, the winmail.dat and the signature logo stop, and an execution you
can read afterwards shows them doing so. In our run the text attachment took that branch and
no .txt file was written. Finally: File Path and Name
{{ $json.fileName }}, Input Binary Field data.
Step 5 — mark as read, and the trigger that goes quiet
Action = Mark as Read is the default and a decision, not a detail. After
our five test mails, a search on the test server found five \Seen and zero
\Unseen. If a later node fails, that mail is already read and will not come back
by itself: the execution list is where you find it. The other reading, Nothing, leaves
mail unread for something downstream — at the price of an interruption delivering the same
invoice twice. With the naming rule above, twice is survivable.
The forum's standing complaint is the trigger that stops firing, and three mechanisms
explain almost all of it. The trigger keeps a watermark — the highest UID it has processed —
in the workflow's static data and searches only above it, so recreating a mailbox or the
workflow resets what "new" means. Options → Custom Email Rules is the search
itself, JSON, defaulting to ["UNSEEN"]; a rule matching nothing looks exactly like
a broken trigger. And Options → Force Reconnect Every Minutes (default 60)
exists for servers that close idle connections. On 2.44.4 the Options collection also has
Fetch Only New Emails (default on), which the documentation page read on 2026-11-17
does not list — it is the switch for the watermark.
Threads people land on when this bites: a trigger that stops after inactivity, saving several attachments under their original names, and the IMAP-to-write-node question.
Step 6 — prove it ran: files, not hope
- Count. Attachments in the mail equal new files. Ours: mail one → two files, mail two → one, the third execution carried two mails → three attachments in, two files out, the third being the text file on the false branch.
- Hash.
sha256sumon the file and on the attachment as it was sent. All five matched, which is what rules out bytes handled as text somewhere in the middle. - Execution list. A run where the write node shows zero items is a run where the path was refused.
Remember the second copy: n8n keeps execution data for 336 hours by default, so your invoices sit twice on one disk until pruning — see the execution history guide.
Honest limits
- No OCR. This workflow saves what was attached. A scan is saved as a scan, and the text extraction that follows returns nothing from it — measured on this run: 0 characters, no error. The OCR guide is about that case.
- One folder, one mailbox. Two suppliers filing into two folders means two triggers, or one rule that collects everything first.
- Size is a memory question. The download happens inside the trigger. n8n documents no per-attachment limit for this node, so the limit is your mail server's and your instance's memory — measure it on your mail, do not take a number from a blog.
- UIDs belong to a mailbox. If the mailbox is recreated, UIDs restart and an old file can be overwritten. Put the date in the name if that matters.
- What was actually tested: n8n 2.44.4, five mails through a local IMAP server, five files written and hashed, one non-PDF routed away, one run deliberately failed to show the file-access error. One mailbox, one afternoon — not a month of your suppliers.
What comes after the files
Saving attachments is the first half of an invoice pipeline. The second half reads the text layer, pulls out supplier, number, dates and amounts, checks that net plus VAT equals the total, and writes each invoice to a ledger once. That is the invoice pipeline guide, and you can try it on one of your own PDFs with the free starter workflow — no account, no API key, nothing written.
The eleven-workflow pack does the whole job on a self-hosted instance, mailbox included.
Invoice Ledger — 11 workflows, USD 49
It is also listed on the n8n template library. One business, one purchase, 14-day fix-or-refund if it does not import or cannot read your invoices.
Sources
- n8n docs — Email Trigger (IMAP) (Mailbox Name; Action; Download Attachments; Format; Custom Email Rules; Force Reconnect). Read 2026-11-17.
- n8n docs — Binary data (
N8N_DEFAULT_BINARY_DATA_MODE, storage path). Read 2026-11-17. - n8n docs — Read/Write Files from Disk (File Path and Name; Input Binary Field; Append; absolute paths; Cloud and Docker notes). Read 2026-11-17.
- n8n docs — v2.0 breaking changes (
N8N_RESTRICT_FILE_ACCESS_TOdefaults to~/.n8n-files) and Security environment variables (semicolon-separated;N8N_BLOCK_FILE_ACCESS_TO_N8N_FILES). Read 2026-11-17.