# Sending a email with a attachment and a text

**URL:** https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626
**Category:** General
**Created:** [25 February 2021 13:51 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626 "2021-02-25T13:51:33Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Bradley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradley/32/37868_2.png) [@Bradley](https://discourse.nodered.org/u/Bradley)
#### Post date: [25 February 2021 13:51 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/1 "2021-02-25T13:51:33Z")

</div>

Im trying for a project to send a email with a bit of text and a attachment but dont find how to do it tried some things also lloked on the internet but dont realy find something similar because the person who will get the email is something we get out a excel file that will be the client base.

So the 2 project that are in here are one that can send the mail with attachment but without text and the mail with text and attachment is the attachment not being able to open. I tried changing to what every thing is send but that doesnt work or im not doing it right im a bit new to all this

Thanks in advance and sorry for the bad english

so here is the code to the first one that send the attachment corectly.

```auto
[{"id":"40815c0a.7e59c4","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"c9c68386.8e578","type":"inject","z":"40815c0a.7e59c4","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":180,"wires":[["1741bf96.e3c538","c81db63b.f5b708"]]},{"id":"1741bf96.e3c538","type":"file in","z":"40815c0a.7e59c4","name":"","filename":"D:\\Games\\Map1.xlsx","format":"","chunk":false,"sendError":false,"x":460,"y":200,"wires":[["56caa4ea.f0e6f4"]]},{"id":"c81db63b.f5b708","type":"change","z":"40815c0a.7e59c4","name":"","rules":[{"t":"set","p":"attachments","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":400,"wires":[["60dc3d9d.3373e4"]]},{"id":"60dc3d9d.3373e4","type":"debug","z":"40815c0a.7e59c4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1010,"y":540,"wires":[]},{"id":"56caa4ea.f0e6f4","type":"book","z":"40815c0a.7e59c4","name":"Map1","raw":false,"x":750,"y":120,"wires":[["4ab5d25b.c9ad24"]]},{"id":"4ab5d25b.c9ad24","type":"sheet","z":"40815c0a.7e59c4","name":"","sheetName":"Blad1","x":880,"y":100,"wires":[["729a6c8.5c1e794","8e740f02.529a8"]]},{"id":"729a6c8.5c1e794","type":"cell","z":"40815c0a.7e59c4","name":"","address":"A1","dataType":"w","x":1030,"y":100,"wires":[["da3e907a.c9219"]]},{"id":"8e740f02.529a8","type":"cell","z":"40815c0a.7e59c4","name":"","address":"F1","dataType":"w","x":900,"y":160,"wires":[[]]},{"id":"da3e907a.c9219","type":"change","z":"40815c0a.7e59c4","name":"","rules":[{"t":"set","p":"to","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":240,"wires":[["f0fad678.519c98"]]},{"id":"75c13d78.8bc3fc","type":"e-mail","z":"40815c0a.7e59c4","server":"smtp.gmail.com","port":"465","secure":true,"tls":false,"name":"","dname":"","x":1230,"y":540,"wires":[]},{"id":"f0fad678.519c98","type":"function","z":"40815c0a.7e59c4","name":"","func":"\n\n\nmsg.payload = \"Email Body Message\";\n\nmsg.topic = \"Subject Message\";\n\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1120,"y":300,"wires":[["72dba50e.beb264","caad305.a2e985"]]},{"id":"72dba50e.beb264","type":"file in","z":"40815c0a.7e59c4","name":"Test","filename":"C:\\Users\\Gebruiker\\Downloads\\1.PDF","format":"","chunk":false,"sendError":false,"encoding":"none","x":1010,"y":360,"wires":[["75c13d78.8bc3fc","caad305.a2e985"]]},{"id":"caad305.a2e985","type":"debug","z":"40815c0a.7e59c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1230,"y":480,"wires":[]},{"id":"5afe5248.eb8a4c","type":"comment","z":"40815c0a.7e59c4","name":"Deze stuurt het bestandje wel goed door maar zonder tekst","info":"","x":700,"y":60,"wires":[]}]

```

and this is the one with broke attachment but with text

```auto
[{"id":"a248e3f6.04a6c","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"73b0b8ce.1d7f3","type":"file in","z":"a248e3f6.04a6c","name":"","filename":"C:\\Users\\Gebruiker\\Pictures\\2.jpg","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":460,"y":440,"wires":[["cda30ce2.0fc9a"]]},{"id":"cda30ce2.0fc9a","type":"function","z":"a248e3f6.04a6c","name":"Prepare Email","func":"msg.attachments = [{filename: \"2.jpg\", content: msg.payload[0]}];\n\n\nmsg.payload = \"Email Body Message\";\n\nmsg.topic = \"Subject Message\";\n\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":860,"y":380,"wires":[["77339089.8bb548"]]},{"id":"9c789286.63f0d","type":"inject","z":"a248e3f6.04a6c","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":220,"wires":[["f01cf936.a41da"]]},{"id":"f01cf936.a41da","type":"file in","z":"a248e3f6.04a6c","name":"","filename":"D:\\Games\\Map1.xlsx","format":"","chunk":false,"sendError":false,"x":320,"y":240,"wires":[["f24e6c8.d84bd9"]]},{"id":"f24e6c8.d84bd9","type":"book","z":"a248e3f6.04a6c","name":"Map1","raw":false,"x":610,"y":160,"wires":[["aaae0997.fc1a08"]]},{"id":"aaae0997.fc1a08","type":"sheet","z":"a248e3f6.04a6c","name":"","sheetName":"Blad1","x":740,"y":140,"wires":[["f9cfa2d5.6b3fd8","751acde8.2dea74"]]},{"id":"f9cfa2d5.6b3fd8","type":"cell","z":"a248e3f6.04a6c","name":"","address":"A1","dataType":"w","x":890,"y":140,"wires":[["5aafded.8d5702"]]},{"id":"751acde8.2dea74","type":"cell","z":"a248e3f6.04a6c","name":"","address":"F1","dataType":"w","x":760,"y":200,"wires":[[]]},{"id":"5aafded.8d5702","type":"change","z":"a248e3f6.04a6c","name":"","rules":[{"t":"set","p":"to","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":280,"wires":[["cda30ce2.0fc9a"]]},{"id":"77339089.8bb548","type":"e-mail","z":"a248e3f6.04a6c","server":"smtp.gmail.com","port":"465","secure":true,"tls":false,"name":"","dname":"","x":1110,"y":380,"wires":[]},{"id":"6d541020.7117","type":"comment","z":"a248e3f6.04a6c","name":"Uitleg denk wijze","info":"Bij deze heb ik geprobeeerd via de functie blok het bestand te versturen zodat de tekst er ook in staat maar dit zorgt ervoor dat het bestand niet bekijkbaar is .","x":480,"y":60,"wires":[]}]

```

---

<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: [25 February 2021 15:24 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/2 "2021-02-25T15:24:46Z")

</div>

Welcome to the forum. Unfortunately your flow is not importable, it is necessary to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![Bradley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradley/32/37868_2.png) [@Bradley](https://discourse.nodered.org/u/Bradley)
#### Post date: [25 February 2021 15:30 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/3 "2021-02-25T15:30:27Z")

</div>

Thx have edit my post sorry that you needed to point out this

---

<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: [25 February 2021 15:32 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/4 "2021-02-25T15:32:23Z")

</div>

No problem. Please show us what a debug node showing what is going into the email node shows in each case. Set to show Complete Message as you have in the flows.

---

<div class="post-metadata">

### Author: ![Bradley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradley/32/37868_2.png) [@Bradley](https://discourse.nodered.org/u/Bradley)
#### Post date: [25 February 2021 15:45 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/5 "2021-02-25T15:45:58Z")

</div>

![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/8/d8e286a620b5439a4ff481766adc1661be099a43.png) this is the one with the bad attachment

---

<div class="post-metadata">

### Author: ![Bradley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradley/32/37868_2.png) [@Bradley](https://discourse.nodered.org/u/Bradley)
#### Post date: [25 February 2021 15:46 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/6 "2021-02-25T15:46:48Z")

</div>

this is the one with the good attachment  
 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90aaef3b1c03c5dd5f7b57929a9ae11a52f46e3e.png)

---

<div class="post-metadata">

### Author: ![Bradley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradley/32/37868_2.png) [@Bradley](https://discourse.nodered.org/u/Bradley)
#### Post date: [25 February 2021 16:00 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/7 "2021-02-25T16:00:08Z")

</div>

```auto
[{"id":"a7a64df7.6f0ca8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"bc9ed8ca.efae88","type":"debug","z":"a7a64df7.6f0ca8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":970,"y":300,"wires":[]},{"id":"7ec48af5.b81ff4","type":"change","z":"a7a64df7.6f0ca8","name":"","rules":[{"t":"set","p":"to","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":300,"wires":[["b6832763.8ee61"]]},{"id":"9278b0f0.474f1","type":"e-mail","z":"a7a64df7.6f0ca8","server":"smtp.gmail.com","port":"465","secure":true,"tls":false,"name":"","dname":"","x":1090,"y":440,"wires":[]},{"id":"30eb4926.303bde","type":"inject","z":"a7a64df7.6f0ca8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":140,"wires":[["ec79b9bb.b30948"]]},{"id":"ec79b9bb.b30948","type":"file in","z":"a7a64df7.6f0ca8","name":"","filename":"D:\\Games\\Map1.xlsx","format":"","chunk":false,"sendError":false,"encoding":"none","x":380,"y":160,"wires":[["4ce36ea1.e98b38"]]},{"id":"4ce36ea1.e98b38","type":"book","z":"a7a64df7.6f0ca8","name":"Map1","raw":false,"x":540,"y":120,"wires":[["fa523dce.814dd"]]},{"id":"fa523dce.814dd","type":"sheet","z":"a7a64df7.6f0ca8","name":"","sheetName":"Blad1","x":670,"y":100,"wires":[["5ffde0e0.4d0a6","80e4ce7f.d43d48"]]},{"id":"5ffde0e0.4d0a6","type":"cell","z":"a7a64df7.6f0ca8","name":"","address":"A1","dataType":"w","x":820,"y":100,"wires":[["7ec48af5.b81ff4"]]},{"id":"739d84ba.594f7c","type":"file in","z":"a7a64df7.6f0ca8","name":"Batch Reports","filename":"D:\\Games\\Map1.xlsx","format":"","chunk":false,"sendError":false,"x":480,"y":380,"wires":[[]]},{"id":"b6832763.8ee61","type":"function","z":"a7a64df7.6f0ca8","name":"","func":"var temp=msg.payload;\n\nvar d =new Date();\nvar message=\"\";\nif(msg.alarm)\n{\n msg.topic=\"Verhuur Eliet\";\n message=\" Bij deze heeft u de machine gehuurd=\";\n msg.attachments = [{filename: \"Map1.xlsx\",},];\n }\n else\n { message=\" Temperature now normal temp=\";\n msg.topic=\"Temperature Alarm Reset\";\n msg.attachments = [{filename: \"Map1.xlsx\", },];\n } \n msg.payload=\"time:\"+d+message +msg.payload;\n return msg;\n ","outputs":1,"noerr":0,"initialize":"","finalize":"","x":780,"y":340,"wires":[["9278b0f0.474f1","bc9ed8ca.efae88"]]},{"id":"80e4ce7f.d43d48","type":"cell","z":"a7a64df7.6f0ca8","name":"","address":"F1","dataType":"w","x":690,"y":160,"wires":[[]]},{"id":"3102f76b.67249","type":"function","z":"a7a64df7.6f0ca8","name":"Prepare Email","func":"msg.attachments = [{filename: \"Map1.xlsx\"}];\n\n\nmsg.payload = \"Bij deze is u verhuring geacepterd\";\n\nmsg.topic = \"Verhuur Eliet NV\";\n\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":960,"y":500,"wires":[[]]}]

```

this is the right code of the one with bad attachment

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [25 February 2021 16:05 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/8 "2021-02-25T16:05:20Z")

</div>

If you look at the `Help` for the `email` node you will see:

> If the payload is a binary buffer then it will be converted to an attachment. The filename should be set using `msg.filename` . Optionally `msg.description` can be added for the body text.
> 
> Alternatively you may provide `msg.attachments` which should contain an array of one or more attachments in [nodemailer](https://nodemailer.com/message/attachments/) format. believe the problem is that you have msg.filename and msg.attachments.

Follow the link to see how msg.attachment needs to be setup.

---

<div class="post-metadata">

### Author: ![Bradley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradley/32/37868_2.png) [@Bradley](https://discourse.nodered.org/u/Bradley)
#### Post date: [25 February 2021 19:18 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/9 "2021-02-25T19:18:30Z")

</div>

I Tried to do what ypu said but dont get the wished results but i get this  
 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/3/a3fcf32f9d83b2350be9d58d41f72299125cf838.png)  
This is My code

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9e0dc61d3f2314864de301a00d248b73d5891e2a.png)

And thx for the tip.

Thx in advance

---

<div class="post-metadata">

### Author: ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)
#### Post date: [26 February 2021 10:15 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/10 "2021-02-26T10:15:40Z")

</div>

Add this sample, just before **return msg** :

```auto

msg.attachments = [{ filename: "file_1.txt",
                     content: "dummy text file 1" },
                   { filename: "file_2.txt",
                     content: "dummy text file 2" }];

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [26 February 2021 11:39 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/11 "2021-02-26T11:39:49Z")

</div>

> [@Bradley](#):
>
> I Tried to do what ypu said but dont get the wished results but i get this  
> ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/3/a3fcf32f9d83b2350be9d58d41f72299125cf838.png)

look at the path ↑.

when generating a file path for windows, use double backslash `\\` or better still use a fwd slash `/` e.g. `d:/file.name`

---

<div class="post-metadata">

### Author: ![Bradley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradley/32/37868_2.png) [@Bradley](https://discourse.nodered.org/u/Bradley)
#### Post date: [26 February 2021 12:38 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/12 "2021-02-26T12:38:28Z")

</div>

I tried what you said and make some othere changes like // but stil dont get it and when use this

> Blockquote  
> msg.attachments = [{ filename: "file\_1.txt",  
> content: "dummy text file 1" },  
> { filename: "file\_2.txt",  
> content: "dummy text file 2" }];  
> But then it just send a broke document see pic code is in the first pic
> 
> ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b24f5a5e2ff52ee5794808d7ce44e40aa99ebfa.png)  
> ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/711ec3fb516c1c8d23291ee435d70b610d8f6d84.png)

And thanks in advance for the help

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [26 February 2021 13:03 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/13 "2021-02-26T13:03:53Z")

</div>

the built in help says...

> Alternatively you may provide `msg.attachments` which should contain an array of one or more attachments in [nodemailer](https://nodemailer.com/message/attachments/) format.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b12af39b9ada97cebc0aaeaba297f6bea0fb7b27.png)

So try ...

```auto
msg.topic = "This should be in the subject";
msg.payload = "This should be in the contents";
msg.attachments = [ 
  { filename: "Map1.xlsx", path: "D:/whatever.xlsx" },
  { filename: "textfile.txt", content: "is this text inside this text file?" },
];

```

---

<div class="post-metadata">

### Author: ![Bradley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradley/32/37868_2.png) [@Bradley](https://discourse.nodered.org/u/Bradley)
#### Post date: [26 February 2021 13:16 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/14 "2021-02-26T13:16:13Z")

</div>

Thank u for the help i have changed what you said and now does it what it need to do and indeed the last line with content works only for txt files and not for xslx files thx for the help and have learnt where to let on and look out for later.

---

<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: [12 March 2021 13:16 UTC](https://discourse.nodered.org/t/sending-a-email-with-a-attachment-and-a-text/41626/15 "2021-03-12T13:16:33Z")

</div>

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