How can I optimise my email code

can someone help me to optimize my code ? I want to send CSV files by email, at the moment I solve this with a join node, is there also a way to send it without join node?

I have added my code.

[{"id":"e56d7627.05dcc8","type":"e-mail","z":"95c84ff6.b3002","server":"","port":"465","secure":true,"tls":true,"name":"","dname":"","x":950,"y":1500,"wires":[]},{"id":"b2f0df20.8ae7","type":"inject","z":"95c84ff6.b3002","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":200,"y":1500,"wires":[["5acda1ca.3d834"]]},{"id":"5acda1ca.3d834","type":"file in","z":"95c84ff6.b3002","name":"","filename":"E:\\NEU.csv","format":"","chunk":false,"sendError":false,"encoding":"none","x":390,"y":1500,"wires":[["2c026326.9e133c"]]},{"id":"2c026326.9e133c","type":"join","z":"95c84ff6.b3002","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":570,"y":1500,"wires":[["66fac98b.2142c8"]]},{"id":"66fac98b.2142c8","type":"function","z":"95c84ff6.b3002","name":"Prepare Email","func":"// Get the current time and convert it to text\nvar now = new Date();\nvar yyyy = now.getFullYear();\nvar mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd  = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mmm  = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss  = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\n\n//Filegenerator\nvar data = \"Wasseraufbereitungsanlage_\"+dd+\"_\"+mm+\"_\"+yyyy+\".csv\";\n\n\nmsg.attachments = [{filename: data, content: msg.payload[0]}];\n\n\nmsg.payload = \"Email Body Message\";\n\nmsg.topic = \"Subject Message\";\n\n\n\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":740,"y":1500,"wires":[["e56d7627.05dcc8"]]}]
  1. Why are you reading the file as a buffer?
  2. why are you creating a array out of the single buffer - i.e. a one row array that is the buffer that you read in.

I am relatively new to Node-Red so I copied the code from the forum that describes how to send images and modified it to send csv files.
Can you please send me an example of the easiest way to send the CSV files by e-mail?

An image is different than a text file. Go read the help tab for the file-in node to see the options. Get rid of the join node and add some debug nodes o you can see what is happening.

In addition you should spend a hour watching the node-red essential videos on YouTube. The time will be well worth it

I can send the file but (see picture) but how can I make it send the file as attachment ?

Like this

[{"id":"e56d7627.05dcc8","type":"e-mail","z":"95c84ff6.b3002","server":"","port":"465","secure":true,"tls":true,"name":"","dname":"","x":930,"y":1420,"wires":[]},{"id":"79d99799.5e6318","type":"debug","z":"95c84ff6.b3002","name":"test","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1210,"y":1460,"wires":[]},{"id":"b2f0df20.8ae7","type":"inject","z":"95c84ff6.b3002","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":200,"y":1500,"wires":[["5acda1ca.3d834"]]},{"id":"5acda1ca.3d834","type":"file in","z":"95c84ff6.b3002","name":"","filename":"E:\\NEU.csv","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":450,"y":1480,"wires":[["6f88f17b.c184b"]]},{"id":"6f88f17b.c184b","type":"function","z":"95c84ff6.b3002","name":"","func":"file = msg.filename \n\nmsg.attachments = {filename: file , content: msg.payload};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":1480,"wires":[["79d99799.5e6318","e56d7627.05dcc8"]]}]

Take a look here : Email with 3 attachments? - #3 by softy2k

1 Like

thanks for your answer I had the code all the way too. My question now was whether you can send CSV or text files as attachments to the email without joint?

If your using a joint it might be hard :rofl:

One thing you should always do is read the Help tab for a node where you can find some very valuable information.

So as you can see, you don't need a join node, use a change node to setup msg.attachments.

If you run into a problem, provide your flow showing what you tried and what is going wrong.

Thank you for your patience

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.