Saving only csv email attachments

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

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.

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

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

And the result:
image

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

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

Cheers
Eike

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

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.

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

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

[{"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.

Thanks @jbudd! That's exactly what I need! You made my day :partying_face::sun_with_face:.

1 Like

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