使用node.js发送附件数组

时间:2018-02-21 23:58:41

标签: node.js nodemailer

我需要使用带有多个附件的nodemailer发送电子邮件,但这些附件的数量必须由数组的大小来定义。我知道nodemailer可以发送多个附件,但我不知道如何发送可变数量的附件。

这是我的代码:

  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。

1 个答案:

答案 0 :(得分:3)

以Nodemailer格式准备一个数组,然后将其附加到邮件对象。

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
};