# Email multiple files as attachments dynamically in email node

**URL:** <https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968>\
**Category:** General\
**Created:** [26 September 2019 20:45 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968 "2019-09-26T20:45:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmansour](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bmansour](https://discourse.nodered.org/u/bmansour)\
**Post date:** [26 September 2019 20:45 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/1 "2019-09-26T20:45:35Z")

</div>

Hi guys, quick one I'm trying to send more than one file as attachements using the email node. This is how I was doing it and working well.

```auto
const date = new Date(msg.payload)

return {
    topic: 'Daily reports: ' + date.toISOString(),
    attachments: [
        {path: flow.get('excel-log-file13230631')},
        {path: flow.get('excel-log-file4659789')},
        {path: flow.get('excel-log-file13273344')},
        {path: flow.get('excel-log-file4679763')},
        {path: flow.get('excel-log-file4678178')},
        {path: flow.get('excel-log-file13245998')}
        //{path: flow.get('excel-log-file13246381')},
        //{path: flow.get('excel-log-file12131116')},
        //{path: flow.get('excel-log-file13272079')}
    ],
};

```

But I noticed that this is not an efficient way of doing so. Basically I have modified my flow to dynamically set my filenames based on flow variables like this:

```auto
let d = new Date().toISOString().slice(0, 10);
let id = msg.payload.id;
let ref = msg.payload.reference;
let filename = '/home/ubuntu/logs/'+id+'_'+ref+'_'+d+'.csv';
const delimiter = ','

flow.set('excel-log-file', filename)

return {}

```

This works well but now I am having trouble iterating through the actual files to attach them and can't seem to find a way to do so... Should it maybe be easier to send the whole folder (as in `/home/ubuntu/logs`) not sure if that is possible.

Please any ideas would be great! Thanks in advance.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [26 September 2019 21:39 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/2 "2019-09-26T21:39:42Z")

</div>

Have you thought of setting a single flow variable instead, but an array of strings to the paths, to be unloaded when adding the attachments?  
You could also take a look at `node-red-contrib-fs` with the setting all files in a single message. That gets you a set of files following a pattern inside that logs folder. If you store that array in a flow variable or sending it directly upstream you can use it that way.

---

<div class="post-metadata">

**Author:** ![bmansour](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bmansour](https://discourse.nodered.org/u/bmansour)\
**Post date:** [26 September 2019 22:49 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/3 "2019-09-26T22:49:44Z")

</div>

@afelix `node-red-contrib-fs` looks like exactly what I need! I'll give it a try in the morning. Also haven't thought of the array an unloading great idea! Will see if this works, thanks!

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [27 September 2019 06:51 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/4 "2019-09-27T06:51:57Z")

</div>

I found and used it recently myself 🙂

---

<div class="post-metadata">

**Author:** ![bmansour](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bmansour](https://discourse.nodered.org/u/bmansour)\
**Post date:** [27 September 2019 09:51 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/5 "2019-09-27T09:51:45Z")

</div>

For some reason `node-red-contrib-fs` did not give me any output? I had a debug node set to `msg.payload` but nothing on debug console ☹

Not sure why that is but I found another useful package that does the same `node-red-contrib-fs-ops` which installed a couple of extra other nodes that are useful for local filesystem operations.

The `fs-ops-dir` node did its thing and returned an array of strings with the file names in the specific directory. I'll now try manipulate this array to create array of paths to feed upstream to email node in attachments.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [27 September 2019 09:55 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/6 "2019-09-27T09:55:35Z")

</div>

what pattern/directory had you have `node-red-contrib-fs`'s file-lister look at? Based on your initial post it should be something like `/home/ubuntu/logs/*.csv`

---

<div class="post-metadata">

**Author:** ![bmansour](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bmansour](https://discourse.nodered.org/u/bmansour)\
**Post date:** [27 September 2019 09:58 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/7 "2019-09-27T09:58:13Z")

</div>

I had it point at simply `/home/ubuntu/logs/` based on the initial post, which does not work. I'll give what you suggest a try now..

---

<div class="post-metadata">

**Author:** ![bmansour](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bmansour](https://discourse.nodered.org/u/bmansour)\
**Post date:** [27 September 2019 10:04 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/8 "2019-09-27T10:04:28Z")

</div>

Changed the pattern from `*` to `*.csv` while keeping directory to `/home/ubuntu/logs/`. Still nothing on debug.

---

<div class="post-metadata">

**Author:** ![bmansour](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bmansour](https://discourse.nodered.org/u/bmansour)\
**Post date:** [27 September 2019 10:13 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/9 "2019-09-27T10:13:44Z")

</div>

Would have liked the `node-red-contrib-fs` file-lister to work since it has the "include full path in output" checkbox which I guess would have returned the full path. Instead I'll have to add a function node to build paths seeing that fs-ops-dir node of `node-red-contrib-fs-ops` only returns array of files and does not have the option to return full path 😔

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [27 September 2019 10:56 UTC](https://discourse.nodered.org/t/email-multiple-files-as-attachments-dynamically-in-email-node/15968/10 "2019-09-27T10:56:02Z")

</div>

I’ll take a look at it later, I noticed this morning I made an error in a Pull Req I placed that got accepted, so I’ll be diving in the code either way.
