Email Workers API: message.headers Does Not Behave as Expected

What is the name of the domain?

workers.dev

What is the isssue you’re encountering

The message.headers.get(‘to’) method in the Email Workers API only returns the first recipient in the “To” address header, rather than all recipients separated by commas.

What are the steps to reproduce the issue?

According to the Cloudflare Email Routing documentation, the EmailMessage object has a headers property that is supposed to provide the Worker with the email’s header information. However, I’ve noticed that when there are multiple recipients in the “To” address of the header, message.headers.get('to') only returns the first recipient.

Per the MDN documentation on the Headers.get method, the expected behavior is for it to return all values, separated by commas.

Hi there,

Make sure the email addresses are already authorized. Then you can maybe use something like this:

export default {
  async email(message, env, ctx) {
    await message.forward("[email protected]");
    await message.forward("[email protected]");
    await message.forward("[email protected]");
  },
};

Take care.

Hey @mcorreia, thanks for replying! My use case isn’t as simple as forwarding to several email addresses. I need to extract several pieces of information from the email header, send them to an LLM provider, let the LLM analyze the email content, and make appropriate decision(s). Therefore, it is critical that I can get all recipient addresses in the email header (instead of only the first one), so that the large language model can make better decisions when there are multiple recipients.

*Just to clarify, I am aware that the email content can be read from the EmailMessage.raw StreamedReader, and I am planning to use some third-party libraries to parse the email if you don’t have plans to fix the EmailMessage.headers API. Anyway, it might be better to just implement the Headers API correctly to include all recipient information, as other users may get confused when it only returns the first recipient. Hope you have a great day!