2

I'm trying to send an email with an attachment with node-sparkpost (which uses the transmissions API under the hood).

Why does the following code send the email, but without the attachment?

"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);
  });
John Keyes
  • 5,479
  • 1
  • 29
  • 48

1 Answers1

1

A classic self rubber ducking moment.

The attachments property MUST be a child of 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")
        }
      ]
    },
John Keyes
  • 5,479
  • 1
  • 29
  • 48