Any attachments supplied in the incoming email can be found in the msg.attachments
property. This will be an array of objects where each object represents a specific attachments. The format of the object is:
{ contentType: // The MIME content description fileName: // A suggested file name associated with this attachment transferEncoding: // How was the original email attachment encodded? contentDisposition: // Unknown generatedFileName: // A suggested file name associated with this attachment contentId: // A unique generated ID for this attachment checksum: // A checksum against the data length: // Size of data in bytes content: // The actual content of the data contained in a Node.js Buffer object // We can turn this into a base64 data string with content.toString('base64') }
The image arrives as a Buffer in the message payload (= $$.payload in Jsonata).
Flow 4 - attachment from filesystem via Jsonata expression
The previous 3 flows all work via memory: the image is passed as Buffer in the message payload.
But sometimes it could be necessary (for some reason) to store the file on the filesystem, and extra pass the file path to the mail node:
Here is the flow:
[{"id":"b86e45d.cc89ab8","type":"function","z":"d7a683c8.03b5e","name":"Flow 1 : Setup mail content","func":"msg.topic = \"Result flow 1\";\n\nmsg.payload = \"Dear,<br><br>Your PIR detector has detected movement in the living room.<br>In attachment you can find a camera snapshot.<br><br>Kind regards,<br>Your Node-Red flow\";\n\nmsg.attachments = [{\n filename: 'camera_image.jpg',\n path: '/tmp/local_image.jpg',\n content: msg.payload\n}];\n \nreturn msg;","outputs":1,"noerr":0,"x":651.0078392028809,"y":619.5977964401245,"wires":[["17cbd320.cff89d"]]},{"id":"bb8506f8.735298","type":"inject","z":"d7a683c8.03b5e","name":"Start","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":240.76563835144043,"y":621.0000143051147,"wires":[["6bb70234.e0626c"]]},{"id":"19678643.ff7fca","type":"change","z":"d7a683c8.03b5e","name":"Flow 2 : Setup mail content","rules":[{"t":"set","p":"attachments","pt":"msg","to":"[{\t \"filename\": 'camera_snapshot.jpg', \t \"content\": $$.payload\t}]","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"Result flow 2","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Dear,<br><br>Your PIR detector has detected movement in the living room.<br>In attachment you can find a camera snapshot.<br><br>Kind regards,<br>Your Node-Red flow","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":648.7656211853027,"y":681.0000905990601,"wires":[["17cbd320.cff89d"]]},{"id":"17cbd320.cff89d","type":"e-mail","z":"d7a683c8.03b5e","server":"your_smpt_server","port":"465","secure":true,"name":"your_smpt_user","dname":"Send mail","x":883.765739440918,"y":619.0000290870667,"wires":[]},{"id":"6bb70234.e0626c","type":"http request","z":"d7a683c8.03b5e","name":"Get image","method":"GET","ret":"bin","url":"https://storage.googleapis.com/relevant-magazine/2017/06/burglar-alamy.jpg","tls":"","x":397.01172256469727,"y":620.2890768051147,"wires":[["19678643.ff7fca","b86e45d.cc89ab8","a1d132a8.6c848","e21291c9.58d5c","4cc18e6f.53a4e"]]},{"id":"a1d132a8.6c848","type":"change","z":"d7a683c8.03b5e","name":"Flow 3 : Setup mail content","rules":[{"t":"set","p":"attachments","pt":"msg","to":"[{\t \"filename\": 'camera_snapshot.jpg', \t \"content\": $$.payload,\t \"cid\": 'some_unique_file_id'\t}]","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"Result flow 3","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Dear,<br><br>Your PIR detector has detected movement in the living room.<br>Here is your camera snapshot:<br><img src=\"cid:some_unique_file_id\"/>',<br><br>Kind regards,<br>Your Node-Red flow","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":649.7656555175781,"y":743.5000762939453,"wires":[["17cbd320.cff89d"]]},{"id":"4cc18e6f.53a4e","type":"change","z":"d7a683c8.03b5e","name":"Flow 4 : Setup mail content","rules":[{"t":"set","p":"attachments","pt":"msg","to":"[{\t \"filename\": 'camera_snapshot.jpg', \t \"path\": '/tmp/local_image.jpg',\t \"content\": $$.payload\t}]","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"Result flow 4","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Dear,<br><br>Your PIR detector has detected movement in the living room.<br>In attachment you can find the file that was locally stored.<br><br>Kind regards,<br>Your Node-Red flow","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":646.7656631469727,"y":855.7501220703125,"wires":[["17cbd320.cff89d"]]},{"id":"e21291c9.58d5c","type":"file","z":"d7a683c8.03b5e","name":"Save image file","filename":"/tmp/local_image.jpg","appendNewline":false,"createDir":false,"overwriteFile":"true","x":607.0039119720459,"y":817.035400390625,"wires":[]}]