Skip to content

Instantly share code, notes, and snippets.

@aydrian
Created January 5, 2021 13:28
Show Gist options
  • Save aydrian/9f585cc495dfbadebcc0217e5ece4a80 to your computer and use it in GitHub Desktop.
Save aydrian/9f585cc495dfbadebcc0217e5ece4a80 to your computer and use it in GitHub Desktop.
Send an email using Courier with SparkPost that includes an inline image.
/*
Required environment variables:
COURIER_AUTH_TOKEN
This will grab an image and convert it to a base64 encoded string.
Use `cid:bulbasaur.png` as the image path in the Courier Notification Designer.
*/
require("dotenv").config();
const { CourierClient } = require("@trycourier/courier");
const fs = require("fs");
const main = async () => {
const courier = CourierClient();
const imageData = fs.readFileSync("bulbasaur.png", { encoding: "base64" });
const { messageId } = await courier.send({
eventId: "BY14E1G7KKMNFAJVYNA1A55WH2F5",
recipientId: "AYDRIAN10036",
override: {
sparkpost: {
body: {
content: {
inline_images: [
{
name: "bulbasaur.png",
type: "image/png",
data: imageData
}
]
}
}
}
}
});
console.log(messageId);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment