Sending attachments to email through node red

7,066 views
Skip to first unread message

Jacob Lewis

unread,
Sep 16, 2016, 5:59:20 AM9/16/16
to Node-RED
Hey all,

I have set up a flow that will get a camera on the Pi to take a picture and save it as test in a JPEG format. The save location is /home/pi/picture/
I then have another separate flow that tries to grab the photo from the location where it is saved and then send it out using an email node.
The email will successfully send and an attachment will go with it but the attachment does not contain anything.
Is there any way to send an attachment using a function node. I will post the code I have used below.

Many thanks
Attachments function.PNG

Zenofmud

unread,
Sep 16, 2016, 6:03:41 AM9/16/16
to node...@googlegroups.com
It looks like you must put the content into ‘content’ part of the array as a Node.js Buffer object


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')
}
 
On Sep 16, 2016, at 5:59 AM, Jacob Lewis <jake.l...@gmail.com> wrote:

<Attachments function.PNG>

Mark Setrem

unread,
Sep 16, 2016, 6:18:29 AM9/16/16
to Node-RED
Not tried it with a buffer but it works with html/text files files.

Here's what I am using within a function node to create the msg.attachments array for adding two files

msg.attachments = [{   // utf-8 string as an attachment 
            filename: 'CrypticCrossword'+msg.timestamp+'.html',
            content: msg.xwordHTML
        },{   // utf-8 string as an attachment 
            filename: 'ANSWERS'+msg.timestamp+'.html',
            content: msg.xwordAnswershtml
        }
        ];

Jacob Lewis

unread,
Sep 16, 2016, 7:03:12 AM9/16/16
to Node-RED
I copied the code you entered and tried to send a text file as an attachment.
The email will send but the attachment will contain no data.
I assume that its not getting the files from the Pi and is just sending the filename as an empty attachment.
I will post screenshots of the code I have used and what the email contains.

email.PNG
attachment.PNG
code.PNG

Mark Setrem

unread,
Sep 16, 2016, 7:18:39 AM9/16/16
to Node-RED
I don't see "path" in either Pauls examples or mine.  
I'm not sure that the email node attachment will pick up a file from disk itself. 

If you are saving the image to disk using node-red rather than doing it that way keep the image within the msg object and then reference that in msg.attachment
 ( e.g.  content: msg.xwordHTML)

If you want to have a separate flow, have you tried loading the image using the "file in" node and then passing that via a function node to fill out msg.attachments prior to the "email out" node

eg   [Inject] --- [file in] ---[function node to configure msg.attachment]---[email out]

Jacob Lewis

unread,
Sep 16, 2016, 7:19:43 AM9/16/16
to Node-RED
Figured out what I was doing wrong. Where I have the path you need to add the name of the file you are sending at the end.
Thanks for the help.

ravi teja

unread,
Jun 30, 2017, 2:37:45 AM6/30/17
to Node-RED
can i know what was u r mistake?
and how did u  correct it?

Bart Butenaers

unread,
Aug 20, 2017, 5:26:38 AM8/20/17
to Node-RED
Hey Ravi,

this is an old post, but I did some experiments yesterday for sending camera snapshot images via email.
Perhaps this can be useful for other people also ...

In the following test flow I used 4 different setups to send the mail:


Flow 1 - attachment via function node
The first flow sends the image as attachment to the mail.
Function nodes are very useful, but when I prefer to use out-of-the-box nodes (or contributions). 
Therefore the following next 3 approaches will use Jsonata expressions.

Flow 2 - attachment via Jsonata expression
Same result as in the previous flow, but now the mail attachment array is setup using a simple Jsonata expression:


The image arrives as a Buffer in the message payload (= $$.payload in Jsonata).


Flow 3 - embedded image via Jsonata expression
In this flow the image is not added as an attachment, but embedded inside the mail body as image (tag).
This is achieved by using the same CID in both the mail body (string) and mail attachment (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":[]}]

And here are the results of all flows:


Kind regards,
Bart Butenaers

Dave C-J

unread,
Aug 20, 2017, 9:18:04 AM8/20/17
to node...@googlegroups.com
Bart  - very nice - you should turn this into a cookbook entry.
Reply all
Reply to author
Forward
0 new messages