# Saving only csv email attachments

**URL:** <https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650>\
**Category:** General\
**Created:** [2 May 2024 08:30 UTC](https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650 "2024-05-02T08:30:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![E-J-D](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@E-J-D](https://discourse.nodered.org/u/E-J-D)\
**Post date:** [2 May 2024 08:30 UTC](https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650/1 "2024-05-02T08:30:55Z")

</div>

Hi,

I also fetch mails with Node-RED and try to save the "CSV" attachments included. I would appreciate if @stephan1827, @Fed or any other here would share a flow where this is realised. Thanks in advance.

Unfortunately I didn't find a working solution here an over the web.

Cheers,  
Eike

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 May 2024 10:04 UTC](https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650/2 "2024-05-02T10:04:05Z")

</div>

Here is an email as shown in the debug panel.  
You can see that it has just one attachment (msg.attachments[0] of type "text/csv" but msg.attachments[0].content is a buffer.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/499a0197d0597d11227d66fe810373f939c98dc0.png)

I use a change node to move msg.attachments[0].content to msg.payload and then a function node

```auto
msg.payload = msg.payload.toString()
return msg;

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/4/6453d10a7551cea42f2c471bb5d35ef3d4a8f145.png)

And the result:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/285ae31c2f1ee04a2c8155f7d9f5f83fc9369da9.png)

---

<div class="post-metadata">

**Author:** ![E-J-D](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@E-J-D](https://discourse.nodered.org/u/E-J-D)\
**Post date:** [2 May 2024 10:31 UTC](https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650/3 "2024-05-02T10:31:26Z")

</div>

Hi @jbudd, thanks for your help. I tried it out but get this result. I checked the attachment and it is UTF-8. Seems to be an encoding problem. Do you habe any idea how to handle?

![Clipboard_05-02-2024_1228](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fbf8b8a9f92fef5da1ae1999b3c93de60ef62556.png)

Did you check if it possible to save the CSV straight with the "write file" node?

Cheers  
Eike

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 May 2024 11:46 UTC](https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650/5 "2024-05-02T11:46:10Z")

</div>

I changed my above post. Move msg.attachments[0].content to msg.payload.  
Thanks for pointing it out 😀

---

<div class="post-metadata">

**Author:** ![E-J-D](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@E-J-D](https://discourse.nodered.org/u/E-J-D)\
**Post date:** [2 May 2024 11:48 UTC](https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650/6 "2024-05-02T11:48:34Z")

</div>

Sorry to confuse... I tried "msg.attachments[0].content to payload" which is wrong as my CSV attachement is msg.attachments[1].content. Now it works. Thank you.

The mails I receive sometimes include an JPG (ad for something), it would be great to have the possibility to handle only CSV attachments. Do you have any idea for that. Sorry for this low knowledge questions as I am not really a coder.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 May 2024 12:16 UTC](https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650/7 "2024-05-02T12:16:57Z")

</div>

Here is an email with two attachments.  
msg.attachments[0].contentType is "image/jpeg"  
msg.attachments[1].contentType is "text/csv"

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea71c5645f55039eb3e1832e2231f88b14b39366.png)

You clearly need some way to process only the right emails - perhaps checking msg.from and msg.topic.  
Then loop through the attachments and only process the ones where contentType is "text/csv".  
Like this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/9/0978a77b1d346df73f1ee18b83940a94fb2d5556.png)

```auto
[{"id":"f2efd1609dfc08bd","type":"e-mail in","z":"a2a74143.03d2d","name":"","protocol":"IMAP","server":"imap.gmail.com","useSSL":true,"autotls":"never","port":"993","authtype":"BASIC","saslformat":true,"token":"oauth2Response.access_token","box":"INBOX","disposition":"Read","criteria":"UNSEEN","repeat":"300","fetch":"trigger","inputs":1,"x":250,"y":300,"wires":[["0f74c54a42d3828a","43ab469a088e0897"]]},{"id":"0f74c54a42d3828a","type":"debug","z":"a2a74143.03d2d","name":"debug 61","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":440,"y":300,"wires":[]},{"id":"56896402d523cfa6","type":"inject","z":"a2a74143.03d2d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":300,"wires":[["f2efd1609dfc08bd"]]},{"id":"43ab469a088e0897","type":"change","z":"a2a74143.03d2d","name":"","rules":[{"t":"move","p":"attachments","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":170,"y":360,"wires":[["867385526ba08092"]]},{"id":"d1e9b080d7de1a22","type":"debug","z":"a2a74143.03d2d","name":"debug 63","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":420,"wires":[]},{"id":"a1b6547d78b8858a","type":"function","z":"a2a74143.03d2d","name":"function 59","func":"msg.payload = msg.payload.toString()\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":420,"wires":[["d1e9b080d7de1a22"]]},{"id":"867385526ba08092","type":"split","z":"a2a74143.03d2d","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":370,"y":360,"wires":[["87f004ecc5981adf","5135c48c46ab6c76"]]},{"id":"87f004ecc5981adf","type":"switch","z":"a2a74143.03d2d","name":"type text/csv?","property":"payload.contentType","propertyType":"msg","rules":[{"t":"eq","v":"text/csv","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":540,"y":360,"wires":[["74d372e55bfea32f"]]},{"id":"74d372e55bfea32f","type":"change","z":"a2a74143.03d2d","name":"","rules":[{"t":"move","p":"payload.content","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":420,"wires":[["a1b6547d78b8858a"]]},{"id":"5135c48c46ab6c76","type":"debug","z":"a2a74143.03d2d","name":"debug 64","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":320,"wires":[]}]

```

Because msg.attachments is an array, the split node will send each array element as a separate msg.payload. Then you can test msg.payload.contentType and process msg.payload.content.

---

<div class="post-metadata">

**Author:** ![E-J-D](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@E-J-D](https://discourse.nodered.org/u/E-J-D)\
**Post date:** [2 May 2024 13:36 UTC](https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650/8 "2024-05-02T13:36:38Z")

</div>

Thanks @jbudd! That's exactly what I need! You made my day 🥳🌞.

---

<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:** [31 July 2024 13:37 UTC](https://discourse.nodered.org/t/saving-only-csv-email-attachments/87650/9 "2024-07-31T13:37:07Z")

</div>

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