How to download multiple files and attach all of them in a single email

The goal

I’m trying to iterate over an unknown number of files, download each file’s binary data, and include all of the files as attachments in the Send Email node.

My approach

In my case, the “get deal info” node returns n file URLs so that each one can be referenced as $node["get deal info"]["data"]["deal"]["statements"]["edges"][*n*]["node"]["url"].

The “iterate over files” code node outputs each URL separately.

Then a “loop over items” node sends each URL to the “download file” node, which makes a GET request and puts each file’s binary data in a field named data{{ $node["loop over items"]["context"]["currentRunIndex"] }}.

The “aggregate files” code node returns the binary data names as a comma-separated string.

And finally, the “send email” node’s Attachments expression is {{ $('aggregate files').item.json["attachments"] }}.

The problem

An email is being sent out, but without any attachments. The comma-separated string “data0,data1,data2” doesn’t seem to reference any binary data. How do I properly aggregate the binary data and attach all the files in one email?

My workflow

Last node’s output

[
  {
    "accepted": [
      "[email protected]"
    ],
    "rejected": [],
    "ehlo": [
      "SIZE 35882577",
      "8BITMIME",
      "AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH",
      "ENHANCEDSTATUSCODES",
      "PIPELINING",
      "CHUNKING",
      "SMTPUTF8"
    ],
    "envelopeTime": 149,
    "messageTime": 401,
    "messageSize": 285,
    "response": "250 2.0.0 OK  1712679531 f8-20020a05600c4e8800b00415dfa709dasm17903532wmq.15 - gsmtp",
    "envelope": {
      "from": "[email protected]",
      "to": [
        "[email protected]"
      ]
    },
    "messageId": "<[email protected]>"
  }
]

My n8n setup

  • n8n version: 1.29.1
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud
  • Operating system: Mac OS, Arc browser

hello @fundmore

You will need to have the binary files as one item to attach them in the Sent Email node.
See example

Thanks for your help. Two questions:

  1. If the “download file” node puts all outputs in a field called “data”, won’t each successive file overwrite the previous one? I’m guessing that’s why in your screenshot, all 3 files are named “April” even though the “iterate over files” node outputs them as February, March, and April.

  2. What expression would I type in the “send email” node’s Attachments field to reference the Aggregate node’s output? I tried {{ $('Aggregate').item.json["data"] }} but it’s returning an array of URLs instead of a comma-separated list of binary data names (see attached).

By the way, the Attachments field’s help text says:

Name of the binary properties that contain data to add to email as attachment. Multiple ones can be comma-separated. Reference embedded images or other content within the body of an email message, e.g.

Any help is much appreciated :slight_smile:

From this forum post I got {{ Object.keys($binary).join(',') }}, which works only when the binary files are in the previous node.

How do I reference the binary data of a node that’s not right before the current one?

I ended up just redownloading & aggregating the files in every loop iteration, which is the slower workaround that I was trying to avoid.

It seems that there is no way of passing binary data between nodes unless the node using that data immediately follows the aggregate node.

  • Merging the aggregate node works for the first iteration, but the rest of the loop iterations do not run
  • Extract from File & aggregating works but when I try to Convert to File within each iteration, the key does not reference the value (i.e. the JSON data) from the previous aggregate node

So if you want to send multiple emails to different recipients, albeit with the same files attached, you’ll have to redownload all the files for every recipient before sending each email.

Can someone from the n8n team confirm that this is the only way?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.