Download from FTP & Email send

Hello.

Can someone help cause i stuck on the following scenario.

I download multiple files via FTP and want these files to send them all together as attachments via Email.

I stuck because for example if i download 3 files via FTP then the Email sends 3 different emails with 1 attachment instead of 1 email with the 3 attachments.

Can someone shine some light?

Thank you.

Hey @Mulen,

Welcome to the community :sparkling_heart:

Did you check this post: How i can attach two spreadsheet to one email - #4 by harshil1712? The solution provided should work for you as well :slight_smile:

If you need more help, do let us know!

Yes i checked it but i don’t have 2 workflows like this post.

My workflow is something like that:

It lists all the items in a folder. Download the items and then i want all to send them via 1 email only.

Now it sends multiple emails depending on how many files are downloaded from FTP

Here are a couple of things to try:

  • compress all the attachments together into a single zip file and then send the zip file
  • loop through all the objects in the array and merge them into a single object then send that so it will attach multiple attachments to one email

These are just theoretical. I’ll give these a try later today as a test.

Thank you.

The zip functionality thought would be good BUT in my workplace they want the attachments separated in the email. Many end-users do not know how to open a zip file in 2021…

I will really appreciate it if you can give me a solution. Thank you. I’m new to n8n and still learning.

The day you stop learning is the day you stop growing! :grinning:

I’ll spend some time later today seeing if I can figure something out for you.

1 Like

Here a basic example:

{
  "nodes": [
    {
      "parameters": {
        "operation": "list"
      },
      "name": "FTP",
      "type": "n8n-nodes-base.ftp",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "ftp": ""
      }
    },
    {
      "parameters": {
        "path": "={{$json[\"path\"]}}"
      },
      "name": "FTP1",
      "type": "n8n-nodes-base.ftp",
      "typeVersion": 1,
      "position": [
        600,
        300
      ],
      "credentials": {
        "ftp": "local ftp"
      }
    },
    {
      "parameters": {
        "fromEmail": "",
        "toEmail": "",
        "subject": "Send multiple attachments",
        "attachments": "={{Object.keys($binary).join(',')}}",
        "options": {}
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        900,
        300
      ],
      "credentials": {
        "smtp": ""
      }
    },
    {
      "parameters": {
        "functionCode": "const newItem = {\n  json: {},\n  binary: {},\n};\n\nlet count = 0;\nfor (item of items) {\n  newItem.binary[`data${count++}`] = item.binary.data\n}\n\nreturn [newItem];"
      },
      "name": "Combine into item",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        750,
        300
      ]
    }
  ],
  "connections": {
    "FTP": {
      "main": [
        [
          {
            "node": "FTP1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "FTP1": {
      "main": [
        [
          {
            "node": "Combine into item",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine into item": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thank you very much.

It almost works it brings in binary tab all the 3 items but json and table are empty. It also sends 1 email now but it is empty without the attachments.

Am i doing something wrong?

@Jan beat me to the punch!

I built up a POC workflow that does exactly what you want:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "fileSelector": "*.n8n"
      },
      "name": "Read Binary Files",
      "type": "n8n-nodes-base.readBinaryFiles",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "fromEmail": "[email protected]",
        "toEmail": "[email protected]",
        "subject": "Files",
        "attachments": "={{$json[\"attachments\"]}}",
        "options": {}
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        850,
        300
      ],
      "credentials": {
        "smtp": "SMTP Creds"
      }
    },
    {
      "parameters": {
        "functionCode": "// Create empty array to hold the items\nvar files = items;\n\n//Clear and prep the items array\nitems = [{json:{attachments:\"\"},binary:{}}];\n\n//iterate over all the files in array\nfor (file of files) {\n  //Add file name to the attachments list\n  items[0].json.attachments = items[0].json.attachments + \",\" + file.binary.data.fileName;\n  //Add the file to the first item in the items array\n  items[0].binary[file.binary.data.fileName] = file.binary.data;\n}\n\n//Clean up the attachments list\nitems[0].json.attachments = items[0].json.attachments.substring(1);\n\nreturn items;"
      },
      "name": "Prep Files",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        640,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Read Binary Files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Binary Files": {
      "main": [
        [
          {
            "node": "Prep Files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prep Files": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

You really only need the last two nodes as they prepare the files and then the second one sends them accordingly.

Let me know if it works.

2 Likes

Oh my god i can’t believe it, it works.

I can’t thank you enough!

I should really learn javascript.

Thank you a million times!

Javascript is a huge help when there isn’t a node yet that does exactly what you want.

Glad this worked for you.

@Tephlon

Can i ask something else?

As i said the above works.

I have the following scenario.

This is the table with files.

The table has a column matchkey.

I want to use this code but instead of merging all the files at once like this

I want the same functionality but they are merged depending of the matchkey column.

So the end result will be something like this


[
{
"matchkey":"mk"
"attachments": "mk0410.dat,mk0410.csv.txt,mk0410.txt"
},
{
"matchkey":"dim"
"attachments": "dim0410.csv.txt,dim0410.dat,dim0410.txt"
}
] 

I really tried to come up with a solution but i can’t make it through.

Do you have any suggestions?

Thanks.

Without knowing all of the background, I would probably start by using an IF node and get it to check the matchkey field. If it is mk, send it to the true branch and send dim to the false branch. Then, you can work with each of these branches independently.