How to download attachments in node-red-node-email

Hello everybody,
I start workig with Node-RED a few weeks ago and I create a flow that is using node-red-node-email to get certain unread emails from INBOX.
Can someone help me on how download the attachments (excel) from the email?

My email flow

Code from function node:

//get nomenclatura
const regex = /Nomenclatura:\s*(.+)/;
const found = msg.payload.match(regex);
if (found != null) {
    msg.nomenclature = found[1];
} else {
    msg.nomenclature = ""
}

//get excel atttachments only - ignore others
var excelArray = [];
for (var i = 0; i < msg.attachments.length; i++) {
    var attachment = msg.attachments[i];
    if (attachment.type === "attachment") {
        if (attachment.contentType === "application/vnd.ms-excel" || attachment.contentType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
            //push array
            excelArray.push(attachment);
        }
    }
}
msg.attachments = excelArray;

return msg;

Thanks

Click on the email node then go to the sidebar and click the 'Help` icon
email_help

Then take a read of the information and see if that answers your question.

@zenofmud I have read the attatchment session on the documentation. And found the attachment "object" im msg.

[{"type":"attachment","content":[208,207,17,224,161,177,26,225,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,62,0,3,0,254,255,9,0,6,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,54,0,0,0,0,0,0,0,0,16,0,0,254,255,255,255,0,0,0,0,254,255,255,255,0,0,0,0,53,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,9,8,16,0,0,6,5,0,84,56,205,7,193,0,2,0,6,8,0,0,225,0,2,0,176,4,193,0,2,0,0,0,226,0,0,0,92,0,112,0,19,0,0,65,110,97,32,67,114,105,115,116,105,110,97,32,77,111,114,101,110,111,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,66,0,2,0,176,4,97,1,2,0,0,0,192,1,0,0,61,1,2,0,1,0,156,0,2,0,17,0,25,0,2,0,0,0,18,0,2,0,0,0,19,0,2,0,0,0,175,1,2,0,0,0,188,1,2,0,0,0,61,0,18,0,0,0,0,0,66,84,149,31,56,0,0,0,0,0,1,0,88,2,64,0,2,0,0,0,141,0,2,0,0,0,34,0,2,0,0,0,14,0,2,0,1,0,183,1,2,0,0,0,218,0,2,0,0,0,49,0,30,0,220,0,0,0,8,0,144,1,0,0,0,2,0,0,7,1,67,0,97,0,108,0,105,0,98,0,114,0,105,0,49,0,30,0,220,0,0,0,8,0,144,1,0,0,0,2,0,0,7,1,67,0,97,0,108,0,105,0,98,0,114,0,105,0,49,0,30,0,220,0,0,0,8,0,144,1,0,0,0,2,0,0,7,1,67,0,97,0,108,0,105,0,98,0,114,0,105,0,49,0,30,0,220,0,0,0,8,0,144,1,0,0,0,2,0,0,7,1,67,0,97,0,108,0,105,0,98,0,114,0,105,0,49,0,30,0,220,0,0,0,8,0,144,1,0,0,0,2,0,0,7,1,67,0,97,0,108,0,105,0,98,0,114,0,105,0,49,0,42,0,104,1,0,0,54,0,144,1,0,0,0,2,0,0,13,1,67,0,97,0,108,0,105,0,98,0,114,0,105,0,32,0,76,0,105,0,103,0,104,0,116,0,49,0,30,0],"contentType":"application/vnd.ms-excel","partId":"2","release":null,"contentDisposition":"attachment","filename":"6 PĂłs-Pago Teste EFATURA ICCID 4008404_4008409.xls","headers":{"content-type":{"value":"application/vnd.ms-excel","params":{"name":"6 PĂłs-Pago Teste EFATURA ICCID 4008404_4008409.xls"}},"content-description":"=?iso-8859-1?Q?6_P=F3s-Pago_Teste_EFATURA_ICCID_4008404=5F4008409.xls?=","content-disposition":{"value":"attachment","params":{"filename":"6 PĂłs-Pago Teste EFATURA ICCID 4008404_4008409.xls","size":"28672","creation-date":"Tue, 13 Dec 2022 13:04:10 GMT","modification-date":"Tue, 13 Dec 2022 13:00:33 GMT"}},"content-transfer-encoding":"base64"},"checksum":"0e9e7cece72f119073619538424316a8","size":28672}]

I have tried to download the file using the following code snippet in the function node to extract and save the attachments:

var attachments = msg.payload.attachments;
for (var i = 0; i < attachments.length; i++) {
   var attachment = attachments[i];
   // Save the attachment to disk
   node.fs.writeFile("C:\\{Users}\\Username\\Documents\\attachments" + i, attachment.content, 'base64', function(err) {
       if (err) {
           node.error("Failed to save attachment: " + err);
       } else {
           node.log("Attachment saved successfully.");
       }
   });
}

But didn't work... is there other way to do this?

You cant use the Node API in this manner - At least I don't think that is possible.
What you need to do, is send the file content to a write file node.

var attachments = msg.payload.attachments;
for (var i = 0; i < attachments.length; i++) {
    var attachment = attachments[I];
    // Save the attachment to disk (sent to the write file node)
    node.send({
        payload: attachment.content,
        encoding: 'base64',
        filename: `C:\\Users\\<Username>\\Documents\\attachments\${i}`
    })

}

Screenshot 2023-02-04 at 16.52.56

You also need to ensure the Node RED process has write access to the location, you wish to save files.

The attachments don't need to be 'downloaded' because they should be in the msg that exits the email node. If you want to write the attachment to a file, extract that portion of the msg object and put it into msg.payload ( you could use a change node to do this) and then write the data using the file-out node as @marcus-j-davies suggested.

@marcus-j-davies

Thanks! That worked.