Rust

Emails

Email address parsing, canonical forms per provider, free, disposable and corporate classification, and validators.
Rust 1,744 lines (1,130 code)split
PartCodeCommentsBlankTotalFiles
Rust source1,1304341801,7447
Rust tests1212111344

Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.

Install#

The crate lives in the Appwrite repository; depend on it from Git.

Cargo.toml
[dependencies]
utopia-emails = { git = "https://github.com/appwrite/appwrite" }

Getting started#

Common tasks with Emails, each a complete program you can copy and run.

Utopia Emails parses email addresses and answers the questions an application asks about them: is the address well formed, what is its canonical form at its mail provider, and is its domain free, disposable or corporate. It also ships validators for request parameters. This guide covers the common tasks.

Validate an address from a sign-up form

EmailValidator accepts a string that parses as a valid address. Surrounding spaces and upper case are fine, because the address is trimmed and lower-cased first.

Rust
use utopia_emails::EmailValidator;

let validator = EmailValidator::default();

assert!(validator.is_valid_address("  Jane.Doe@Example.com "));
assert!(!validator.is_valid_address("jane.doe"));
assert!(!validator.is_valid_address("jane@doe@example.com"));
assert!(!validator.is_valid_address(""));

// An optional field: allow it to stay empty.
assert!(EmailValidator::new(true).is_valid_address(""));

Read the parts of an address

Parse the address once, then read its local part, its domain, and the provider and subdomain inside the domain. formatted returns any one of them by name.

Rust
use utopia_emails::{Email, Format};

let email = Email::new("  Jane@Mail.Example.co.uk ")?;

assert_eq!(email.get(), "jane@mail.example.co.uk");
assert_eq!(email.local(), "jane");
assert_eq!(email.domain(), "mail.example.co.uk");
assert_eq!(email.provider(), "example.co.uk");
assert_eq!(email.subdomain(), "mail");
assert!(email.has_subdomain());
assert_eq!(email.formatted(Format::Provider), "example.co.uk");
assert_eq!(email.formatted(Format::from_name("local")), "jane");

Check the local part and the domain

FILTER_VALIDATE_EMAIL allows more than most applications want. has_valid_local allows only letters, digits and ._+- with no stray dots, and has_valid_domain requires a known public suffix. EmailLocal and EmailDomain apply the same rules as validators.

Rust
use utopia_emails::{Email, EmailDomain, EmailLocal};

let email = Email::new("o'brien@example.com")?;
assert!(email.is_valid());
assert!(!email.has_valid_local());
assert!(email.has_valid_domain());

assert!(EmailLocal.is_valid_address("jane.doe+news@example.com"));
assert!(!EmailLocal.is_valid_address("o'brien@example.com"));
assert!(EmailDomain.is_valid_address("jane@mail.example.com"));
assert!(!EmailDomain.is_valid_address("jane@example.notatld"));

Normalize an address before storing it

Many providers deliver several spellings to the same inbox. Gmail ignores dots and +tags, and Outlook ignores +tags. Store canonical to stop one person from signing up many times. Unknown domains keep their address as is.

Rust
use utopia_emails::Email;

let canonical = |address: &str| Email::new(address)?.canonical();

assert_eq!(canonical("Jane.Doe+promo@googlemail.com")?, "janedoe@gmail.com");
assert_eq!(canonical("jane.doe+promo@hotmail.co.uk")?, "jane.doe@outlook.com");
assert_eq!(canonical("jane.doe+promo@me.com")?, "jane.doe@icloud.com");
assert_eq!(canonical("jane.doe+promo@example.com")?, "jane.doe+promo@example.com");

Find the mail provider behind a domain

is_canonical_supported says whether a provider with aliasing rules owns the domain, and canonical_domain names that provider's main domain. Use it to group users by provider.

Rust
use utopia_emails::Email;

let email = Email::new("jane@live.com")?;
assert!(email.is_canonical_supported());
assert_eq!(email.canonical_domain(), Some("outlook.com"));

let email = Email::new("jane@example.com")?;
assert!(!email.is_canonical_supported());
assert_eq!(email.canonical_domain(), None);

Reject disposable domains

Each address is free (a public mail service), disposable (a throwaway inbox) or corporate (anything else). Use EmailNotDisposable to block throwaway inboxes, and EmailCorporate when only work addresses are welcome.

Rust
use utopia_emails::{Email, EmailCorporate, EmailNotDisposable};

let email = Email::new("jane@mailinator.com")?;
assert!(email.is_disposable());
assert!(!email.is_free());

let email = Email::new("jane@gmail.com")?;
assert!(email.is_free());
assert!(!email.is_corporate());

assert!(!EmailNotDisposable.is_valid_address("jane@10minutemail.com"));
assert!(EmailNotDisposable.is_valid_address("jane@gmail.com"));
assert!(!EmailCorporate.is_valid_address("jane@gmail.com"));
assert!(EmailCorporate.is_valid_address("jane@company.org"));

Report addresses that cannot be parsed

Parsing fails on an empty value or one without exactly one @. Normalizing fails when a provider's rules leave nothing of the local part. Each error carries a message you can show.

Rust
use utopia_emails::{Email, Error};

for value in ["", "jane.doe", "jane@doe@example.com"] {
    match Email::new(value) {
        Err(Error::Parse(_)) => {}
        other => panic!("expected a parse error, got {other:?}"),
    }
}
assert_eq!(Email::new("jane.doe").unwrap_err().to_string(), "'jane.doe' must be a valid email address");

let error = Email::new("...@gmail.com")?.canonical().unwrap_err();
assert!(matches!(error, Error::InvalidArgument(_)));
assert_eq!(error.to_string(), "Email local part cannot be empty after normalization");

Add rules for your own mail provider

Implement Provider for a domain the built-in providers do not know, then pass it to canonical_with ahead of the defaults. The first provider that supports the domain wins.

Rust
use utopia_emails::{Canonical, Email, Error, Provider, PROVIDERS};

struct Acme;

impl Provider for Acme {
    fn canonical(&self, local: &str, _domain: &str) -> Result<Canonical, Error> {
        let local = local.split('-').next().unwrap_or(local);
        Ok(Canonical { local: local.to_owned(), domain: "acme.com".to_owned() })
    }

    fn canonical_domain(&self) -> &str {
        "acme.com"
    }

    fn supported_domains(&self) -> &[&str] {
        &["acme.com", "acme.net"]
    }
}

let providers: Vec<&dyn Provider> = std::iter::once(&Acme as &dyn Provider).chain(PROVIDERS.iter().copied()).collect();

assert_eq!(Email::new("jane-news@acme.net")?.canonical_with(&providers)?, "jane@acme.com");
assert_eq!(Email::new("jane+news@gmail.com")?.canonical_with(&providers)?, "jane@gmail.com");

Tasks

The free and disposable domain lists are embedded in the crate (data/free-domains.txt, data/disposable-domains.txt), so a binary needs no data files at runtime. The emails-import task refreshes them from the public lists they are built from, merged with the manual lists next to them (data/*-domains-manual.txt); rebuild afterwards to embed them.

Shell
# Preview what would change in the disposable domains, then write it.
cargo run -p utopia-emails --bin emails-import -- disposable
cargo run -p utopia-emails --bin emails-import -- disposable --commit=true

# Both lists from every source, even when nothing changed.
cargo run -p utopia-emails --bin emails-import -- all --commit=true --force=true

# One source only, and statistics about the current lists.
cargo run -p utopia-emails --bin emails-import -- free --source=kikobeats
cargo run -p utopia-emails --bin emails-import -- stats

A source that cannot be downloaded is reported and skipped; the task fails (exit code 1) only when no source gives a domain. To add a domain by hand, add a line to the manual list and run the task: an edit to the generated lists is lost on the next import. UTOPIA_EMAILS_DATA_DIR reads and writes the lists in another directory, and UTOPIA_EMAILS_<TYPE>_<SOURCE>_URL (such as UTOPIA_EMAILS_DISPOSABLE_MARTENSON_URL) downloads a source from another address.

Reference#

Email addresses: parsing, validation, the canonical form of an address per mail provider, and the free / disposable / corporate classification of its domain.

Domain parsing (provider, subdomain, known suffixes) is utopia-domains.

The free and disposable lists are embedded in the crate (data/free-domains.txt, data/disposable-domains.txt) and parsed on first use, unless a server installs other lists with Lists::install at startup. The emails-import task refreshes them from their sources (see the guide).

Modules#

Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.

API#

What utopia_emails exports at its root.

Structs

CanonicalThe canonical form of an address: its local part and domain with a provider's aliases removed.DomainListA set of domains.EmailA parsed email address: trimmed, lower-cased and split at its single `@`.EmailCorporateValidates an email address on a corporate domain (`Email::is_corporate`).EmailDomainValidates an email address whose domain is valid (`Email::has_valid_domain`).EmailLocalValidates an email address whose local part is valid (`Email::has_valid_local`).EmailNotDisposableValidates an email address that is not on a disposable domain (`Email::is_disposable`).EmailValidatorValidates an email address; with `allow_empty`, also accepts `""`.FastmailFastmail: keeps the local part and uses `fastmail.com`.GenericThe fallback for any domain: only lower-cases the local part. Its canonical domain is empty and it lists no domains.GmailGmail: drops `+tags` and all dots; `googlemail.com` is `gmail.com`.IcloudiCloud, me.com and mac.com: drops `+tags`, keeps dots, and uses `icloud.com`.ListsThe free and disposable domain lists `crate::Email` classifies domains with.OutlookOutlook, Hotmail, Live, MSN and Passport: drops `+tags`, keeps dots, and uses `outlook.com`.ProtonmailProton Mail: drops `+tags`, keeps dots and the domain (each Proton domain is a separate address).WallaWalla: keeps the local part; `walla.com` is `walla.co.il`.YahooYahoo, Ymail and Rocketmail: keeps the local part (a `-keyword` is a separate disposable address, not an alias) and uses `yahoo.com`.YandexYandex: keeps the local part and uses `yandex.ru`. Not in `PROVIDERS`.

Commands#

Programs the crate ships besides its API. Run them with cargo from the repository root.

emails-import

import

Refreshes the free and disposable email domain lists the crate embeds, data/free-domains.txt and data/disposable-domains.txt, from their sources, and reports statistics about them.

Shell
cargo run -p utopia-emails --bin emails-import -- disposable --commit=true
cargo run -p utopia-emails --bin emails-import -- stats

An update fetches every source of the list (or the one --source names), keeps the domains that parse with a known name and suffix and are not test domains, merges them with the manual list (data/*-domains-manual.txt), removes duplicates and sorts them. It only previews the changes unless --commit=true is given, and stops early when nothing changed unless --force=true is. A source that fails is reported and skipped. Rebuild the crate after a commit: the lists are embedded at compile time.

Commands

disposable

update the disposable domains from the manual list, martenson (disposable-email-domains), disposable, wesbos, fakefilter and adamloving.

free

update the free domains from the manual list and kikobeats.

all

disposable, then free, both from every source.

stats

count the domains of both lists, by section and by TLD.

Arguments

--commit=<bool>

write the updated list (default false: preview the changes). Not for stats.

--force=<bool>

update even when the list did not change (default false). Not for stats.

--source=<key>

only this source, such as martenson or kikobeats (default: every source). disposable and free only.

Environment

UTOPIA_EMAILS_DATA_DIR

the directory the lists are read from and written to (default: the crate's data/).

UTOPIA_EMAILS_<TYPE>_<SOURCE>_URL

where to download one source from, such as UTOPIA_EMAILS_DISPOSABLE_MARTENSON_URL or UTOPIA_EMAILS_FREE_KIKOBEATS_URL.

Exit codes

0

done, including a preview, a list already up to date (which ends all before the free domains), an unknown command and invalid arguments (which print nothing).

1

no source given or found, every source failed, or the list could not be written; the reason is on standard error.

255

no command.

Writes

  • crates/utopia/emails/data/disposable-domains.txt
  • crates/utopia/emails/data/free-domains.txt

The crate embeds these files when it is compiled: rebuild after running the task.