# How can I optimise my email code

**URL:** <https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270>\
**Category:** General\
**Created:** [17 January 2021 16:01 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270 "2021-01-17T16:01:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![robo7795](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@robo7795](https://discourse.nodered.org/u/robo7795)\
**Post date:** [17 January 2021 16:01 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/1 "2021-01-17T16:01:13Z")

</div>

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

```auto
[{"id":"e56d7627.05dcc8","type":"e-mail","z":"95c84ff6.b3002","server":"smtp.gmail.com","port":"465","secure":true,"tls":true,"name":"recipient@mail.com","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"]]}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 January 2021 20:32 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/2 "2021-01-17T20:32:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robo7795](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@robo7795](https://discourse.nodered.org/u/robo7795)\
**Post date:** [18 January 2021 11:52 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/3 "2021-01-18T11:52:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 January 2021 12:18 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/4 "2021-01-18T12:18:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robo7795](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@robo7795](https://discourse.nodered.org/u/robo7795)\
**Post date:** [18 January 2021 14:55 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/5 "2021-01-18T14:55:09Z")

</div>

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

![Like this](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a24d7c34159a65131e1800a727054fd7a1590a4b.jpeg)

 ![csv File in Mail text](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/4/e415e427066828614f6bfff9eceadba01b7a3773.jpeg)

```auto
[{"id":"e56d7627.05dcc8","type":"e-mail","z":"95c84ff6.b3002","server":"smtp.gmail.com","port":"465","secure":true,"tls":true,"name":"recipient@mail.com","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"]]}]

```

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [18 January 2021 15:52 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/6 "2021-01-18T15:52:22Z")

</div>

Take a look here : [Email with 3 attachments? - #3 by softy2k](https://discourse.nodered.org/t/email-with-3-attachments/28110/3)

---

<div class="post-metadata">

**Author:** ![robo7795](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@robo7795](https://discourse.nodered.org/u/robo7795)\
**Post date:** [18 January 2021 16:35 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/7 "2021-01-18T16:35:50Z")

</div>

hey,  
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?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 January 2021 16:53 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/8 "2021-01-18T16:53:15Z")

</div>

If your using a `joint` it might be hard 🤣

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

 ![Screen Shot 2021-01-18 at 11.52.12 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a149823a485fd087dcb35d3223d47f760b540f8e.png)

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.

---

<div class="post-metadata">

**Author:** ![robo7795](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@robo7795](https://discourse.nodered.org/u/robo7795)\
**Post date:** [18 January 2021 18:42 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/9 "2021-01-18T18:42:15Z")

</div>

Thank you for your patience

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 February 2021 18:42 UTC](https://discourse.nodered.org/t/how-can-i-optimise-my-email-code/39270/10 "2021-02-01T18:42:28Z")

</div>

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