# Need help in sending image attachments on email

**URL:** <https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226>\
**Category:** General\
**Created:** [12 February 2022 15:15 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226 "2022-02-12T15:15:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![XhanceHd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhancehd/32/48135_2.png) [@XhanceHd](https://discourse.nodered.org/u/XhanceHd)\
**Post date:** [12 February 2022 15:15 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/1 "2022-02-12T15:15:40Z")

</div>

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

```auto
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

```auto

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [12 February 2022 15:21 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/2 "2022-02-12T15:21:40Z")

</div>

> [@XhanceHd](#):
>
> ```auto
> msg.attachments = [
> { filename : "image.jpg",
> content : msg.payload[0] },
> {
> filename : "image2.jpg",
> contnent : msg.payload[1]
> }
>     
> ] ;  
> 
> msg.payload = "Test";
> return msg;
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![XhanceHd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhancehd/32/48135_2.png) [@XhanceHd](https://discourse.nodered.org/u/XhanceHd)\
**Post date:** [12 February 2022 15:35 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/3 "2022-02-12T15:35:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 February 2022 16:00 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/4 "2022-02-12T16:00:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![XhanceHd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhancehd/32/48135_2.png) [@XhanceHd](https://discourse.nodered.org/u/XhanceHd)\
**Post date:** [12 February 2022 16:29 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/5 "2022-02-12T16:29:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paulkeates](https://avatars.discourse-cdn.com/v4/letter/p/9de053/32.png) [@paulkeates](https://discourse.nodered.org/u/paulkeates)\
**Post date:** [12 February 2022 16:34 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/6 "2022-02-12T16:34:43Z")

</div>

Hi,

Try using the image tools:[node-red-contrib-image-tools (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-image-tools)

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

Cheers,

Paul

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 February 2022 17:14 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/7 "2022-02-12T17:14:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![XhanceHd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhancehd/32/48135_2.png) [@XhanceHd](https://discourse.nodered.org/u/XhanceHd)\
**Post date:** [13 February 2022 04:16 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/8 "2022-02-13T04:16:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![XhanceHd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhancehd/32/48135_2.png) [@XhanceHd](https://discourse.nodered.org/u/XhanceHd)\
**Post date:** [13 February 2022 05:05 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/9 "2022-02-13T05:05:50Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/5203c0ae0aab5949390011b67bb63b7f1df3d60c.png)  
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

```auto

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;

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [13 February 2022 07:05 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/10 "2022-02-13T07:05:22Z")

</div>

> [@XhanceHd](#):
>
> How do I convert the image from base64 to buffer on function node?

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](https://nodemailer.com/message/attachments/) on which the email node is based on .. it gives an example how to define the attachment if it's already base64

```auto
{ // 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'),`

---

<div class="post-metadata">

**Author:** ![XhanceHd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhancehd/32/48135_2.png) [@XhanceHd](https://discourse.nodered.org/u/XhanceHd)\
**Post date:** [13 February 2022 07:19 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/11 "2022-02-13T07:19:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![XhanceHd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhancehd/32/48135_2.png) [@XhanceHd](https://discourse.nodered.org/u/XhanceHd)\
**Post date:** [22 March 2022 13:53 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/12 "2022-03-22T13:53:01Z")

</div>

Just tried it and it work, Thanks man

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 April 2022 13:53 UTC](https://discourse.nodered.org/t/need-help-in-sending-image-attachments-on-email/58226/13 "2022-04-05T13:53:12Z")

</div>

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