Sending a email with a attachment and a text

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.

[{"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

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

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

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

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.

afbeelding this is the one with the bad attachment

this is the one with the good attachment
afbeelding

[{"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

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 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.

I Tried to do what ypu said but dont get the wished results but i get this
afbeelding
This is My code

And thx for the tip.

Thx in advance

Add this sample, just before return msg :


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

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

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

And thanks in advance for the help

the built in help says...

Alternatively you may provide msg.attachments which should contain an array of one or more attachments in nodemailer format.

image

So try ...

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?" },
];
1 Like

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.

1 Like

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