# Email attachment | Invalid file

**URL:** <https://discourse.nodered.org/t/email-attachment-invalid-file/74550>\
**Category:** General\
**Created:** [1 February 2023 12:00 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550 "2023-02-01T12:00:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://discourse.nodered.org/u/Manikandan)\
**Post date:** [1 February 2023 12:00 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/1 "2023-02-01T12:00:39Z")

</div>

Hi all,

I’m looking for some help with a flow. I would like to attach a file on my email. The node-red is successfully sending it, but unfortunately while receiving it. it appears to be a invalid file with no data on it.

Your input on resoling this issue highly appreciated.

![2023-02-01_11h56_54](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/86499da829cafa14eff9ef08d437be36f662d421.png)

```auto
[
    {
        "id": "f6cfd0144d7f0ade",
        "type": "inject",
        "z": "9436f441bf6223e5",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 180,
        "y": 80,
        "wires": [
            [
                "68fa5e8c01fd96f3"
            ]
        ]
    },
    {
        "id": "68fa5e8c01fd96f3",
        "type": "file in",
        "z": "9436f441bf6223e5",
        "name": "",
        "filename": "/Shared/ONLINE/read_send_test.txt",
        "filenameType": "str",
        "format": "",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 230,
        "y": 160,
        "wires": [
            [
                "c67282934acdcc28",
                "11fde0f7462e2601"
            ]
        ]
    },
    {
        "id": "c67282934acdcc28",
        "type": "change",
        "z": "9436f441bf6223e5",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "attachments",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 540,
        "y": 160,
        "wires": [
            [
                "9bd459d05a7dcc99",
                "d9f16e1b29146ad0"
            ]
        ]
    },
    {
        "id": "11fde0f7462e2601",
        "type": "debug",
        "z": "9436f441bf6223e5",
        "name": "debug 34",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 140,
        "y": 260,
        "wires": []
    },
    {
        "id": "9bd459d05a7dcc99",
        "type": "function",
        "z": "9436f441bf6223e5",
        "name": "Email Draft",
        "func": "let temp = msg.attachments;\n\nmsg.from = \"xxxxxx@gmail.com\";\nmsg.cc = \"yyyyy@gmail.com\";\nmsg.topic = \"Attachment check \" \n\nmsg.attachments = temp;\n\nmsg.description = \"- Auto Re-start at 6AM \"\n\n\nmsg.payload = \"Hello World Attachment Test\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 750,
        "y": 160,
        "wires": [
            [
                "55ac7cce3d36c917",
                "282bb48371ea445f"
            ]
        ]
    },
    {
        "id": "d9f16e1b29146ad0",
        "type": "debug",
        "z": "9436f441bf6223e5",
        "name": "debug 35",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 400,
        "y": 260,
        "wires": []
    },
    {
        "id": "55ac7cce3d36c917",
        "type": "e-mail",
        "z": "9436f441bf6223e5",
        "server": "smtp.gmail.com",
        "port": "465",
        "secure": true,
        "tls": true,
        "name": "",
        "dname": "Attachment Check | Node-Red",
        "x": 990,
        "y": 160,
        "wires": []
    },
    {
        "id": "282bb48371ea445f",
        "type": "debug",
        "z": "9436f441bf6223e5",
        "name": "debug 36",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 760,
        "y": 260,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 February 2023 12:34 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/2 "2023-02-01T12:34:23Z")

</div>

Hi @Manikandan

I don't use the email node, but from what I gathered, `attachments` needs to be an array of objects.  
and each of these objects needs to follow the node mailer format of an attachment

> **[Attachments :: Nodemailer](https://nodemailer.com/message/attachments/)**
>
> Nodemailer is a module for Node.js to send emails

try the below.

```auto
[{"id":"f6cfd0144d7f0ade","type":"inject","z":"2d7bf6e3.84c97a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":205,"y":785,"wires":[["68fa5e8c01fd96f3"]]},{"id":"68fa5e8c01fd96f3","type":"file in","z":"2d7bf6e3.84c97a","name":"","filename":"/Shared/ONLINE/read_send_test.txt","filenameType":"str","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":255,"y":865,"wires":[["c67282934acdcc28","11fde0f7462e2601"]]},{"id":"c67282934acdcc28","type":"change","z":"2d7bf6e3.84c97a","name":"","rules":[{"t":"set","p":"attachments","pt":"msg","to":"[\t {\t \"filename\": \"my-text-file.txt\",\t \"content\": payload,\t \"contentType\":\"text/plain\"\t }\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":565,"y":865,"wires":[["9bd459d05a7dcc99","d9f16e1b29146ad0"]]},{"id":"11fde0f7462e2601","type":"debug","z":"2d7bf6e3.84c97a","name":"debug 34","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":165,"y":965,"wires":[]},{"id":"9bd459d05a7dcc99","type":"function","z":"2d7bf6e3.84c97a","name":"Email Draft","func":"let temp = msg.attachments;\n\nmsg.from = \"xxxxxx@gmail.com\";\nmsg.cc = \"yyyyy@gmail.com\";\nmsg.topic = \"Attachment check \" \n\nmsg.attachments = temp;\n\nmsg.description = \"- Auto Re-start at 6AM \"\n\n\nmsg.payload = \"Hello World Attachment Test\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":775,"y":865,"wires":[["55ac7cce3d36c917","282bb48371ea445f"]]},{"id":"d9f16e1b29146ad0","type":"debug","z":"2d7bf6e3.84c97a","name":"debug 35","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":425,"y":965,"wires":[]},{"id":"55ac7cce3d36c917","type":"e-mail","z":"2d7bf6e3.84c97a","server":"smtp.gmail.com","port":"465","secure":true,"tls":true,"name":"","dname":"Attachment Check | Node-Red","x":1015,"y":865,"wires":[]},{"id":"282bb48371ea445f","type":"debug","z":"2d7bf6e3.84c97a","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":785,"y":965,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://discourse.nodered.org/u/Manikandan)\
**Post date:** [1 February 2023 13:21 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/3 "2023-02-01T13:21:30Z")

</div>

Hi @marcus-j-davies ,

Thank you very much, it worked.

Is it possible to attach multiple files while sending an email by this approach ?

Best Regards,  
Mani

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 February 2023 13:24 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/4 "2023-02-01T13:24:13Z")

</div>

Yes,  
The change node creates an array, the example I put has 1 entry, but you can add many, as long as each entry has the properties Node mailer needs  
`[{...},{...},{...}]`

---

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://discourse.nodered.org/u/Manikandan)\
**Post date:** [1 February 2023 13:43 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/5 "2023-02-01T13:43:57Z")

</div>

Hi @marcus-j-davies ,

thanks for your swift response, tried using with '[{...},{...},{...}]'  
the problem i am facing is attaching two different file with the single change node.  
I tired with the attached code, two emails getting triggered with the single file on it.  
Any help on making attachments using two files in change node would be highly apricated ?

```auto
[
    {
        "id": "1b25d09fdf1b64e3",
        "type": "function",
        "z": "8bca76c821d6dc43",
        "name": "Email Draft",
        "func": "let temp = msg.attachments;\n\nmsg.from = \"xxx@gmail.com\";\nmsg.cc = \"xxx@gmail.com\";\nmsg.topic = \"Attachment check \" \n\nmsg.attachments = temp;\n\nmsg.description = \"- Auto Re-start at 6AM \"\n\n\nmsg.payload = \"Hello World Attachment Test\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 930,
        "y": 360,
        "wires": [
            [
                "10c2e77837581529"
            ]
        ]
    }
]

```

Best regards,  
Mani

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 February 2023 14:00 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/6 "2023-02-01T14:00:31Z")

</div>

There are countless ways, here is just one (for 2 files)

- Add 2 **File** nodes (for each file)
- Attach them to a **Join** Node with these properties (image 1)
- Attach the **Join** node to your **Change** node
- Then in your **Change** node, use the below code

```auto
[
   {
       "filename": "my-text-file1.txt",
       "content": payload[0],
       "contentType":"text/plain"
    },
     {
       "filename": "my-text-file2.txt",
       "content": payload[1],
       "contentType":"text/plain"
    }
]

```

Image 1

 ![Screenshot 2023-02-01 at 13.54.41](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33929f24dcdf3bc62f007a602bee70545f0b35da.png)

Im sure there are more optimised/better ways, but it will give you some idea on how to do it  
You will need play with this setup a little to obtain the filename, so you can tell them apart

i.e set **Combine each** to `Complete message` and alter the change node

```auto
[
   {
       "filename": payload[0].filename,
       "content": payload[0].payload,
       "contentType":"text/plain"
    },
     {
       "filename": payload[1].filename,
       "content": payload[1].payload,
       "contentType":"text/plain"
    }
]

```

---

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://discourse.nodered.org/u/Manikandan)\
**Post date:** [1 February 2023 14:45 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/7 "2023-02-01T14:45:46Z")

</div>

Hi @marcus-j-davies ,

It worked with this approach, much appreciated @marcus-j-davies.

I was trying to use the the below code to attach the csv file, the file is send successfully but the file name as become attachment1.txt and attachment.txt.

the file and the file extension not not reflect,  
I presume this might be due the contenttype as text/plain, any clue on fixing this ?

```auto
"filename": payload[1].filename,
       "content": payload[1].payload,
       "contentType":"text/plain"

```

_Best Regards,_  
Mani

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 February 2023 15:04 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/8 "2023-02-01T15:04:46Z")

</div>

Check what you're getting out of the Join Node.  
Providing it is set to `Complete message`

You should be getting something like below.

```auto
{
  payload: [
    {
      filename: "some-file-name.csv",
      payload: <FileContents>
    },
    {
      filename: "some-other-file-name.csv",
      payload: <FileContents>
    }
  ]
}

```

Also try using `text/csv` as the `contentType`

---

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://discourse.nodered.org/u/Manikandan)\
**Post date:** [2 February 2023 08:21 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/9 "2023-02-02T08:21:55Z")

</div>

hi @marcus-j-davies ,

While using complete Message getting the attached error message at email node.

Also, if you look at the filename from debug window , it shows the complete path as a file name.  
I think, the characters '/' in the filename is creating an issue, because in the real world we can't create a file name with the '/'. please let me know your suggestions ?

 ![2023-02-02_08h15_38](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/545f850dbf817b752b25605f25b0bcb72618ce1b.png)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [2 February 2023 09:30 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/10 "2023-02-02T09:30:45Z")

</div>

> [@marcus-j-davies](#):
>
> ```auto
> [
> {
> "filename": payload[0].filename,
> "content": payload[0].payload,
> "contentType":"text/csv"
> },
> {
> "filename": payload[1].filename,
> "content": payload[1].payload,
> "contentType":"text/csv"
> }
> ]
> 
> ```

Your change node should be as above (modified to take a 3rd attachment)

As for the filename, just use the JSONata method of `$split`  
[String functions · JSONata](https://docs.jsonata.org/string-functions#split)

```auto
[
   {
       "filename": $split(payload[0].filename,'/')[2],
       "content": payload[0].payload,
       "contentType":"text/csv"
    },
     {
       "filename": $split(payload[1].filename,'/')[2],
       "content": payload[1].payload,
       "contentType":"text/csv"
    }
]

```

---

<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:** [2 February 2023 10:49 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/11 "2023-02-02T10:49:05Z")

</div>

Why bother with reading the file in the first place? You can give the email node the path to the file and it will grab it. Try this:

```auto
[{"id":"1b25d09fdf1b64e3","type":"function","z":"37326f3f1c6a564d","name":"Email Draft","func":"let temp = msg.attachments;\n\nmsg.from = \"xxx@gmail.com\";\nmsg.cc = \"xxx@gmail.com\";\nmsg.topic = \"Attachment check \";\nmsg.filepath = '/Shared/ONLINE/read_send_test.txt';\nmsg.attachments = [{ filename: \"test.txt\", path: msg.filepath }]\n\nmsg.payload = \"Hello World Attachment Test\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":420,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://discourse.nodered.org/u/Manikandan)\
**Post date:** [2 February 2023 11:03 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/12 "2023-02-02T11:03:29Z")

</div>

Thanks for you suggestions @zenofmud ., initially i tried with the function node and as the number of attachments gone more than one, tried other methods.

@marcus-j-davies : the reference which has been provided worked well.  
Now to add more complex, will be possible to send only few attachments based on the some variables.

_like :_  
Attach file 1 only if the file\_column \>= 10;  
Attach file 2 only if the file\_column \>= 9;

_Best Regards,_  
Mani

---

<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:** [4 February 2023 13:24 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/13 "2023-02-04T13:24:45Z")

</div>

> [@Manikandan](#):
>
> Now to add more complex, will be possible to send only few attachments based on the some variables.

Sure you can do that. You could a `switch` node to determine the file\_column value and output it to different `change` nodes based on its values. Each `change` node would set up the data as you want, then the `change` nodes would all link to the same email node.

---

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://discourse.nodered.org/u/Manikandan)\
**Post date:** [5 February 2023 18:18 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/14 "2023-02-05T18:18:14Z")

</div>

hi @zenofmud ,

I was breaking my head in making all these logic in the single change node for the required attachment.  
Thanks for your suggestions, It worked.

_Best Regards,_  
Mani

---

<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:** [19 February 2023 18:18 UTC](https://discourse.nodered.org/t/email-attachment-invalid-file/74550/15 "2023-02-19T18:18:22Z")

</div>

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