Closed antho227 closed 11 months ago
Hi @icecomagency your approach is a bit incorrect. You're actually trying to send messages one by one and the Messages
value you're passing is in the wrong format, that's why you're getting an error. Try to process messages by batches, something like this:
const Mailjet = require('node-mailjet');
const mailjet = Mailjet.apiConnect(
process.env.MJ_APIKEY_PUBLIC,
process.env.MJ_APIKEY_PRIVATE,
);
async function arraySend(array, locals) {
const batchSize = 50;
let count = 1;
for (let i = 0; i < array.length; i += batchSize) {
const batch = array.slice(i, i + batchSize);
await mailjet
.post('send', { version: 'v3.1' })
.request({
Messages: batch,
})
.then((result) => console.log(`Processed batch ${count} of ${Math.round(array.length/batchSize)},
Messages sent: `, JSON.stringify(result.body))
)
.catch((err) => console.log(err));
// this is just to track the process
count++;
}
}
Please let us know if something is unclear.
I get an error when I'd like to send emails using promises. I'm using promises because I have 889 emails to send and I know that I can send up to 50 emails per calls.
My code :
Error message :
UnhandledPromiseRejectionWarning: Error: Unsuccessful ... Mailjet