Check if email has already been forwarded to an address in email worker

What is the name of the domain?

What is the issue you’re encountering

Email worker sending email to both addresses when delivery to one address fails

Can you elaborate on what you mean with this specific part, and how deep it goes?

Are you just looking for some kind of “fail over”, as this part could sound like?

So that if the attempt to forward the message to Alice has failed, it will try forwarding it to Bob instead?

→ E.g. “[email protected]” is attempting to forward to “[email protected]”, but since the delivery to “[email protected]” is failing, it will then try forwarding it to “[email protected]” instead?

Basically, at the moment my email worker forwards emails to both a protonmail account and an outlook account. Delivery to the outlook account fails sometimes, so what I’m assuming happens is that the email worker keeps running until delivery to both addresses succeeds. The only problem is that, every time the worker runs again, it sends another copy of the email to the protonmail account, resulting in a few tens of the same email going to that address. So I want to check whether that email has already been sent to a particular address, if possible.

Unfortunately, that is what is happening with free mails. :frowning:

Sometimes Google (Gmail) is having anxiety about messages being delivered to them, and some other times, it is Microsoft (Hotmail/Live/Outlook) having anxiety.

I would more likely think that it is depending on how exactly you’ve implemented the Email Worker.

Are you able to share some code?

Do you still have (some of) these duplicates?

If so, are you able to check the message headers on the duplicates, and verify:

  1. Do all these message have a “Message-ID” header?

  2. If you check the contents of the “Message-ID” header, if available, is it always an 100% identical “Message-ID” header on all of these duplicates?
    → If one of the messages has “Message-ID: <[email protected]>”, is that the exact same on all “few tens of the same mail” you see?

It wouldn’t be technically impossible to do something like that, - however, it would require some kind of (possibly advanced) coding.

Including obviously something to store the data about whether the message has been forwarded or not.

Google (Gmail) has for many years been refusing to accept deliveries, that are not confirming to standards, such as as e.g. RFC 5322 from October 2008.

If the message you send towards Google (Gmail) is missing various headers, such as e.g. “From”, “To”, and/or “Message-ID”, or possibly having duplicate headers of those, Google (Gmail) will refuse the delivery.

The “Message-ID” is supposed to be unique for each message, and used in email clients to combine multiple messages in a thread, - together with a few other headers.

So if you’re willing to mandate the existence of that header, - you can possibly accomplish what you want, in a more simple way by e.g. saving a “tuple” of:

  1. Sender address
  2. Destination address
  3. Message-ID

And then in the worker check whether a such combination already exist, - and if not, then try to forward, and if the forward appears to be a success, then save the “tuple” of that combination.

By leaning on to the “Message-ID” header as here, I would then refuse deliveries without a “Message-ID”.

But alternatively, if you’re not willing to sacrifice (most likely spam, or otherwise misconfigured systems), - you can attempt to make some message body hash instead, and compare against that, e.g.:

  1. Sender address
  2. Destination address
    3a. SHA384 hash of HTML content.
    3b. SHA384 hash of plain text content.
    3c. Possibly both of 3a + 3b.

The “tuple” can eventually be mixed together with the “Subject” header field, too.

With a hash of the message contents, it could be a way avoid leaning (too much) on the presence of various headers, that some (bad) senders maybe aren’t adding to the message, or are possibly (and incorrectly) re-using for multiple distinct messages.

Cloudflare D1 could for example be an option, in order to store the data needed for something like this.

Sorry for the severe delay; I was trying to finish up a few projects before June.

Regarding sharing the code:

export default {
  async email(message, env, ctx) {
    const forwardList = [
      "[email protected]",
      "[email protected]"
    ];
    for (const address of forwardList) {
      await message.forward(address);
    }
  }
}

And regarding the duplicates, they seem to have the same Message-ID headers as well! At least on the three I looked at; I didn’t really have the frame of mind to look at them all! :sweat_smile:

Could you give me an example of the javascript or python I would have to implement in order to achieve this? I don’t really know how to store data between retries yet!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.