John Keyes
John Keyes

Reputation: 5604

node-sparkpost not including attachment in email

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 = "[email protected]";
let toAddress = "[email protected]";

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

Upvotes: 2

Views: 410

Answers (1)

John Keyes
John Keyes

Reputation: 5604

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")
        }
      ]
    },

Upvotes: 1

Related Questions