dd_prairie
dd_prairie
CDCloudflare Developers
Created by dd_prairie on 5/5/2025 in #workers-help
Email Routing - Forwarding to multiple recipients
Hello, We are attempting to forward emails to multiple verified recipients. Such as:
export default {
async email(message, env, ctx) {

for (let recipient of recipients) {
await message.forward(recipient);
}
}
}
export default {
async email(message, env, ctx) {

for (let recipient of recipients) {
await message.forward(recipient);
}
}
}
That works generally, but sometimes one recipient's message.forward will result in an error, such as a 451. When this occurs, the processing halts, and further message.forward()'s don't occur (which makes sense given the code; i.e. [email protected] fails, [email protected] isn't attempted because the prior error). With that, we've attempted handling the errors, and using Promise.all()/allSettled(), but the results are still less than ideal: - just have a log of the error and no retry was attempted. - the run is "successful" even if there was an error (which understandably won't retry) - or we get a similar behavior as before where it stops at the error and no further message.forward()'s are processed
export default {
async email(message, env, ctx) {
var promises = [];
recipients.forEach(recipient => {
promises.push(
message.forward(recipient)
.then(() => console.log(`Email forwarded to: ${recipient}`))
.catch((err) => {
console.log(`Forwarding email failed to ${recipient}. Error: ${err}`);
throw(err);
})
);

});
await Promise.all(promises);
}
};
export default {
async email(message, env, ctx) {
var promises = [];
recipients.forEach(recipient => {
promises.push(
message.forward(recipient)
.then(() => console.log(`Email forwarded to: ${recipient}`))
.catch((err) => {
console.log(`Forwarding email failed to ${recipient}. Error: ${err}`);
throw(err);
})
);

});
await Promise.all(promises);
}
};
With that, is there any automatic retry logic when a Runtime API/Email Routing Worker fails (or specifically in a case of a 451)? I'm also open to any alternatives/suggestions for approaching something like this. Thanks for your time!
1 replies