Email attachment | Invalid file

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

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

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

try the below.

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

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

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
[{...},{...},{...}]

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 ?

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

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
[
   {
       "filename": "my-text-file1.txt",
       "content": payload[0],
       "contentType":"text/plain"
    },
     {
       "filename": "my-text-file2.txt",
       "content": payload[1],
       "contentType":"text/plain"
    }
]

Image 1

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

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

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 ?

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

Best Regards,
Mani

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

You should be getting something like below.

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

Also try using text/csv as the contentType

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 ?

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

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

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:

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

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

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.

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

1 Like

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