I am using Node.js and Axios to send emails via SparkPost Transmission API. I am able to get the emails successfully, however, I am not able to get the attachments. The Spark Post documentation says that we can get the attachment files using content.attachments, but still I was able to get the mail, but did not receive the expected attachment file.
Attached below is the code snippet:
static async sendModelReport() {
const data = {
content: {
template_id: process.env.emailTemplate,
attachments: [{
name: 'report.pdf',
type: 'application/pdf',
data: 'Q29uZ3JhdHVsYXRpb25zLCB5b3UgY2FuIGJhc2U2NCBkZWNvZGUh',
}], // taken from example and tried others too.
},
recipients: [{
address: process.env.internalEmail,
substitution_data: {
environment: process.env.NODE_ENV,
},
}],
};
try {
const response = await axios({
method: 'post',
url: `${process.env.emailServiceBaseURL}/api/v1/transmissions`,
data,
headers: {
'Content-Type': 'application/json',
Authorization: process.env.emailServiceKey,
},
});
if (response.data && response.data.results) {
return response.data.results;
}
} catch (error) {
console.log(error.response.data);
throw new Error(`Email Service Error: ${error.message}`);
}
}
It turns out spark post doesn't support sending attachements with stored templates. One work around is that we can get the stored template and use that template html to send mail.
}