How to merge Files from Split-in-Batches Node into One to send in a Mail?

Hi,

I want to send e mail with attachments and before doing that, I need to gather all the attachments with the gDriveIds of attachments and then send them in one mail.

But I couldn’t achive it like this. I’m able to get the attachment but in every attachment I get, it sends a mail.

How can I collect all the attachments I download from drive and send them in one mail?

Information on your n8n setup

  • n8n version:Version 0.219.0
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker):
  • Operating system:Linux

I found a couple of solutions,

But these are not for files, works well with data.

1 Like

Hey @BramKn ,

This one didn’t solve my issue with files. It collects the data correct but not the binary files.

Then I tried this algorithm to collect all files into one but this algorithm cannot see both files, can only see a file from one cycle;

let results = [];

for (const item of $input.all()) {
    for (key of Object.keys(item.binary)) {
        results.push({
            json: {
                fileName: item.binary[key].fileName
            },
            binary: {
                data: item.binary[key],
            }
        });
    }
}

return results;

Hi @onurbolaca

try this

const allData = []

let counter = 0;
do {
  try {
    const items = $items("RSS Feed Read", 0, counter);
    allData.push.apply(allData, items);
  } catch (error) {
    return [{allData}];  
  }

  counter++;
} while(true);


1 Like

Hey @BramKn ,

Thank you for your answer,

I applied what you provide and the data looks like this now;

I need to put them into attachments fields of send mail node;

Hi @onurbolaca
Try this

const allData = []

let counter = 0;
do {
  try {
    const items = $items("RSS Feed Read", 0, counter);
    allData.push.apply(allData, items);
  } catch (error) {
    return [allData];  
  }

  counter++;
} while(true);

Hey @BramKn ,

Still the same;

Do you know any other way?


image

Thank you!

This was working well but couldnt combine with what I have;

let results = [];


for (const item of $input.all()) {
    for (key of Object.keys(item.binary)) {
        results.push({
            json: {
                fileName: item.binary[key].fileName
            },
            binary: {
                data: item.binary[key],
            }
        });
    }
}


return results;

It is simply the return data being wrong, and me not looking properly :wink:
another one

const allData = []

let counter = 0;
do {
  try {
    const items = $items("RSS Feed Read", 0, counter);
    allData.push.apply(allData, items);
  } catch (error) {
    return allData;  
  }

  counter++;
} while(true);
1 Like

Thanks @BramKn ,

This one merge the files as one result.

I cannot send them in one mail though, it sends two mails for each file;

Do you have any idea about how to send them in one mail?

It comes like this;

Drive Node

Merge Data JS Node

Ah didn’t see you wanted to have em on one item.
You need to add them together idd. I don’t have a quick example for you.

1 Like

Thanks @BramKn ,

Here is how it works;

const result = { binary: {} };
const resultArr = [];

let counter = 0;
do {
  try {
    const items = $items("Download Files", 0, counter);

    // Add each item to the binary object of the result object
    items.forEach((item, index) => {
      result.binary[`fileName${counter + index}`] = item.binary.data;
    });

  } catch (error) {
    resultArr.push(result);

     return resultArr;
     
  }

  counter += items.length;
 
} while (true);



I haven’t tried it yet, but in a never n8n version the “split in batches” node has a “done” output which as far is i understood it, should output all items at once.

You can aggregate these items into one with the List node.

EDIT: I do not know if that works with binary data (files).

@FelixL ,

Please see above. It is possible with js code to combine all files as different arrays and same array like in here: Merge Two Files in First Row for One Mail

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