MailAddress v1.0.1 MailAddress.Parser View Source

Functions to parse mail addresses.

Link to this section Summary

Functions

Parses an email address, with options configured by the options parameter

Checks if the given email address string has valid syntax by attempting to parse it, using the provided (or default) options

Link to this section Functions

Link to this function parse(arg, options \\ %MailAddress.Options{}) View Source

Parses an email address, with options configured by the options parameter.

Parsing begins at the first character of the string and continues until either the closing bracket (if configured to use require_brackets in the options, or an opening bracket was the first character of the string), a character is encountered which would not be valid in a domain, or end of string is reached.

Returns either {:ok, parsed_address, remainder_of_string} or {:error, error_reason_string}.

Examples

iex> {:ok, addr, ""} = MailAddress.Parser.parse("test@example.org")
iex> addr
#MailAddress<test@example.org>

iex> MailAddress.Parser.parse("test@example.invalid_domain!")
{:error, "unexpected character (_)"}

iex> {:ok, addr, ""} = MailAddress.Parser.parse("<>", %MailAddress.Options{require_brackets: true, allow_null: true})
iex> addr
#MailAddress<>

iex> MailAddress.Parser.parse("<>", %MailAddress.Options{require_brackets: true, allow_null: false})
{:error, "address can't be null"}

iex> MailAddress.Parser.parse("abc@def", %MailAddress.Options{require_brackets: true})
{:error, "opening bracket ('<') expected"}

iex> {:ok, addr, ""} = MailAddress.Parser.parse("test@EXAMPLE.ORG", %MailAddress.Options{downcase_domain: true})
iex> addr
#MailAddress<test@example.org>

iex> {:ok, addr, " some more text"} = MailAddress.Parser.parse("<test@example.org> some more text")
iex> addr
#MailAddress<test@example.org>
Link to this function valid?(address, options \\ %MailAddress.Options{}) View Source

Checks if the given email address string has valid syntax by attempting to parse it, using the provided (or default) options.

Returns true if valid, false if invalid.

Examples

iex> MailAddress.Parser.valid?("test@example.org")
true

iex> MailAddress.Parser.valid?("@invalid@example.org")
false

iex> MailAddress.Parser.valid?("\"@invalid\"@example.org")
true