node-sparkpost不包括电子邮件附件

时间:2019-02-08 12:43:12

标签: javascript email sparkpost

我正在尝试发送一封带有node-sparkpost附件的电子邮件(该附件使用了内部的transmissions API)。

以下代码为什么发送电子邮件,但没有附件?

"use strict";
let Sparkpost = require("sparkpost");
let apiKey = "xxx";
let fromAddress = "dan@example.com";
let toAddress = "dare@example.com";

let spClient = new Sparkpost(apiKey);

spClient.transmissions
  .send({
    options: {},
    content: {
      from: fromAddress,
      subject: "The subject",
      html: "See attached file.",
      text: "See attached file."
    },
    recipients: [{ address: toAddress }],
    attachments: [
      {
        name: "attachment.json",
        type: "application/json",
        data: Buffer.from("{}").toString("base64")
      }
    ]
  })
  .then(data => {
    console.log("email mail sent");
    console.log(data);
  })
  .catch(err => {
    console.log("email NOT sent");
    console.log(err);
  });

1 个答案:

答案 0 :(得分:0)

一个经典的自我陶醉的时刻。

attachments属性必须是content的子级:

    content: {
      from: fromAddress,
      subject: "The subject",
      html: "See attached file.",
      text: "See attached file.",
      attachments: [
        {
          name: "attachment.json",
          type: "application/json",
          data: Buffer.from("{}").toString("base64")
        }
      ]
    },