Email multiple files as attachments dynamically in email node

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.

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:

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.

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.

1 Like

@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!

I found and used it recently myself :slight_smile:

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 :frowning_face:

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.

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

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..

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

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 :pensive:

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.

1 Like