生成 XML 文件并将其附加到电子邮件中 ​​(Node.js)

Generate XML file and attach it in email (Node.js)

如何使用 nodemailer 创建 XML 文件并将其附加到电子邮件中?

这是 XML 文件的格式:


<?xml version="1.0" encoding="UTF-8"?>
<CaseData>
   <CaseNo>329388m1</CaseNo>
</CaseData>

撰写电子邮件的代码:

const mailOptions = {
    from: 'andreifletcherssolicitors@gmail.com',
    to: '97andreienache@gmail.com',
    subject: 'Test',
    text: 'Hello World!'
};

可以使用xmlbuilder更容易生成xml

var builder = require('xmlbuilder');

var obj = {
  CaseData: {
    CaseNo: '329388m1'
  }
};

var xml = builder.create(obj).end({ pretty: true});
console.log(xml);

然后像这样用 nodemailer 附加它

let message = {
    ...
    attachments: [
        {
            filename: 'myxmlfile.xml',
            content: xmlStringAbove,
            contentType: 'text/xml'
        }
    ]
}