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.
from "std/email" import { isValidEmail, canonicalEmail }Example
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
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
pub fn isValidEmail(s: &string): boolWhether 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).