Download multiple E-Mail Attachments from multiple E-Mails

Describe the issue/error/question

I have a IMAP Trigger which checks for new Emails. The Emails have attachments. Some Emails have multiple Attachments, some Emails only have one attachment. Depending on the Recipient in the email the attachments are supposed to be uploaded to a specific path with the Nextcloud API. For this I have an javascript function node.
Everything works but if I have multiple Emails, it only works for one Email. All Emails are marked as read but it only downloads the attachments for one email.

Please share the workflow

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "let emailTrierVon = \"[email protected]\";\nlet emailBitburgVon = \"[email protected]\";\n\n\nlet pfadTrier = \"/Trier Dateien/\";\nlet pfadBitburg = \"/Bitburg Dateien/\";\n\n\n// Loop over inputs and add a new field called 'myNewField' to the JSON of each one\n/* \nfor (item of items) {\n  item.json.myNewField = 1;\n  console.log(item);\n}\n*/\n// You can write logs to the browser console\nfor (let item in items) {\n  if(Object.keys(items[item]).includes('binary')){\n    if(items[item].json.to.text == emailTrierVon) {\n      items[item].json.pfad = pfadTrier;\n    } else if(items[item].json.to.text == emailBitburgVon){\n      items[item].json.pfad = pfadBitburg;\n    }\n    return Object.keys(items[item].binary).map(key => {\n    return {\n      json: {\n        json: items[item].json,\n        },\n      binary: {\n        data: items[item].binary[key],\n      }\n    }\n});\n  }\n  return item;\n}\nreturn items;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        740,
        190
      ]
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "mailbox": "Test",
        "postProcessAction": "nothing",
        "format": "resolved",
        "dataPropertyAttachmentsPrefixName": "data",
        "options": {}
      },
      "name": "IMAP Email",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 1,
      "position": [
        540,
        190
      ],
      "alwaysOutputData": false,
      "credentials": {
        "imap": "DATEV KSGOC"
      }
    },
    {
      "parameters": {
        "path": "=/Clever Fit/test/{{$binary.data.fileName}}",
        "binaryDataUpload": true
      },
      "name": "Nextcloud",
      "type": "n8n-nodes-base.nextCloud",
      "typeVersion": 1,
      "position": [
        950,
        190
      ],
      "credentials": {
        "nextCloudApi": "KSGOC Cloud"
      }
    },
    {
      "parameters": {
        "functionCode": "let emailTrierVon = \"[email protected]\";\nlet emailBitburgVon = \"[email protected]\";\n\n\nlet pfadTrier = \"/Trier Dateien/\";\nlet pfadBitburg = \"/Bitburg Dateien/\";\n\n\n// Loop over inputs and add a new field called 'myNewField' to the JSON of each one\n/* \nfor (item of items) {\n  item.json.myNewField = 1;\n  console.log(item);\n}\n*/\n// You can write logs to the browser console\nfor (let item in items) {\n  if(Object.keys(items[item]).includes('binary')){\n    if(items[item].json.to.text == emailTrierVon) {\n      items[item].json.pfad = pfadTrier;\n    } else if(items[item].json.to.text == emailBitburgVon){\n      items[item].json.pfad = pfadBitburg;\n    }\n    return Object.keys(items[item].binary).map(key => {\n    return {\n      json: {\n        json: items[item].json,\n        },\n      binary: {\n        data: items[item].binary[key],\n      }\n    }\n});\n  }\n  return item;\n}\nreturn items;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        740,
        190
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Nextcloud",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IMAP Email": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Information on your n8n setup

  • n8n version:
    0.136.0

  • Database you’re using (default: SQLite):
    SQLite

  • Running n8n via [Docker, npm, n8n.cloud, desktop app]:
    Docker

Hi @aiko, welcome to the community :tada:

I think you could approach this in a similar fashion to what I have described here:

Essentially convert a single n8n item (your incoming email) with multiple binary files into multiple n8n items each with a single binary file inside the data property.

Your Nextcloud node would then run once for each item it receives and can process a single file with each run. Let me know if you run into any trouble with this :slight_smile:

Hi @MutedJam,

thanks a lot for your answer. I use this solution already. The problem is, I have three emails with attachments. It works for one email but it ignores the other Emails?!

Hello, I found the solution witch for loops. If you use loops, you cant use return, cause it finishes the loop. You have to use an array and push it into it.
Thanks a lot!

1 Like

Hi @aiko, I am so sorry for missing the multiple email part from your question, not sure what I was thinking yesterday. I am glad to you found a solution though, thanks so much for confirming.

Based on your description the loop seems to be the most sensible approach here. Hope you enjoy your automation journey :slight_smile: