★ Cloudflare: Email Workers
• 317 words • 2 min • updated
My journey into the serverless world hereby starts.
And I am very happy to have chosen the best™ provider in the universe to delight me in this sweet learning curve. ✨
Problem statement: I have my own domain, you have probably noticed that.
Given a specific hypothetical email address neetflex@1 within my own
domain, make it an email recipient that automatically forwards to both
user1@example.com and user2@example.com.
The first attempt involves setting up an Email
Routing rule. The issue is
that you can only do 1:1 mappings: neetflex@ -> user1@example.com
would work, but it is impossible to add user2@example.com for the same
recipient.
Workaround: Email Workers:
With Email Workers you can leverage the power of Cloudflare Workers to implement any logic you need to process your emails and create complex rules. These rules determine what happens when you receive an email.
Aside: The platform is such a delight to use, I am deeply enchanted. Their documentation is absolutely superb. I am getting seduced and couldn’t agree more with Daniel Miessler.
I proceed to create an Email Worker with the following code2:
export default {
async email(message, env, ctx) {
if (message.from !== "info@account.neetflex.com") {
return;
}
for (const address of [
// keep-sorted start
"user1@example.com",
"user2@example.com",
// keep-sorted end
]) {
await message.forward(address);
}
}
}…named neetflex-magic-link.
There’s an integrated testing tool that makes it a breeze to ensure my worker logic works as intended.
Once I am satisfied with the results, I deploy the email worker.
Final step: create an email route rule from neetflex@{domain} to the
aforementioned email worker. Done, profit!$!3.
At the time of this post, you can choose among JavaScript, TypeScript, Python and Rust to code your worker in.
Original idea: source from Cloudflare Community.