IMAP Email: Getting a specific e-mail attachment

All,

Scenario: You want to process an attachment (let’s say a pdf) in an e-mail that has multiple attachments.

As I couldn’t find an easy way to filter on attachments, I created a Function Node with the following code:

items[0].json.attachmentIndex = Object.values(items[0].binary).findIndex(key => key.mimeType === "application/pdf");
return items;

This returns the index of the pdf attachment, which can be used in the Binary property of further Nodes like this:

attachment_{{$node["Function"].json["attachmentIndex"]}}

The above code is based on the assumption that there is only one pdf attachment. But I think it could be easily extended with other criteria, like filename.

Sharing this as it took me a while to figure out how to get to the right attachment (i.e. the one I needed to act upon).

Interested to read if there are other (better!) solutions.

3 Likes

Thanks a lot @vco1 for sharing that with people!

1 Like