View Source ExEmail (ExEmail v0.1.1)

ExEmail implements a parser and validator for email addresses according to the ABNF rules defined in the following RFCs:

When parsing emails addressed to IP addresses, the domain part is parsed via the rules defined in RFC3986: Uniform Resource Identifier (URI): Generic Syntax.

Notes

  • ExEmail is a reimplementation of :email_validator in Elixir. It's a fantastic library.
  • ABNF rules are compiled using AbnfParsec. Because it's built on top of NimbleParsec, the ABNF shipped with this library differs from the relevant RFCs in specific ways. ABNF assumes that when a rule defines multiple choices, that each choice can be attempted, and then backtracked to the last matching rule; NimbleParsec does not support backtracking, so rules must be defined such that the most appropriate rule is most likely to be matched first.

Summary

Types

t()

A tuple of the local and domain parts of an email address; parsing the address alice@example.com returns {"alice", "example.com"}.

Functions

Parses an email address into a tuple of {:ok, {local_part, domain_part}}, or {:error, ExEmail.Error.t()}.

Validates the format of an email address, returning either :ok or a tuple of {:error, ExEmail.Error.t()}.

Types

domain_part()

@type domain_part() :: String.t()

local_part()

@type local_part() :: String.t()

t()

@type t() :: {local_part(), domain_part()}

A tuple of the local and domain parts of an email address; parsing the address alice@example.com returns {"alice", "example.com"}.

Functions

parse(address)

@spec parse(String.t()) :: {:ok, t()} | {:error, ExEmail.Error.t()}

Parses an email address into a tuple of {:ok, {local_part, domain_part}}, or {:error, ExEmail.Error.t()}.

Examples

iex> ExEmail.parse("a@example.com")
{:ok, {"a", "example.com"}}

iex> ExEmail.parse("@example.com")
{:error, ExEmail.Error.new("parse error", "@example.com")}

validate(address)

@spec validate(String.t()) :: :ok | {:error, ExEmail.Error.t()}

Validates the format of an email address, returning either :ok or a tuple of {:error, ExEmail.Error.t()}.

Examples

iex> ExEmail.validate("a@example.com")
:ok

iex> ExEmail.validate("@example.com")
{:error, ExEmail.Error.new("parse error", "@example.com")}