N8n IMAP Email node - How to download and write all attachments using Write Binary File node?

like in topic. I am struggling with that for two days now. There is no problem to download one, but I have problem how to handle with various number of attachments.

Hi @piotr, welcome to the community!

I played around with this for a bit, and did indeed run into some trouble as well when trying to receive emails with more than one attachment though I have a feeling this might be related to my test email provider and I’ll run some further tests tomorrow.

In the meantime, could you share your workflow so we can get an idea of what you were doing here? You might also want to take a look at How to save multiple email attachments with original filename? - #3 by N8N_userN, maybe this related thread already contains some helpful insights for you.

Hi @piotr, thanks for your patience here.

I have ran some additional tests this morning and did not run into any problems saving more than one attachment to disk using a workflow like this:

{
    "nodes": [
      {
        "parameters": {
          "downloadAttachments": true,
          "options": {}
        },
        "name": "Read Incoming Email",
        "type": "n8n-nodes-base.emailReadImap",
        "typeVersion": 1,
        "position": [
          250,
          200
        ],
        "credentials": {
          "imap": "foo"
        }
      },
      {
        "parameters": {
          "fileName": "=/home/node/{{$binary.attachment_0.fileName}}",
          "dataPropertyName": "attachment_0"
        },
        "name": "Write Attachment 0",
        "type": "n8n-nodes-base.writeBinaryFile",
        "typeVersion": 1,
        "position": [
          450,
          100
        ]
      },
      {
        "parameters": {
          "fileName": "=/home/node/{{$binary.attachment_1.fileName}}",
          "dataPropertyName": "attachment_1"
        },
        "name": "Write Attachment ",
        "type": "n8n-nodes-base.writeBinaryFile",
        "typeVersion": 1,
        "position": [
          650,
          200
        ]
      },
      {
        "parameters": {},
        "name": "NoOp",
        "type": "n8n-nodes-base.noOp",
        "typeVersion": 1,
        "position": [
          650,
          400
        ]
      },
      {
        "parameters": {
          "conditions": {
            "boolean": [
              {
                "value1": "={{$binary.attachment_1 ? true : false}}",
                "value2": true
              }
            ]
          }
        },
        "name": "Attachment 1 exists?",
        "type": "n8n-nodes-base.if",
        "typeVersion": 1,
        "position": [
          450,
          300
        ]
      }
    ],
    "connections": {
      "Read Incoming Email": {
        "main": [
          [
            {
              "node": "Write Attachment 0",
              "type": "main",
              "index": 0
            },
            {
              "node": "Attachment 1 exists?",
              "type": "main",
              "index": 0
            }
          ]
        ]
      },
      "Attachment 1 exists?": {
        "main": [
          [
            {
              "node": "Write Attachment ",
              "type": "main",
              "index": 0
            }
          ],
          [
            {
              "node": "NoOp",
              "type": "main",
              "index": 0
            }
          ]
        ]
      }
    }
  }

Now this requires me to know how many attachments there would be at best as each one uses it’s own Write Binary File node. I have also added an IF node to capture cases where fewer attachments are sent.

Would this approach work for you?

Hi @piotr, to follow up on my post from earlier today: This approach can get complex when dealing with a lot of individual files. So you might want to utilize a function node as described under JavaScript Code Snippets | Docs instead which can help you dealing with multiple files as well.