Attachment of a e-mail not working with Raspberry Pi but working in a PC

Hi,
I've been having a problem trying to send a email with a .csv file attached. I know how to do it, at least, with a pc, but, for some reason, the raspberry Pi 3 doesn't send the file, despite of that both have the same flows (exept for the site of the file inside the PC). The debug console shows exactly the same msg when I add a debug node just before the e-mail node, so I don't understand what's the problem now and I would like to know if someone had a similar problem using a Raspberry Pi 3, and specially, if someone knows how to solve it.

Here I paste the code used in the Raspberry.

Thanks you, very much.

[{"id":"9358a7d4.f758f8","type":"inject","z":"e121a050.a5197","name":"Daily email","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 02 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":1700,"wires":[["90b08b74.7de978","61123edf.1ce33","59124705.4e7df8"]]},{"id":"abc787eb.26d238","type":"file in","z":"e121a050.a5197","name":"get file data","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":750,"y":1620,"wires":[["f2ee8d29.cc06"]]},{"id":"f2ee8d29.cc06","type":"function","z":"e121a050.a5197","name":"write email","func":"file = msg.filename;    // create local file variable for convenient reference\nvar d = new Date();\nd.setDate(d.getDate() - 1);// create current date object for the time string\nvar tstring = d.toString().substring(4,15);\nvar n = global.get(\"serial_numbers\");\nvar nstring = n.toString().substring(6,20);\n\nmsg.attachments =\n     { filename : file.substring(file.lastIndexOf('/')+1, file.lenght),\n        content : msg.payload };    // content should be a file binary buffer\n    \nmsg.topic = \"Battery Data logs at \" + tstring + \" of module \" + nstring; // email subject\n\nmsg.payload = \"Please, don't answer this email.\\n \\n \\nHere you have attatched the daily battery data logs of your module: \" + nstring; // email body\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":930,"y":1620,"wires":[["cdf643bd.03c86","831cdbb4.e8ded8"]]},{"id":"831cdbb4.e8ded8","type":"debug","z":"e121a050.a5197","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":1580,"wires":[]},{"id":"d3b7b402.b40a48","type":"function","z":"e121a050.a5197","name":"format","func":"flow.set(\"log\", true);\nvar date = msg.payload.split('-');\ndate = date[0].replace(/\\//g, '');\nvar filename = \"/home/pi/Documents/Logs/battery_data \" + date + \".csv\";\nmsg.filename = filename;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":1620,"wires":[["abc787eb.26d238"]]},{"id":"90b08b74.7de978","type":"moment","z":"e121a050.a5197","name":"","topic":"","input":"","inputType":"msg","inTz":"Europe/Madrid","adjAmount":"1","adjType":"days","adjDir":"subtract","format":"L-LTS","locale":"es_ES","output":"payload","outputType":"msg","outTz":"Europe/Madrid","x":400,"y":1620,"wires":[["d3b7b402.b40a48"]]},{"id":"721cb58d.f8d34c","type":"file in","z":"e121a050.a5197","name":"get file values","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":760,"y":1700,"wires":[["71ae0a45.ea7d64"]]},{"id":"71ae0a45.ea7d64","type":"function","z":"e121a050.a5197","name":"write email","func":"file = msg.filename;    // create local file variable for convenient reference\nvar d = new Date();\nd.setDate(d.getDate() - 1);// create current date object for the time string\nvar tstring = d.toString().substring(4,15);\nvar n = global.get(\"serial_numbers\");\nvar nstring = n.toString().substring(6,20);\n\nmsg.attachments =\n     { filename : file.substring(file.lastIndexOf('/')+1,file.length),\n        content : msg.payload };    // content should be a file binary buffer\n    \nmsg.topic = \"Power values logs at \" + tstring + \" of module \" + nstring; // email subject\n\nmsg.payload = \"Please, don't answer this email.\\n \\n \\nHere you have attatched the daily power values logs of your module: \" + nstring; // email body\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":930,"y":1700,"wires":[["211c83f0.3f6ccc","dd4a51be.14951"]]},{"id":"dd4a51be.14951","type":"debug","z":"e121a050.a5197","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":1660,"wires":[]},{"id":"d36692ff.31d7f","type":"function","z":"e121a050.a5197","name":"format","func":"flow.set(\"log\", true);\nvar date = msg.payload.split('-');\ndate = date[0].replace(/\\//g, '');\nvar filename = \"/home/pi/Documents/Logs/power_values \" + date + \".csv\";\nmsg.filename = filename;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":1700,"wires":[["721cb58d.f8d34c"]]},{"id":"61123edf.1ce33","type":"moment","z":"e121a050.a5197","name":"","topic":"","input":"","inputType":"msg","inTz":"Europe/Madrid","adjAmount":"1","adjType":"days","adjDir":"subtract","format":"L-LTS","locale":"es_ES","output":"payload","outputType":"msg","outTz":"Europe/Madrid","x":400,"y":1700,"wires":[["d36692ff.31d7f"]]},{"id":"cdf643bd.03c86","type":"e-mail","z":"e121a050.a5197","server":"smtp.gmail.com","port":"465","secure":true,"tls":false,"name":"xxx@gmail.com","dname":"Battery Data Email","x":1170,"y":1620,"wires":[]},{"id":"211c83f0.3f6ccc","type":"e-mail","z":"e121a050.a5197","server":"smtp.gmail.com","port":"465","secure":true,"tls":false,"name":"xxx@gmail.com","dname":"Power Values Email","x":1180,"y":1700,"wires":[]},{"id":"5e799071.d6136","type":"file in","z":"e121a050.a5197","name":"get file warnings","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":760,"y":1780,"wires":[["91ee381f.66a3b8"]]},{"id":"91ee381f.66a3b8","type":"function","z":"e121a050.a5197","name":"write email","func":"file = msg.filename;    // create local file variable for convenient reference\nvar d = new Date();\nd.setDate(d.getDate() - 1);// create current date object for the time string\nvar tstring = d.toString().substring(4,15);\nvar n = global.get(\"serial_numbers\");\nvar nstring = n.toString().substring(6,20);\n\nmsg.attachments =\n     { filename : file.substring(file.lastIndexOf('/')+1,file.length),\n        content : msg.payload };    // content should be a file binary buffer\n    \nmsg.topic = \"These are the warning at \" + tstring + \" of module \" + nstring; // email subject\n\nmsg.payload = \"Please, don't answer this email.\\n \\n \\nHere you have attatched the daily warning logs of your module: \" + nstring; // email body\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":930,"y":1780,"wires":[["8330fd8f.21877","c6312613.87a1f8"]]},{"id":"c6312613.87a1f8","type":"debug","z":"e121a050.a5197","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":1740,"wires":[]},{"id":"1d70c496.4271bb","type":"function","z":"e121a050.a5197","name":"format","func":"flow.set(\"log\", true);\nvar date = msg.payload.split('-');\ndate = date[0].replace(/\\//g, '');\nvar filename = \"/home/pi/Documents/Logs/warnings \" + date + \".csv\";\nmsg.filename = filename;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":1780,"wires":[["5e799071.d6136"]]},{"id":"59124705.4e7df8","type":"moment","z":"e121a050.a5197","name":"","topic":"","input":"","inputType":"msg","inTz":"Europe/Madrid","adjAmount":"1","adjType":"days","adjDir":"subtract","format":"L-LTS","locale":"es_ES","output":"payload","outputType":"msg","outTz":"Europe/Madrid","x":400,"y":1780,"wires":[["1d70c496.4271bb"]]},{"id":"8330fd8f.21877","type":"e-mail","z":"e121a050.a5197","server":"smtp.gmail.com","port":"465","secure":true,"tls":false,"name":"xxx@gmail.com","dname":"Warnings Email","x":1160,"y":1780,"wires":[]}]

Admin edit - removed personal email address

First thing I noticed is that you didn't fill in date time/formatter the input from field, don't know if that matters but personally I don't like fields with red boxes around it. Better be save then sorry.
What about rights, does the dir /home/pi/Documents/Logs/battery_data have enough rights to be read and modified by other/all users?

Ps. Better remove your mail address form the flow before posting it here

About the mail, yeah, i forgot to remove it at the moment of pasting the code, thanks for the advice.
The time/formatter, there is no problem cause of it uses the payload recieved, i don't know why the box is red, but it works perfectly. And for the rights, yes, it have all the rights needed cause I can create, delete, modify and acces to all the files in that directory, also I paste here the debug msg from the debug console to show what arrives to the email node (what is working in my PC but is not sending the file attached using the Raspberry)

{"_msgid":"30626749.98f3a8",
"payload":"Please, don't answer this email.\n \n \nHere you have attatched the daily battery data logs of your module: ...",
"topic":"Battery Data logs at Apr 28 2021 of module 82011906030010","settings":{"input":"2021-04-28T15:05:48.464Z",
"input_format":"",
"input_tz":"Europe/Madrid",
"output_format":"L-LTS",
"output_locale":"es_ES",
"output_tz":"Europe/Madrid"},
"filename":"/home/pi/Documents/Logs/battery_data 27042021.csv",
"attachments":{"filename":"battery_data 27042021.csv",
"content":[68,65,84,79,44,86,65,76,79,82, ... ,49]}}

I haven't sent attachments, but looking at the help text for the node it says:

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

Your payload does not appear to be a binary buffer but you have provided a filename. You have also provided msg.attachments but have not specified the full path, and also you have provided an object not an array.

[Edit] In fact the filename in the attachements is ok, of course, you don't need a path there. However, the fact that is not an array may be significant.

Thanks for your answer.

Remember that I said about the same code works perfeclty right in my PC. Exactly the same code. And if you look at the debug msg I pasted in my last msg, you can see part of the buffer, the filename, etc.

I've been trying things in the raspberry and seems that the problem is not the code rather than google blocking the attachments of the e-mail sended by the raspberry.

I wouldnt put all your eggs in one basket :slight_smile:

It might be the PI has a different node-version or a module (or sub module) is a different version.

I would recommend you use a "file in" (set to load a buffer) and use that in the msg.attachments. also, stick to what the docs say - make msg.attachments an array.

msg.attachments = [{
 "filename": 'filename.xxx', 
 "content": msg.payload, //the buffer from file in node
}]

This will ensure the file is actually accessible to node-red (since it has to load it).

Thanks for your answer
I use a file in, the filename of the msg.attachments is an array and the content is the msg.payload. I have checked with a debug node and the msg.attachments is configured correctly. So this is not the problem.

did you verify that msg.attachments[0].content is definitely a buffer (with more than 0 bytes)?

As a test, use the same flow on the PC and compare the (complete) msg object that is sent into the email node (use a debug set to show complete message).

PS, can you confirm versions on PC and PI...

  • cd .node-red
  • node -v
  • npm list nodemailer
  • npm list node-red-node-email

Tested in a PC with the same code, working perfectly and the debug window output is exactly the same...

Can you show us a screenshot of the debug please?
Also the answers to the questions in the previous post.

Yeah


The debug with this code

image
File in node

image
The function

image
msg.attachments.content

So - yes - as @colin keeps trying to point out... msg.attachments MUST be an array [ ], not a bare object... so [ { filename: ... } ] not { filename: ... }

And why is it working being exactly the same in the PC?

WOW, yeah, that solved the problem, but, I still dont undertand why it's not a problem when I do the same in the PC, amazing.

thank you all very much guys!

That is irrelevant. You are using different versions of lots of stuff. You should fix it on the PC too as it may fail if you upgrade something.

this is the issue...

We tried to tell you ...

Sorry, I didn't see the [ ] in the code that you wrote. I was too ofuscated with the point that it was working with the same code in the PC.

2 Likes

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