You can use the Node-RED email node package to send emails with attachments as soon as it’s installed, but there are a few small details that are critical in getting it to work smoothly. In this post I’ll go over a basic flow to read in a specific file on a regular schedule and email that file out as an email attachment to yourself or other recipients:
This flow was written using Node-RED v1.0.3 on groov hardware, but this technique will work for many Node-RED versions running on other Linux or even Windows systems after some small modifications.
The first key is the format of the attachment JSON object, which according to the email node info tab must follow the nodemailer format. In this case I only have one attachment, so I provide just one file JSON object that holds the file name and file content, following this basic format template:
msg.attachments =
{ filename : "name_of_attachment.txt", // name of the file that will be attached
content : msg.payload }; // content should be a file binary buffer
The second key part of this flow is to get the msg.payload
“binary buffer”. To do that I just need to read the data in using the pre-installed file node. For the simplicity of this example, this file is hard coded in the file node (I’ll share an example of variable file names in the thread below):
The most important things here are that I include the entire file path, and that I return a single buffer object that will then be written to msg.payload
and handed in as the attachment content in the next function node.
Once this msg.attachment
object is made I just need to set the email subject in msg.topic
, create a new email body in msg.payload
, and configure the email-write node. I can handle most of this in the function node, and can even add the day and time to the email subject. In this example I use the end of the file path to get the file name that will be sent in the email – this way a file path of /home/dev/unsecured/hello_world.txt
will automatically name the file attachment hello_world.txt
Note: If you are using gmail, make sure you set up the sending account with an application password. Because of this requirement I recommend using an email account separate from your personal or work email accounts just for Node-RED.
The flow JSON text is pasted below if you want to import this and try it for yourself. Just make sure you install the “node-red-node-email” package before importing this flow (since the email node is not a preinstalled core node as of Node-RED v1.0+).
Please feel free to modify this for your needs and don’t hestitate to post any questions or suggestions to this thread.
And as always, happy coding!
[{"id":"7f372dd4.9ac984","type":"file in","z":"eedf3627.4e1558","name":"get file","filename":"/home/dev/unsecured/hello_world.txt","format":"","chunk":false,"sendError":false,"encoding":"none","x":430,"y":1760,"wires":[["892f85fd.ddf898"]]},{"id":"892f85fd.ddf898","type":"function","z":"eedf3627.4e1558","name":"write email","func":"file = msg.filename; // create local file variable for convenient reference\nvar d = new Date(); // create current date object for the time string\nvar tstring = d.toString().substring(0,4) + d.toString().substring(15,21);\n\nmsg.attachments =\n { filename : file.substring(file.lastIndexOf('/')+1,file.length),\n content : msg.payload }; // content should be a file binary buffer\n \nmsg.topic = \"Your Daily Report for \" + tstring; // email subject\n\nmsg.payload = \"See attached text file: `\" + msg.attachments.filename + \"`\"; // email body\n\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":1760,"wires":[["3d2eae0.1c8b452"]]},{"id":"e56d7627.05dcc8","type":"e-mail","z":"eedf3627.4e1558","server":"smtp.gmail.com","port":"465","secure":true,"tls":true,"name":"recipient@mail.com","dname":"","x":830,"y":1760,"wires":[]},{"id":"5fb69172.b3471","type":"inject","z":"eedf3627.4e1558","name":"6:00PM","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 18 * * *","once":false,"onceDelay":0.1,"x":280,"y":1760,"wires":[["7f372dd4.9ac984"]]},{"id":"3d2eae0.1c8b452","type":"debug","z":"eedf3627.4e1558","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":1720,"wires":[]}]