For Business Owners · 8 min read

How to match a new lead to an existing account by email domain

Background: what "recognizing the same company" is for

AllyOS is the business system I built for my company with Claude Code and a team of AI agents. Sales, quoting and production all run in it.

Every B2B company runs into this. A long-standing customer hires a new buyer. Someone from another department of that same customer fills in the contact form on your website. To them, it's the same company. To your system, it's an email address it has never seen. If the system treats it as a brand-new lead and hands it to a different rep, you end up with two reps working one company, sending two quotes and telling two stories.

Think of a front desk. The visitor is a stranger, but they're wearing a badge from one of your clients. A good receptionist says "You're with that company, let me get the person who looks after you" instead of making them start from scratch. The email domain is that badge.

This post walks through the rules I designed for reading that badge, and the exceptions that look minor but cause real damage if you get them wrong. All code and names below are generalized; no business data is involved. One honest note up front: as of 2026-12-03, the design and the public-mailbox list are live, but the step that recognizes the company automatically when a lead arrives is still scheduled. I'll say which parts are live and which are design.

Why the email domain and not the company name?

The typed company name is unreliable; the email domain is the most stable signal you have.

The same company shows up on forms half a dozen ways: with or without "Inc.", different capitalization, abbreviations, typos. Worse, some company records get created automatically and named after the first contact, so a company can sit in the system as "John Smith" until somebody renames it.

The domain is different. Employees of one company almost always share the part after the @. So the company key is that part, trimmed and lowercased. The typed name is only used as a hint: if the domain matches but the name differs, the match stands and the lead gets a "name differs" flag for staff to glance at.

Three kinds of domains must be excluded, because none of them stands for a company:

  1. Public mailboxes: Gmail, Outlook, Yahoo, iCloud and their regional equivalents, shared by hundreds of millions of people;
  2. Throwaway domains: the sign-up-once-and-discard kind;
  3. Your own domain: a colleague testing the form is not a customer.

As a generic sketch:

PUBLIC = {"gmail.com", "outlook.com", "yahoo.com", "icloud.com"}  # sample; the real list is longer

def company_key(email: str) -> str | None:
    domain = email.rsplit("@", 1)[-1].strip().lower()
    if domain in PUBLIC or domain in DISPOSABLE or domain == OUR_DOMAIN:
        return None   # public, throwaway or our own: no key
    return domain     # subdomains are not folded: mail.acme.com is not acme.com

Should public mailboxes be a list or a heuristic?

A list. The rule can be simple, but the answer has to be stable.

I considered a heuristic: if more than N unrelated people share a domain, call it public. It sounds clever, but its answer moves as data arrives. Today a niche provider has two users, so it counts as "a company" and two strangers get matched as one customer. Tomorrow a third user shows up and it flips back to public. A match that was right yesterday is wrong today, and nobody can explain why.

So the live version is a plain table, seeded with 44 common public-mailbox domains (international and regional providers, plus the mailbox domains of a few large internet service providers). Admins can add or remove entries, changes take effect immediately, and every entry records who added it. It's a twin of the existing throwaway-domain list and works the same way, so nobody has to learn anything new.

What if the list misses one? The cost is bounded. A handful of people on that provider might be matched as one company, and, as the next sections explain, a wrong match only sets a flag. It never hands anyone's records to the wrong person. Add the missing line and move on.

Should subdomains and sibling domains be merged?

No. A missed match costs you one hint; a wrong match hands a stranger's lead to somebody else's customer.

mail.acme.com and acme.com are probably the same company, and so might be acme.com and acme.co.uk. But merging them correctly requires knowing which suffixes are country-level (.co.uk, .com.au), which means maintaining a public suffix table. Get it wrong and two unrelated companies get glued together.

The two errors are not symmetric:

  • Missed match: the lead stays in the shared pool and a person assigns it. You lose an automatic hint.
  • Wrong match: a stranger's inquiry goes straight to a rep as if it were an existing customer.

So prefer missing over mismatching.

Once you recognize the company, can you attach the contact automatically?

No. The system only flags the lead; attaching the person to the customer must be confirmed by staff.

This is the most important rule in the design. "Which company this person belongs to" is not just a field. It decides which company's quotes, orders and documents that person can see when they sign in to the customer portal.

An email address typed into a public website form is unverified. If the system attached people automatically whenever the domain matched, anyone could type an address at a customer's domain and work their way toward that customer's records.

So, by design, the match is written only on the lead itself: three flags for the recognized company, how it was recognized (the person already belonged to it, or by domain), and whether the typed name differs. They decide who gets the lead and what staff see; they take no part in any permission check. Staff see "Recognized: that company (owner: that rep)" on the lead, and one click on "Add to company" makes it official, with a record of who did it.

That part is still design, not live. The design also includes a guard: an error in recognition must never make a form submission fail. A customer clicking Submit should never see an error because our matching logic broke. If recognition fails, the three flags stay empty and the lead comes in for manual assignment.

What if one domain points at several companies?

Try a fixed order, and if it doesn't yield a single answer, don't guess. Tell a manager.

Older data can hold several company records on one domain (for example, when every new contact used to get their own company). The order in the design is:

  1. If the person already belongs to a company that has an owner, use it;
  2. If everyone else on that domain belongs to exactly one company, use it;
  3. If exactly one of the companies on that domain has an owner, use it;
  4. If the person belongs to a company, even one with no owner, use it;
  5. Otherwise, no match. If the reason is that two or more companies have owners, a manager is notified.

There are 8 ways a lead can enter the system (website form, live chat, booking, customer portal, staff entry and others). By design, recognition is not copied into each one; it lives in the layer where every lead is saved, so all of them inherit it, including any added later.

Measured numbers

Engineering counts only, no business data.

MetricValueHow we measured
Phases the feature is split into12measured 2026-12-03, read the design's phase table
Phases merged4 (design + 3 groundwork phases, including the public-mailbox list)measured 2026-12-03, phase table checked against commits and migration files on the development branch
Initial public-mailbox entries44 domainsmeasured 2026-12-03, counted the seed rows in the migration file
Database tests for the list9 assertionsmeasured 2026-12-03, read the planned assertion count in the test file (not run in this container)
Ways a lead enters the system8, sharing one recognition layermeasured 2026-12-03, checked one by one against the design
Steps in the multi-company order5measured 2026-12-03, read the design
Automatic recognition on arrivalnot live, scheduledmeasured 2026-12-03, no matching commit on the development branch yet

A checklist you can copy

  1. Use the part after the @, trimmed and lowercased, as the company key; use the typed name only for a "name differs" flag.
  2. Exclude three kinds of domains: public mailboxes, throwaway domains, your own domain.
  3. Keep public mailboxes in a list people maintain, not a "shared by more than N people" rule that drifts with the data.
  4. Make list edits take effect immediately, and record who made them.
  5. Don't fold subdomains or sibling domains yet; a missed match is cheaper than a wrong one.
  6. A match only flags the lead; staff confirm attaching the contact to the customer, and that step is logged.
  7. If recognition errors, let the lead through; never fail a customer's form submission.
  8. When a domain points at several companies, follow a fixed order and notify a manager instead of guessing.
  9. Put recognition in the layer every entry path shares, not in each path separately.
  10. Ship in phases: groundwork that changes no behavior first (the list, visibility), then the step that changes who gets a lead.

Further reading: Wikipedia's entries on email addresses and the Public Suffix List explain why "merge by domain" is harder than it looks; the PostgreSQL docs on CREATE TRIGGER cover the "one shared layer where every row is saved" approach.

FAQ

Most of our customers email us from Gmail. Is this still useful?

For public-mailbox senders, domain matching can't help, and the system honestly reports "no match" and leaves the lead for manual assignment. Steps 1 and 4 of the order, "this person already belongs to a company", still work. If most of your customers use personal addresses, the better first investment is keeping contacts properly linked to their companies. If you want a process like this moved into a system, see my services and process.

Why not merge automatically and save staff a click?

Because company membership decides what a person can see. Website form addresses are unverified, so automatic merging would hand the keys to customer records to anyone who can type. One extra click buys you a confirmed, logged decision every time. My earlier post on stopping two reps from calling the same lead makes a similar trade-off: the system warns, people decide.

How much upkeep does the list need?

The number of public mailbox providers is small, and the initial 44 cover most cases. What you'll add over time is the occasional regional provider or ISP mailbox domain, one line each. The list never changes on its own, which is exactly why we chose it: the answer you get today is the answer you get tomorrow.

About the author

Morningstar Lucifer (晨星·路西法) is an AI engineering systems architect based in Chengdu, China, with 12 years in front-end architecture and team leadership. Using Claude Code and a multi-agent development pipeline, he built AllyOS, the system that runs AllyNutra's entire business, in 118 days. He also built TeacherCopilot, a profitable AI product.

Written by the author together with AI. All numbers were measured in the AllyOS repository on the dates shown; code samples and paths are rewritten generic versions to protect client and company information.

Book a 15-minute call Services and process (in Chinese)