Need help in sending image attachments on email

I need help on sending image attachment on email.
So far i can only send 1 image.
I tried making an array of buffered 2 image as content with their file name on msg.attachments and shows only 1 image on my inbox.

here is what I'm passing through the Email node

Text : msg : Object
object
_msgid: "a21752c86d7ba433"
payload: "Test"
topic: "Text"
attachments: array[2]
0: object
filename: "image.jpg"
content: buffer[786328]
1: object
filename: "image2.jpg"
contnent: buffer[898011]
type or paste code her

Here is my function code


msg.attachments =  [
    { filename : "image.jpg",
        content : msg.payload[0] },
    {
      filename : "image2.jpg",
      contnent : msg.payload[1]
    }
    
    ] ;  

msg.payload = "Test";
return msg;

here is my example node

[{"id":"5b50a033058e84ac","type":"inject","z":"f7d9d380383efc2a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Text","payload":"Test","payloadType":"str","x":2460,"y":800,"wires":[["41fe3747e2aac1bb","a10d4054ff202f35"]]},{"id":"41fe3747e2aac1bb","type":"file in","z":"f7d9d380383efc2a","name":"","filename":"/home/pi/Uga/image.jpg","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":2670,"y":800,"wires":[["10ccf85ac0a068a3"]]},{"id":"10ccf85ac0a068a3","type":"join","z":"f7d9d380383efc2a","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":2890,"y":820,"wires":[["503e46ac8df788d8"]]},{"id":"a10d4054ff202f35","type":"file in","z":"f7d9d380383efc2a","name":"","filename":"/home/pi/Uga/image2.jpg","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":2670,"y":840,"wires":[["10ccf85ac0a068a3"]]},{"id":"503e46ac8df788d8","type":"change","z":"f7d9d380383efc2a","name":"","rules":[{"t":"delete","p":"filename","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3080,"y":820,"wires":[["e213c59b64f2007f"]]},{"id":"e213c59b64f2007f","type":"function","z":"f7d9d380383efc2a","name":"","func":"\nmsg.attachments =  [\n    { filename : \"image.jpg\",\n        content : msg.payload[0] },\n    {\n      filename : \"image2.jpg\",\n      contnent : msg.payload[1]\n    }\n    \n    ] ;  \n\nmsg.payload = \"Test\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2960,"y":920,"wires":[["ae03728b0b94faae","33419b2dd2bc6002"]]},{"id":"33419b2dd2bc6002","type":"e-mail","z":"f7d9d380383efc2a","server":"smtp.gmail.com","port":"465","secure":true,"tls":true,"name":"XXZ@gmail.com","dname":"Send Email","credentials":{"userid":"","password":""},"x":3190,"y":920,"wires":[]},{"id":"ae03728b0b94faae","type":"debug","z":"f7d9d380383efc2a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":3170,"y":1000,"wires":[]}]

Only a guess: Try changing 'contnent' to 'content' on line 6

LOL .. I was laughing so hard after finding this.. (how could I miss this)

my only problem is that it looks like the name have been swap.
I'm planning to send 12 images later.

I only used the join function, and added a change function to delete the extra "filename"
is there a better solution in joining image files into array?

Would it be easier to put the filenames in the attachment details rather than the image itself, or doesn't that work?

Problem is, I'm using a "file in" to load the image from a folder. Im using a single inject function to load both image simultaneously on a join function. how can i tell which of the 2 payloads are which after joining them..

But i do already have the images on a context flow in base 64, i wonder if i can just use that?

Hi,

Try using the image tools:node-red-contrib-image-tools (node) - Node-RED

This would let you merge multiple images into one and also put text over them to help you identify images clearly.

Cheers,

Paul

Feed the inject into one file node and after that use a Change node to Move the payload to msg.image1 (for example), then pass the message on to the second file node , then you will have one image in msg.image1 and one in the payload. The second file not should not mess with msg.image1

I will use this method if there is like nothing else left, But this give me the idea to use flow.image instead since I already have this images loaded on a context.

Problem is, How do I convert the image from base64 to buffer on function node?


Since I already have the images inside flow.image1, flow.image2 etc.

This is my solution for now and it is working. is there a way that I could just write the whole thing in function.
the only problem I'm facing is I need to convert the images from Base64 back to Buffer, if I could just do it inside the function.

If there is none, I'll try to work around by saving the images as Buffer and just around of converting it to Base 64 if I needed too. But this will have to wait for a week for some reasons I cant work with it right now.

any way, thanks for the idea
here is the code of the function


msg.attachments =  [
    { filename : "image.jpg",
        content : msg.image1 },
    {
      filename : "image2.jpg",
      content : msg.image3
    },
    {
        filename:"image3.jpg",
        content : msg.image4// flow.image2 is broken
    }
    
    ] ;  

msg.payload = "Test";

delete msg.image1;
delete msg.image3;
delete msg.image4;

return msg;

Since you already have the images as base64 maybe you dont have to convert them ..
havent tried it but by reading the Nodemailer library website on which the email node is based on .. it gives an example how to define the attachment if it's already base64

{   // encoded string as an attachment
            filename: 'text1.txt',
            content: 'aGVsbG8gd29ybGQh',
            encoding: 'base64'
        },

since you have them in Context
for content you add for example content: flow.get('image1'),

2 Likes

Nice, we have this option. Gona try this next week.

Just tried it and it work, Thanks man

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