RFC 5322 email message parser using NimbleParsec.
Summary
Functions
Decodes RFC 2047 encoded-words in a string.
Parses a raw email message string into a structured map.
Parses a raw email message string, raising on failure.
Parses the given binary as parse_comment.
Parses the given binary as parse_headers.
Parses the given binary as parse_msg_id.
Parses the given binary as parse_msg_id_list.
Parses the given binary as parse_token.
Strips RFC 5322 comments from a header value. Comments are enclosed in parentheses and may be nested. Preserves parentheses inside quoted-strings.
Functions
Decodes RFC 2047 encoded-words in a string.
RFC 2047 defines a mechanism for encoding non-ASCII text in email headers.
Encoded-words have the format: =?charset?encoding?encoded_text?=
Supports both Base64 (B) and Quoted-Printable (Q) encodings.
Examples
iex> Mailex.Parser.decode_rfc2047("=?UTF-8?B?SGVsbG8gV29ybGQ=?=")
"Hello World"
iex> Mailex.Parser.decode_rfc2047("=?UTF-8?Q?Hello_World?=")
"Hello World"
iex> Mailex.Parser.decode_rfc2047("Plain text")
"Plain text"
iex> Mailex.Parser.decode_rfc2047(nil)
nil
Parses a raw email message string into a structured map.
Returns {:ok, message} on success, {:error, reason} on failure.
Parses a raw email message string, raising on failure.
@spec parse_comment(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_comment.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_comment (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
@spec parse_headers(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_headers.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_headers (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
@spec parse_msg_id(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_msg_id.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_msg_id (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
@spec parse_msg_id_list(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_msg_id_list.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_msg_id_list (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
@spec parse_token(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as parse_token.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the parse_token (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map
Strips RFC 5322 comments from a header value. Comments are enclosed in parentheses and may be nested. Preserves parentheses inside quoted-strings.