Downloading a Gmail attachment

I am triggering a flow with the Gmail New Email trigger. I would like to download the attachment in the email. When I look at the log output, the attachment ID is in $1.data.payload.parts[1].body.attachmentId. However, when I try to use this as data variable in the subsequent Gmail Get Attachment action node, $1.data.payload.parts[1] is not available, only parts[0]. How can I get/download the attachment?

1 Like

I figured out how to extract all attachments from incoming emails, so here’s what I came up with:


You set up a Gmail trigger for new messages and send test attachments. All attachment IDs are located in the “parts” folder, and we need to iterate through it to process each attachment separately.

Next, we go to the AI Assistant node to write code that will take the “parts” array and exclude the element with “multipart.”

Here’s the code it generated:

/** @CustomParams
{
  "parts_array": {
    "title": "Array of Parts",
    "key": "parts_array",
    "description": "JSON string of the parts array from node 10",
    "type": "string"
  }
}
*/
export default async function run({ data }) {
  const partsArray = JSON.parse(data.parts_array);
  // Exclude the first element if it's multipart
  const filteredParts = partsArray.filter((part, index) => {
    return !(index === 0 && part.mimeType && part.mimeType.includes('multipart'));
  });
  return { filteredParts };
}

The output is an array containing only the files.

We add the resulting array to the iterator.

Next, we configure the “Get Attachments” node by adding the message ID from the trigger and setting up the JSON request in the format shown in the example below.

And here’s what we’ve got: each element is processed individually. You can then filter, save, and handle the data according to your logic.

Additionally, I would set up a filter after the trigger to check for the presence of attachments. Otherwise, the scenario will fail due to the absence of an attachments folder.

1 Like

btw I forgot to mention, but there’s a trigger specifically for receiving messages only with attachments.
image


Like here. If you don’t want to set it up yourself, just let me know, and I’ll send you the scenario file since it’s already created.

1 Like

Thank you Raian! I would assume that the extra variables for the attachment is something you will add on the roadmap?

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