I'm sending an email using nodemailer
and in case a send is faild, I wanna use a different host in a retry.
I tried:
let tries = 0;
let maxTries = 2;
const sendEmail = async () => {
tries += 1;
if (tries === maxTries) return;
const transporter = nodemailer.createTransport({
host: tries === 1 ? host1 : host2,
port: port,
secure: false
});
// mailOptions object here....
await transporter.sendMail(mailOptions, (err, info) => {
if (err) {
return sendEmail();
} else {
console.log('Email Sent Successfuly');
}
});
}
It works but it feels a bit cumbersome.
Is there a better way to implement this feature?