XML as Email Attachment

Hi again,

how i can build a xml that is generated as an attachment ?

I use this function: But when i put the data as attachment email send without attachment.

return [
  {
  json: {
    
  },
  binary: {
    data: {
      data: $node["XML1"].json["data"],
      mimeType: "application/xml",
      fileExtension: 'xml',
      fileName: "SCH.xml"
    }
  }
  }
]

Any idea ?

The property data (binary.data.data) should be Base64 encoded. Introduction | Docs

This should do it.

return [
  {
  json: {
    
  },
  binary: {
    data: {
      data: Buffer.from($node["XML1"].json["data"]).toString('base64'),
      mimeType: "application/xml",
      fileExtension: 'xml',
      fileName: "SCH.xml"
    }
  }
  }
]

Thanks a lot @RicardoE105 that is correct. Another option would be to use the “Move Binary Data” Node.

Ricardo looks good !

Hello,

I have a similar problem. My custom node creates a XML file which should be attached to an email.

To send the email the Send Email node is used.

I tried everything for the input data, but nothing worked:

"binaryData": {
    "data": "PD94bWwgdmVyc2lvbj0nMS4wJyBlbmNvZGluZz0nVVRGLTgnID8...",
    "mimeType": "application/xml",
    "fileName": "test.xml"
}

"binary": {
    "data": {
        "data": "PD94bWwgdmVyc2lvbj0nMS4wJyBlbmNvZGluZz0nVVRGLTgnID8...",
        "mimeType": "application/xml",
        "fileName": "test.xml"
    }
}

"data": {
    "data": "PD94bWwgdmVyc2lvbj0nMS4wJyBlbmNvZGluZz0nVVRGLTgnID8...",
    "mimeType": "application/xml",
    "fileName": "test.xml"
}

The binary data is shortened here but is correct. Here is the main part of the function which generates it:

// read the XML file content    
const fileContent = fs.readFileSync(this.filePath, "utf-8");

// get the binary data
const binaryData = Buffer.from(fileContent, "utf-8");

// convert the binary data to Base64
 const base64Encoded = binaryData.toString('base64');

// return as object in the n8n format
return {
    data: base64Encoded,  // Base64-encoded string
    mimeType: 'application/xml', // Set MIME type (e.g. application/xml for XML files)
    fileExtension: 'xml', // File extension (e.g. xml)
    fileName: 'test.xml',  // File name you want to appear
};

In the field “Attachments” in the Send Mail node I put only the name. I tried “data”, “binary” and “binaryData”, but nothing works. The email arrives every time without an attachment.

Used version of n8n: 1.83.2 (self hosted in a Docker container)

I hope somebody can help me what I do wrong. If you need more information, please let me know.

Peter