Gmail attachment

Describe the problem/error/question

I’m trying to build an automation that sends an email to the correct recipient and attaches a specific file from Google Drive.
The workflow successfully extracts information from the filename, finds the correct email address, and creates the email content.
Sending the email without an attachment works, and sending the file alone as an attachment also works.
However, when I try to send the email with the attachment included and the filtered emailadress, the workflow fails.
I suspect the issue might come from the Merge node, possibly not passing through the binary data correctly.

What is the error message (if any)?

The item has no binary field ‘Bericht’ [item 0] (item 0)

Check that the parameter where you specified the input binary field name is correct, and that it matches a field in the binary input

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.90.2
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi Benry!

I looked at your workflow and I think there are 2 problems:

  1. Keeping the data from the previous node when using Code nodes.
  2. Merge node should use the Keep Matches options.

Code Node

Generally what I’ve found is that you want to try and append data from one node to the next. So think of each node adding to or subtracting to a particular data item as it makes its way through the workflow.

What I can see in your Code 2 node is that you create a data object and then return that entire data object in the return statement, which means that the downloaded binary data retrieved in the Google Drive node is thrown away.

Instead, what I think you should do is keep the loop that iterates over each input item and append the data that you’re extracting from the filename (Jahr, Probenummber, Kundenummern, etc..) like this

for (const item of $input.all()) {
  const parts = parseFilename();
  const data = {
  "Jahr": convertValue(parts[0] || ""),
  "Probenummer": convertValue(parts[1] || ""),
  "Kundennummern": convertValue(parts[2] || ""),
  "Grund": convertValue(parts[3] || ""),
  "Produktgruppe": convertValue(parts[4] || ""),
  "Rebsorte": convertValue(parts[5] || ""),
  "Geprüft für": convertValue(parts[6] || ""),
  "Artikelnummer": convertValue(parts[7] || ""),
  "NummerA": convertValue(parts[8] || ""),
  "NummerB": convertValue(parts[9] || "")
  };

  item.json.data = data;
  
}

This will keep the binary object for each item (in fact in the code the n8n IDE will provide a hint when you type in item.binary.Bericht.).

You can read more about the Data structure in the docs.

Merge Node

It appears that you want to use the Merge node to lookup the email address and match based on Kundennummern however in the Output Type option you’ve selected Keep Everything. What this will do is actually ignore the Fields to Match option and merge all inputs. This means that items that don’t match will also get passed through from the google sheet (in other words items that were not downloaded from google drive and have no Bericht data).

But in order to filter the results so that only records where Kundennummernis the same, you should change this to Keep Matches.

:warning: If you follow the change that I made in the Code Node, the field Kundennummern is now inside another object called data so you’ll need to also enable the switch for Fields To Match Have Different Names and Input 1 Field would be set to Kundennummern but Input 2 Field would be set to data.Kundennummern

Finally, if you think I’ve solved your problem, please don’t forget to set this as Solution :white_check_mark: :grinning_face: !

Thanks for your response - it really helped me!
I’m still not completely sure I understood the part about the merge node. What I did was first merge the email address with the remaining data from Code2, which worked fine. Then I tried to merge that result (Merge1) with the document I want to attach to the email. The first merge works, but the second one doesn’t.

Maybe there’s a different or better way to attach the document to the email?

It’s a little hard to tell as I’m guessing at what the google sheet contains. But I can see that if you have an Output Type of Keep Everything, you may end up with items that don’t have the attachment.

In any case the merge directly before the Send Gmail (it’s just called Merge) isn’t needed anymore because the code I provided above keeps that data along with the exracted data from the filename.

Also bear in mind that when you Fetch Test Event in the Google Drive Trigger, you may only get 1 file - you may need to do some testing with the worfklow active to see what happens if you get multiple files in the 1 trigger. So this is why it may work when testing but it’s different when the workflow is active.

So that the Get row(s) from google sheet doesn’t trigger on each file, you would also want to enable Execute Once in the node’s Settings.

In fact, I’ve been playing around a little more and here is my shortcut version you can refer to (you will need to change all the document and folder references of course and I took a shortcut with the code node too).

2 Likes

I had a similar issue with the merge, where data from one input and text from the other were supposed to be combined.

The solution for me was to attach the ‘Data’ to input 1 and everything else to input 2, etc. Only then was ‘Data’ passed through correctly.

Amazing! You solved it. Thank you very much!

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