Cloudflare Free Shadow Email

Forget those sketchy temp email sites โ€“ we're gonna build our own, using the power of Cloudflare! ๐Ÿฆธโ€โ™‚๏ธ

Think of it like this: you want to sign up for a website, but you're not sure if they'll start bombarding you with spam. You could use your personal email, but then your inbox becomes a war zone. โš”๏ธ

That's where our temp email setup comes in! We can create random email addresses at your domain, have Cloudflare forward the emails to your real account, and then just delete the temporary address. It's like a secret agent email cloak! ๐Ÿ•ต๏ธโ€โ™€๏ธ

What you'll need:

  • A Cloudflare Account: Don't have one? No problem! It's free to sign up at cloudflare.com. Seriously, it's free like free pizza (okay, maybe not that good, but still free). ๐Ÿ•

Cloudflare user homepage

  • A Domain Name: You'll need a domain you own. If you don't have one, you can grab a super cheap (or even free, sometimes!) one at godaddy.com. Pick a fun one!

cloudflare-domain-dns

Cloudflare Email Routing

Step 1: Add your domain to Cloudflare

First things first, head over to the Cloudflare homepage and add that domain you own into your account. Cloudflare will guide you through the DNS setup. Don't worry, it's not as scary as it sounds. It's like teaching a cat to high-five โ€“ a little patience and you'll get there. ๐Ÿˆโ€โฌ›

Step 2: Dive into Email Routing

Once your domain is all settled in with Cloudflare, go to the domain's dashboard. It's like your domain's control center! On the left-hand side, you'll see a tab called Email. Click on it!

You'll be greeted with an overview section โ€“ look for "Email Routing" and click on it. Cloudflare will show you a big, friendly button saying Enable Email Routing. Go ahead and smash that button!

Next, it will ask you to confirm by Add records and enable. Give it a click, and boom! Cloudflare is now ready to handle your temporary emails. โœจ

cloudflare-email-routing

Configure Email Routing

Cloudflare Email Routing is like having a super-organized assistant for your email. It lets you create all sorts of custom email addresses for your domain and then it automatically forwards those emails to your real inbox. Think of it as a smart redirect for your messages.

Routing: Custom Address

Let's face it, using just one email address for everything is like wearing the same pair of socks for a week. (Don't judge, we've all been there). With Cloudflare's Email Routing, you can get fancy with custom email addresses!

Want a special address just for online shopping? No problem! How about one for that side hustle you've been planning? You can make as many as you want, and Cloudflare's super-simple web interface is like child's play to use. Just point, click, and bamโ€”you've got a shiny new email address.

Think of it like having different mail slots for different purposes:

  • shopping@yourdomain.com - For all those online retail therapy moments.
  • work@yourdomain.com - Keep your work emails separate from everything else.
  • secretproject@yourdomain.com - Because every secret project deserves its own email address.

The best part? You still get all those emails in your one real inbox.

Routing: Catch-All Forward to Email

Ever worry you'll miss an email because someone types your address wrong? Or maybe you want to be a bit moreโ€ฆ creative with your addresses without having to set up a rule for each and every single one. That's where the catch-all address comes to the rescue!

Enabling the catch-all means that ANY email sent to something@yourdomain.com that you haven't specifically set up will get forwarded to your chosen inbox. It's like the email equivalent of a big net that catches everything!

So, if someone accidentally sends an email to oops@yourdomain.com or if you just want to have some fun like pineapple@yourdomain.com you'll still see it.

Why is this awesome?

  • No Missed Emails: Even typos are caught.
  • Super Flexibility: You can use any address you want without having to set up rules for them.
  • Fun: Experiment with silly email addresses โ€“ why not?

So go ahead, create those custom email addresses, enable that catch-all, and bask in the glory of an organized inbox. You deserve it!

Routing: Catch-All Forward to Workers

Now, things get really interesting! If you're feeling adventurous (and a little bit nerdy, like me!), you can have that "catch-all" email go straight to a Cloudflare Worker.

What's a worker, you ask? Think of it as a mini-program that lives in the Cloudflare cloud. It can do all sorts of cool things, like:

  • Filtering: Want to only accept emails from certain people? A worker can do that!
  • Custom Routing: Send emails to different inboxes based on keywords in the email address? Yup, the worker can handle it!
  • Special Actions: Maybe you want to send an auto-response, save the email to a database, or even trigger a cat-themed disco party.

By using a worker, you're taking full control of your email routing. It's like being a DJ for your inbox, mixing the perfect beats of email organization!

the index.ts file is the main entry of the worker.

import { mailHandler } from './email'

export default {
	email: mailHandler,
} satisfies ExportedHandler<Env>;

dynamic forward routing by database rules.


import { ExecutionContext, ForwardableEmailMessage } from "@cloudflare/workers-types";
import { TempEmail } from "./types";

export async function mailHandler(message: ForwardableEmailMessage, env: Env, ctx: ExecutionContext): Promise<void> {
    const { DB: db } = env;
    const q = `SELECT * FROM temp_emails WHERE email = ? AND expire_ts > ?`;
    const nowTs = Math.floor(Date.now() / 1000);
    const tempEmail = await db.prepare(q).bind(message.to, nowTs).first<TempEmail>();
    if (!tempEmail) {
        console.info(`No temp email found for ${message.to}`);
        message.setReject("Address not allowed");
        return;
    }
    try {
        await message.forward(tempEmail.forward_email);
    } catch (e) {
        console.error(`Error forwarding email: ${e}`);
    }
}

or you can just forward all the email to a fixed email address.

export async function mailHandler(message: ForwardableEmailMessage, env: Env, ctx: ExecutionContext): Promise<void> {
    const fixedForwardAddr ="yourFixForwardEmailAddr@gmail.com"
    try {
        await message.forward(fixedForwardAddr);
    } catch (e) {
        console.error(`Error forwarding email: ${e}`);
    }
}

Usages

Now your can send email to xx@yourdomain.com and it will be forwarded to your personal email address.

cloudflare worker email forward log


Was this article helpful to you?
Provide feedback

Last edited on December 27, 2024.
Edit this page