Skip to content

std/email ​

Email addresses as a browser reads them: the "valid email address" grammar of the HTML standard (what <input type=email> accepts), within SMTP's length limits. A server using it agrees with the form about which addresses are valid. No quoted local parts, comments, IP literals or non-ASCII. An accepted address holds no whitespace, angle brackets, commas, semicolons, colons, quotes or control bytes, so it is safe as one SMTP command argument or header value.

milo
from "std/email" import { isValidEmail, canonicalEmail }

Example ​

milo
from "std/email" import { isValidEmail, canonicalEmail }

fn main() {
    print(isValidEmail("ada@example.com"))   // true
    print(isValidEmail("ada@@example.com"))  // false
    print(canonicalEmail("  Ada@Example.COM\n") ?? "invalid")  // Ada@example.com
}

API reference ​

canonicalEmail ​

milo
pub fn canonicalEmail(raw: &string): Option<string>

The address as a browser would submit it from <input type=email>: newlines removed and surrounding whitespace trimmed (the HTML value sanitization), then validated. The domain is lowercased, since domains are case-insensitive; the local part keeps its case (RFC 5321 leaves it to the receiving server). None if not valid.

isValidEmail ​

milo
pub fn isValidEmail(s: &string): bool

Whether s is a valid email address per the HTML standard, within SMTP's limits (RFC 5321: a 64-byte local part, 254 bytes in all).