Back to all posts

Support Mail Without a Ticket System

Sixtus Miracle AgboSixtus Miracle Agbo
6 min read
Support Mail Without a Ticket System

TL;DR: I built support mail for PuntuQuest from three pieces: a Cloudflare Email Worker that catches incoming mail, a Firebase Cloud Function that stores it, and a Next.js admin panel that threads conversations and replies through Resend. No Zendesk, no monthly seat fee. Then an attacker pass found sender spoofing, thread leaks between players, and a missing flood cap. Two rounds of fixes later, it works.

PuntuQuest needed a support email. The app was on the App Store, players were writing in, and I was reading their messages from a forwarded inbox with no way to reply as support@puntuquest.com.

The obvious answer was a ticket system. Zendesk, Freshdesk, Intercom. They all work. They all cost money per seat per month, and for a solo indie app that gets a handful of support emails a week, that felt wrong.

So I built my own. Three pieces, each doing one thing.

The three pieces

Piece 1: a Cloudflare Email Worker. Cloudflare already handled PuntuQuest's DNS, so Email Routing was free. I wrote a small Worker that runs on every email to support@puntuquest.com. It does two things in parallel: forwards the email to my personal inbox (so nothing is lost if everything else breaks), and posts the raw email to a Cloud Function. If both fail, it rejects the message so the sender knows it did not arrive.

const [forwarded, posted] = await Promise.allSettled([
  message.forward(env.OWNER_EMAIL),
  raw ? toPanel(raw, env, post) : Promise.resolve("too large"),
]);
if (forwarded.status === "rejected" && posted.status === "rejected") {
  message.setReject("Support mail cannot be received right now.");
}

Piece 2: a Firebase Cloud Function. The function takes the raw email, checks a shared secret, and stores it in a Firestore collection. It is a holding area. The function does not parse the email, does not thread it, does not decide anything. It stores the raw bytes in parts under Firestore's 1 MiB document limit and waits.

The reason it does so little is that the panel cannot receive mail directly. The function's job is to hold the mail until the panel asks for it.

Piece 3: a Next.js admin panel. When I open the Support section, the panel reads the inbox, parses each email into plain text (HTML-only mail is converted), threads it by Message-ID, and empties the inbox. Replies go out through Resend with proper In-Reply-To and References headers, so the player's email client shows the conversation as a thread.

Threading

Email threading is built on Message-IDs. Every email carries one. When you reply, your email client puts the original's Message-ID in the In-Reply-To header and lists the whole chain in References.

I keep a Firestore collection that maps every Message-ID I have seen, mine and theirs, to the thread it belongs to. When a new email arrives, the panel looks up its In-Reply-To and References against that collection. If it finds a match, the email joins that thread and reopens it.

There is a catch: anyone can put any In-Reply-To on an email. If I threaded purely on Message-ID, someone who learned or guessed one could write into another player's thread. So threading has two extra conditions: the sender must be verified (more on that below), and the sender's address must match the thread's player. Anything else starts a new thread.

Sender verification

Anyone can put any From address on an email. That is how email works. The only thing that tells you the address is real is what the receiving mail server checked on the way in.

Cloudflare Email Routing runs SPF, DKIM, and DMARC on every email it accepts. It writes the results in an Authentication-Results header. I read that header and mark a letter as verified only when DMARC passes for the sender's own domain.

const results = headers
  .find((line) => line.key === "authentication-results")
  ?.value ?? "";
// check the topmost result from our receiving server,
// with dmarc=pass for the sender's domain

Only the topmost Authentication-Results header counts, because it is the last one added, by the server that took the mail in. Any header below it came with the mail and could say anything a sender chose. Comments and quoted strings are stripped out first, because a sender can put a semicolon inside them to fake a result boundary.

An unverified letter still shows up in the panel. It carries a warning: "Our mail server could not confirm that a letter here came from this address. Anyone can put an address on a letter: confirm by reply before deleting or changing an account."

What the attacker found

After the support system was built and working, I ran an attacker pass. A separate session, fresh context, whose only job was to break it. It found real problems.

Missing admin checks. Next.js layouts are not re-run on every client navigation. I had the admin check in the layout, which meant navigating to a page inside the panel could skip it. The fix: every page and every server action checks the admin session as its first line, and a test walks every page and action file to verify none skip it.

Sender spoofing into threads. The first version threaded on Message-ID alone. If mail named a known Message-ID in its References, it joined that thread. No sender check. An attacker who knew or guessed a Message-ID could write into someone else's conversation. The fix: a letter joins an existing thread only when it passes DMARC and its address matches the thread's player. Everything else starts a new thread.

No flood cap. There was nothing stopping someone from sending thousands of emails to support@, burying real mail under noise. Each one would start a thread and sit in the Open tab. The fix: unverified emails are rate-limited per hour. Past the cap, the rest go to a "Set aside" tab instead. Verified mail is never held back, so nobody can bury a player's real mail by writing in their name.

Thread leaks on erase. When erasing a thread (for a player who asks for their mail to be deleted), the first version deleted the thread and its messages but left the Message-ID mappings behind. A later reply from the same player could land in a thread that no longer existed. The fix: erasing a thread also removes its Message-IDs and the player's set-aside letters. The Message-IDs go first, so no letter arriving during the erase can land in the thread being deleted.

Double sends. Each reply carries a nonce generated in the form. The first send records the message as "sending" before it goes out. If the admin clicks Send twice, the second attempt sees the record and stops. If the first send failed and you want to retry, you can, but only with the same words, so the record never says what did not go. This was partially there already but had gaps the attacker tightened.

The second round

The first round of fixes went in. I ran the attacker again. It found more:

The sender verification was checking Authentication-Results but was not stripping comments and quoted strings from the header value. A sender could put ; dmarc=pass header.from=gmail.com inside a comment or a quoted string in an earlier result, and the parser would read it as a real pass. The fix: strip comments and quoted strings before splitting on semicolons, and reject the header entirely when one is left unclosed (which no honest server writes).

The flood cap was counting by address. An attacker could use a different From address on each message and bypass it entirely. The fix: one counter for all unverified mail, regardless of sender. The limit is on the volume, not the source.

After the second round, the attacker could not find another way in.

What it costs

Nothing monthly. Cloudflare Email Routing is free. The Worker is on the free tier. The Cloud Function is a few invocations a week. Firestore is well within the free tier for this volume. Resend's free tier covers 100 emails a day, which is orders of magnitude more than I need for support replies.

The cost was time. Building the support desk, the threading, the reply pipeline, the email worker, and two rounds of attacker fixes came to about 4,000 lines across 72 files. For a handful of support emails a week, that is a lot of code.

But the alternative was paying for a service that does all of this, handing it my players' email addresses, and trusting it to stay affordable. For an indie app, I would rather own the system and understand every part of it.

What I was working with

  • Next.js 16 for the admin panel
  • Cloudflare Email Workers and Email Routing
  • Firebase Cloud Functions and Firestore
  • Resend for outbound replies
  • Real MIME fixtures for the tests (no test ever reaches a real inbox)
Share this post
Sixtus Miracle Agbo

Sixtus Miracle Agbo

Full-Stack Developer crafting high-performance web and mobile applications. I write about software development, technology, and lessons learned building real products.

Get in touch

Subscribe to my newsletter

New posts on web & mobile development, straight to your inbox. No spam, unsubscribe anytime.