Split binary file data into individual items - Attachments from mail

I’m trying to save all the attachments from a mail with the write binary file but I can’t manage to write other than the attachment_0 for all the items.
I’ve found this code: Split binary file data into individual items - n8n Documentation but I get this alert: Legacy items is only available in the ‘Run Once for All Items’ mode

what can I do to solve it? The proposed solution from the helper, change items for $input.item aslso gives me an error: ERROR: $input.item is not iterable [line 3, for item 0]
TypeError

Maybe it’s something quite easy but I can’t see it now

Hey @idar,

Welcome to the community :tada:

Assuming you are using the new code node and have it set to Run Once for All Items using the below should do the job.

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;
2 Likes

Hey @Jon !

This doesn’t work when there are multiple files coming from multiple mails, this code makes attachments seperate array item.

My code looks like this;

It just reveals all the files when there are multiple mails and files;

Merging multiple texts works but splitting the attachments into one name doesnt work for multiple files.

I tried to merge data like this;

Do you have any solution about how to send multiple attachments through this way?

It works!!!

Thanks a lot :smiley:

2 Likes

Hi John,

Thanks for this - how to keep the name of the property as “attachment_x” rather than “data”?

Luke

Hey Luke,

I don’t have a quick answer for that, I suspect it would be a case of changing the data option to use the binary item key name.

Thanks john - i carried on experimenting and yep, I can name them all “whateverthehellIlike”, with this:

whateverthehellIlike : item.binary[key],

So replacing that with a string should work, but if I remember rightly you can’t use a variable in the key definition in JS. Someone will correct me if I’m wrong!

For my purposes, just changing them all to be “attachment_0” worked, as I’d written the rest of the routine for single attachment emails using that property name throughout.

1 Like

@Jon How does this code need to be updated if I am taking binary not from the CURRENT node but from OTHER node called “GMAIL Trigger”?

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