Email Worker to Forward to Multiple Emails

I am trying to use an email worker to forward from 1 email address to a few gmail accounts. Below is what I have and when I test it in the code editor it works; however, when I send from a real email account (after deploying) the delivery fails in my CF activity log and I get the following message returned to the sending account.

Email Message:

The response from the remote server was:

521 5.3.0 Upstream error, please check {link here} for possible reasons why.

Code:

export default {
  async email(message, env, ctx) {
    
    const allowList = ["[email protected]"];
    
    if (allowList.indexOf(message.headers.get("to")) == 0) {
      await message.forward("[email protected]");
      await message.forward("[email protected]");
      await message.forward("[email protected]");
    }
  }
}

Tail your email worker.

You can use Wrangler tail from the CLI or go to Workers โ†’ your worker โ†’ Logs โ†’ start log stream.

Once youโ€™ve got your worker running, send an email. Your email should appear as an event, and you should see the error it is giving.

If I had to guess, do you have every one of those gmails addresses as verified destination addresses within Email Routing?

1 Like

Did you find the problem?

This works

const emailsMap = new Map([
  ["[email protected]", ["[email protected]", "[email protected]"]]
]);

export default {
	async email(message, env, ctx) {
	  const recipients = emailsMap.has(message.to) ? emailsMap.get(message.to) : [];
	  const promised = [];
	  recipients.forEach(recipient => {
		  promised.push(message.forward(recipient));
		  console.log(`Email forwarded to: ${recipient}`)
	  })
	  await Promise.all(promised)
	}
};
3 Likes

I have found this also appears to work. You must have the forwarding addresses already authorized destination addresses.

export default {
async email(message, env, ctx) {
await message.forward(โ€œ[email protected]โ€);
await message.forward(โ€œ[email protected]โ€);
},
};