Q: Insert images into body of email?

Hey team,

I have a flow that attaches 3 jpg images to an email. I would much prefer those images were inserted into the message body. (code included below)

How would I go about changing that, please?

Thank you in advance,
Dax.

[{"id":"d0ab4ca9.10dcd","type":"tab","label":"<<Sandbox>>","disabled":false,"info":""},{"id":"e7ef36ed.77e238","type":"exec","z":"d0ab4ca9.10dcd","command":"/usr/bin/ffmpeg -y -i rtsp://USER:PW@IP:PORT/VIDEOSTREAM.264 -vframes 1 -qscale:v 2","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Grab a frame -> jpg","x":430,"y":320,"wires":[["c1dc64d3.4d0f78"],[],[]]},{"id":"db598c22.260ce","type":"e-mail","z":"d0ab4ca9.10dcd","server":"MAIL_SERVER","port":"465","secure":true,"tls":true,"name":"EMAIL_ADDRESS","dname":"Send email","x":930,"y":300,"wires":[]},{"id":"c1dc64d3.4d0f78","type":"function","z":"d0ab4ca9.10dcd","name":"Compose email 3","func":"// only run this if the topic contains 'email', so other types do not get emailed\nif (msg.topic.indexOf(\"3mail\") !== -1) {\n\n    if (msg.topic.indexOf(\"/1\") !== -1) {\n        context.set(\"file1\", msg.path + msg.file);\n    }\n    if (msg.topic.indexOf(\"/2\") !== -1) {\n        context.set(\"file2\", msg.path + msg.file);\n    }\n    if (msg.topic.indexOf(\"/3\") !== -1) {\n        msg.file3 = msg.path + msg.file;\n        msg.file1 = context.get(\"file1\");\n        msg.file2 = context.get(\"file2\");\n        msg.payload = msg.file1 + \" \" + msg.file2 + \" \" + msg.file3;\n        node.status({fill:\"blue\",shape:\"ring\",text:\"Last update: \"+msg.formatteddate});\n        return msg;\n    }\n    \n}","outputs":1,"noerr":0,"x":690,"y":300,"wires":[["5735b3fe.1263cc"]]},{"id":"5735b3fe.1263cc","type":"change","z":"d0ab4ca9.10dcd","name":"Set up email (x3)","rules":[{"t":"set","p":"attachments","pt":"msg","to":"[{\t    \"filename\": \"Image1.jpg\", \t    \"path\": msg.file1,\t    \"content\": $$.payload\t},\t{\t    \"filename\": \"Image2.jpg\", \t    \"path\": msg.file2,\t    \"content\": $$.payload\t},\t{\t    \"filename\": \"Image3.jpg\", \t    \"path\": msg.file3,\t    \"content\": $$.payload\t}]","tot":"jsonata"},{"t":"set","p":"from","pt":"msg","to":"[ { \"address\": \"EMAILADDRESS\", \"name\": \"RPi Server\" } ]","tot":"json"},{"t":"set","p":"topic","pt":"msg","to":"EMAIL_SUBJECT","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"EMAIL_CONTENT","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":360,"wires":[["db598c22.260ce"]]},{"id":"f9a140de.9817e","type":"inject","z":"d0ab4ca9.10dcd","name":"Email 3 pics","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"1","payloadType":"num","x":70,"y":100,"wires":[["5a1649e9.abc528"]]},{"id":"5a1649e9.abc528","type":"function","z":"d0ab4ca9.10dcd","name":"1st img","func":"var now = new Date();\n// Create formatted time\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\nconst trigger = parseInt(msg.payload)\nif (trigger == 1) {\n\n// Last update: \"+dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss});\n\n// file path with / at the end\nvar path = \"/home/pi/node-red-static/\";                     // This is the path\nvar filename = \"frame_\"+yyyy+mm+dd+\"-\"+hh+mmm+ss+\".jpg\";     // file name\nmsg.payload = path + filename;                              // pass the full path to payload for the exec node to add to the end of the command\nmsg.file = filename;                                        // To be used later to store the information in the DB\nmsg.path = path;                                            // Same as above\nmsg.wwwpath = \"/\";                                          // Same as above\nmsg.topic = \"3mail/1|delete\";                               // Flag to store this image in the DB\nmsg.type = \"temp\";                                          // Image type e.g. Front camera, etc.\nmsg.epoch = now.getTime();                                  // Current timestamp\nmsg.formatteddate = dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss;   // Formatted timestamp to be used later\n\n  return msg;}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":240,"y":100,"wires":[["80515e48.4bbc1"]]},{"id":"80515e48.4bbc1","type":"delay","z":"d0ab4ca9.10dcd","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":80,"y":160,"wires":[["454a113e.d58d1"]]},{"id":"454a113e.d58d1","type":"function","z":"d0ab4ca9.10dcd","name":"2st img","func":"var now = new Date();\n// Create formatted time\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// Last update: \"+dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss});\n\n// file path with / at the end\nvar path = \"/home/pi/node-red-static/\";                     // This is the path\nvar filename = \"frame_\"+yyyy+mm+dd+\"-\"+hh+mmm+ss+\".jpg\";     // file name\nmsg.payload = path + filename;                              // pass the full path to payload for the exec node to add to the end of the command\nmsg.file = filename;                                        // To be used later to store the information in the DB\nmsg.path = path;                                            // Same as above\nmsg.wwwpath = \"/\";                                          // Same as above\nmsg.topic = \"3mail/2|delete\";                               // Flag to store this image in the DB\nmsg.type = \"temp\";                                          // Image type e.g. Front camera, etc.\nmsg.epoch = now.getTime();                                  // Current timestamp\nmsg.formatteddate = dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss;   // Formatted timestamp to be used later\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":240,"y":160,"wires":[["1123bba9.6008d4"]]},{"id":"1123bba9.6008d4","type":"delay","z":"d0ab4ca9.10dcd","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":80,"y":220,"wires":[["8efb23b5.964b"]]},{"id":"8efb23b5.964b","type":"function","z":"d0ab4ca9.10dcd","name":"3rd img","func":"var now = new Date();\n// Create formatted time\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// Last update: \"+dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss});\n\n// file path with / at the end\nvar path = \"/home/pi/node-red-static/\";                     // This is the path\nvar filename = \"frame_\"+yyyy+mm+dd+\"-\"+hh+mmm+ss+\".jpg\";     // file name\nmsg.payload = path + filename;                              // pass the full path to payload for the exec node to add to the end of the command\nmsg.file = filename;                                        // To be used later to store the information in the DB\nmsg.path = path;                                            // Same as above\nmsg.wwwpath = \"/\";                                          // Same as above\nmsg.topic = \"3mail/3|delete\";                               // Flag to store this image in the DB\nmsg.type = \"temp\";                                          // Image type e.g. Front camera, etc.\nmsg.epoch = now.getTime();                                  // Current timestamp\nmsg.formatteddate = dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss;   // Formatted timestamp to be used later\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":240,"y":220,"wires":[["e7ef36ed.77e238"]]}]

Use html.

See this

1 Like

Thanks very much, Steve! I will check that out.

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