Invoice Ledger

n8n IMAP download attachment: getting invoice PDFs onto disk

The trigger does not give you a file. It gives you an item with one binary property per attachment, and the rest of the job is naming and paths.

Guide · node names and parameters checked against n8n documentation on 17 November 2026 · workflow run on n8n 2.44.4 · works with JavaScript switched off

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

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

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:

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

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

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

  1. n8n docs — Email Trigger (IMAP) (Mailbox Name; Action; Download Attachments; Format; Custom Email Rules; Force Reconnect). Read 2026-11-17.
  2. n8n docs — Binary data (N8N_DEFAULT_BINARY_DATA_MODE, storage path). Read 2026-11-17.
  3. 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.
  4. n8n docs — v2.0 breaking changes (N8N_RESTRICT_FILE_ACCESS_TO defaults to ~/.n8n-files) and Security environment variables (semicolon-separated; N8N_BLOCK_FILE_ACCESS_TO_N8N_FILES). Read 2026-11-17.

Version note: node names and parameters were read from the n8n documentation on 2026-11-17 and compared with the installed package of n8n 2.44.4, the version the workflow was run on. Where the documentation and that version disagree, both are stated.