使用node.js发送附件数组
我需要使用带有多个附件的NodeEmailer发送电子邮件,但这些附件的数量必须由数组的大小定义。我知道NodeEmailer可以发送多个附件,但我不知道如何发送数量可变的附件 这是我的代码:使用node.js发送附件数组,node.js,nodemailer,Node.js,Nodemailer,我需要使用带有多个附件的NodeEmailer发送电子邮件,但这些附件的数量必须由数组的大小定义。我知道NodeEmailer可以发送多个附件,但我不知道如何发送数量可变的附件 这是我的代码: const files = get(req, "body.data.files"); files.forEach(file => { senderMail.send(file, { name: get(req, "body.data.files.name"), url
const files = get(req, "body.data.files");
files.forEach(file => {
senderMail.send(file, {
name: get(req, "body.data.files.name"),
url: get(req, "body.data.files.url")
});
});
let mailOptions = {
from: "Me", //
sender address
to: data.personal.user_email, // list of receivers
subject:
"An email with attachments"
text: "someText",
html: "",
attachments: [
{
filename: name,
path: url
}
]
};
一些数据是从JSON获取的。准备一个NodeEmailer格式的数组,然后将其附加到邮件对象
const files = get(req, "body.data.files");
const attachments = files.map((file)=>{
return { filename: file.name, path: file.url };
});
let mailOptions = {
from: "Me", //
sender address
to: data.personal.user_email, // list of receivers
subject:
"An email with attachments"
text: "someText",
html: "",
attachments: attachments
};
你看过文档了吗,这里:它只是附件上的一个数组。你已经有了一个。是的,但是那样的话,我必须输入我要发送的数组的每个元素,作为发送的数组的一个元素,我希望根据json中的数据添加一个新的附件,如果没有,则添加一个新的附件。是的。你必须建立阵列!您可以使用array.push,也可以在附件列表上使用map。