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

**URL:** <https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879>\
**Category:** General\
**Created:** [28 April 2021 15:37 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879 "2021-04-28T15:37:25Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![RogerMadrid](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@RogerMadrid](https://discourse.nodered.org/u/RogerMadrid)\
**Post date:** [28 April 2021 15:37 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/1 "2021-04-28T15:37:25Z")

</div>

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.

```auto
[{"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_

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [28 April 2021 18:39 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/2 "2021-04-28T18:39:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RogerMadrid](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@RogerMadrid](https://discourse.nodered.org/u/RogerMadrid)\
**Post date:** [29 April 2021 06:00 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/3 "2021-04-29T06:00:30Z")

</div>

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]}}

---

<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:** [29 April 2021 09:10 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/4 "2021-04-29T09:10:28Z")

</div>

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](https://nodemailer.com/message/attachments/) 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.

---

<div class="post-metadata">

**Author:** ![RogerMadrid](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@RogerMadrid](https://discourse.nodered.org/u/RogerMadrid)\
**Post date:** [29 April 2021 09:52 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/5 "2021-04-29T09:52:59Z")

</div>

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.

---

<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:** [29 April 2021 09:57 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/6 "2021-04-29T09:57:54Z")

</div>

> [@RogerMadrid](#):
>
> 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 🙂

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.

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

---

<div class="post-metadata">

**Author:** ![RogerMadrid](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@RogerMadrid](https://discourse.nodered.org/u/RogerMadrid)\
**Post date:** [29 April 2021 11:18 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/7 "2021-04-29T11:18:00Z")

</div>

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.

---

<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:** [29 April 2021 11:24 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/8 "2021-04-29T11:24:11Z")

</div>

> [@RogerMadrid](#):
>
> I have checked with a debug node

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`

---

<div class="post-metadata">

**Author:** ![RogerMadrid](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@RogerMadrid](https://discourse.nodered.org/u/RogerMadrid)\
**Post date:** [29 April 2021 12:55 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/9 "2021-04-29T12:55:17Z")

</div>

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

---

<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:** [29 April 2021 14:04 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/10 "2021-04-29T14:04:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RogerMadrid](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@RogerMadrid](https://discourse.nodered.org/u/RogerMadrid)\
**Post date:** [29 April 2021 14:20 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/11 "2021-04-29T14:20:56Z")

</div>

Yeah

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/74ae099ef7e09aa0a96973d5470498d51d383b1a.png)  
The debug with this code

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5a3883c526df366b0ee0c45919f97b11e6b96191.png)  
File in node

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3e79ec64d4a99f83d5096e42da325377cad13e5e.png)  
The function

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65cf431a705abdf34ec099bb700a137ed56a944e.png)  
msg.attachments.content

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 April 2021 14:23 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/12 "2021-04-29T14:23:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RogerMadrid](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@RogerMadrid](https://discourse.nodered.org/u/RogerMadrid)\
**Post date:** [29 April 2021 14:25 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/13 "2021-04-29T14:25:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RogerMadrid](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@RogerMadrid](https://discourse.nodered.org/u/RogerMadrid)\
**Post date:** [29 April 2021 14:28 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/14 "2021-04-29T14:28:19Z")

</div>

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!

---

<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:** [29 April 2021 14:28 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/15 "2021-04-29T14:28:32Z")

</div>

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.

---

<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:** [29 April 2021 14:36 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/16 "2021-04-29T14:36:19Z")

</div>

> [@RogerMadrid](#):
>
> WOW, yeah, that solved the problem, but, I still dont undertand why

this is the issue...

> [@RogerMadrid](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3e79ec64d4a99f83d5096e42da325377cad13e5e.png)

We tried to tell you ...

> [@Colin](#):
>
> `msg.attachments` which should contain an array

> [@Steve-Mcl](#):
>
> stick to what the docs say - make `msg.attachments` an array.
> 
> ```auto
> msg.attachments = [{
> "filename": 'filename.xxx', 
> "content": msg.payload, //the buffer from file in node
> }]
> 
> ```

> [@Steve-Mcl](#):
>
> did you verify that `msg.attachments[0].content`

---

<div class="post-metadata">

**Author:** ![RogerMadrid](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@RogerMadrid](https://discourse.nodered.org/u/RogerMadrid)\
**Post date:** [29 April 2021 14:38 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/17 "2021-04-29T14:38:45Z")

</div>

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.

---

<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:** [13 May 2021 14:39 UTC](https://discourse.nodered.org/t/attachment-of-a-e-mail-not-working-with-raspberry-pi-but-working-in-a-pc/44879/18 "2021-05-13T14:39:40Z")

</div>

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