Cloudflare offers a free solution for setting up advanced email forwarding using Cloudflare Email Routing and Cloudflare Workers. This repository provides a boilerplate for the worker and a quick guide on how to set it up.
Tip
Simplify your life! Instead of using Cloudflare, consider the beautiful ImprovMX service. It offers similar functionality in a much easier way, and it's free if you need just one domain. Definitely worth the money if you need more features!
-
Create your own repository
Click Use this template at the top right corner and create your own repository, either public or private. -
Sign up for Cloudflare and add your domain
If you don’t already have a Cloudflare account, sign up and add your domain. Be aware that you will need to migrate your DNS records, which may take some time. -
Create the email forwarding worker
In Cloudflare, go to Workers & Pages, click Create, then Create Worker. Name it something likeemail-forwarding
and click Deploy. You don’t need to modify the code at this point. Click Continue to the project. -
Connect your worker to the repository
Navigate to Settings, scroll to the Build section, and click Connect in the Git repository box. Authorize your GitHub account, select the repository (e.g.email-forwarding
), and click Connect. -
Setup forwarding and destination addresses
Configure the forwarding and destination email addresses (see below) in your repository. -
Set up email routing for your domain
Go to your domain, click Email, then Email Routing, and Get Started. Afterward, click Skip getting started, enable Email Routing, and click Add records and enable.Then, go to the Routing Rules tab. Next to the catch-all address, click Edit, choose Send to a Worker as the action, and select your
email-forwarding
worker. Save and enable the rule.In case you have multiple domains, just repeat this step for each: you can use a single worker for multiple domains.
You will also need to verify the destination addresses in Cloudflare: go back to your domain, open the Destination Address tab, add the email addresses you want to forward to, and click the verification link in the email sent by Cloudflare.
-
Test it out
You're all set! 🚀 Send a test email to the address you just set up and check that it gets forwarded correctly.
If you have any troubles or feedback, feel free to create an issue.
To adjust incoming and forwarding email addresses, edit the forwardList
constant in the worker script:
const forwardList = {
"[email protected]": [
"[email protected]",
"[email protected]",
],
"[email protected]": "[email protected]",
};
The key represents the recipient email address on your domain, and the value is either a single email address or an array of addresses where messages should be forwarded.
You can also use subaddressing aka the plus sign trick. For example, emails sent to [email protected] will still be delivered as if sent to [email protected].