View Source Mail.Message (mail v0.4.3)

Summary

Functions

Build a new HTML message

Build a new text message

Deletes a specific header key

Deletes a list of headers

Delete a matching part

Gets the boundary value from the content_type header

Gets the content_type from the header

Determines the message has any attachment parts

Determines the message has any text (text/plain or text/html) parts

Is the part an attachment or not

Is the message text based or not

Will match on a full or partial content type

Adds a new attachment part to the provided message

Sets the body field on the part

Adds a boundary value to the content_type header

Add a new content-type header

Add a new header key/value pair

Types

@type t() :: %Mail.Message{
  body: term(),
  headers: term(),
  multipart: term(),
  parts: term()
}

Functions

Link to this function

build_attachment(path_or_file_tuple, opts \\ [])

View Source

Add attachment meta data to a Mail.Message

Will allow you to create a new part that is meant to be used as an attachment.

You can pass either a filepath or a tuple as the second argument. If a tuple is being passed the tuple must only have two elements: {filename, filedata}.

The mimetype of the file is determined by the file extension.

Examples

iex> message = Mail.Message.build_attachment("README.md")
%Mail.Message{body: <<"# Mail\n", _::binary>>, headers: %{"content-type" => ["text/markdown"], "content-disposition" => ["attachment", {"filename", "README.md"}], "content-transfer-encoding" => :base64}} = message

iex> message = Mail.Message.build_attachment({"README.md", "file contents"})
%Mail.Message{body: "file contents", headers: %{"content-type" => ["text/markdown"], "content-disposition" => ["attachment", {"filename", "README.md"}], "content-transfer-encoding" => :base64}} = message

Options

See put_attachment/3 for options

Custom mimetype library

By default Mail will use its own internal mimetype adapter. However, you may want to rely on Plug and the custom mimetypes that you have created for it. You can override the mimetype function in the config.exs of your application:

config :mail, mimetype_fn: &CustomMimeAdapter.type/1

This function should take a string that is the file extension. It should return a single mimetype.

CustomMimeAdapter.type("md")
"text/markdown"
Link to this function

build_html(body, opts \\ [])

View Source

Build a new HTML message

Examples

iex> Mail.Message.build_html("<h1>Some HTML</h1>")
%Mail.Message{body: "<h1>Some HTML</h1>", headers: %{"content-type" => ["text/html", {"charset", "UTF-8"}], "content-transfer-encoding" => :quoted_printable}}

iex> Mail.Message.build_html("<h1>Some HTML</h1>", charset: "UTF-8")
%Mail.Message{body: "<h1>Some HTML</h1>", headers: %{"content-type" => ["text/html", {"charset", "UTF-8"}], "content-transfer-encoding" => :quoted_printable}}

Options

  • :charset - The character encoding standard for content type
Link to this function

build_text(body, opts \\ [])

View Source

Build a new text message

Examples

iex> Mail.Message.build_text("Some text")
%Mail.Message{body: "Some text", headers: %{"content-type" => ["text/plain", {"charset", "UTF-8"}], "content-transfer-encoding" => :quoted_printable}}

iex> Mail.Message.build_text("Some text", charset: "us-ascii")
%Mail.Message{body: "Some text", headers: %{"content-type" => ["text/plain", {"charset", "us-ascii"}], "content-transfer-encoding" => :quoted_printable}}

Options

  • :charset - The character encoding standard for content type
Link to this function

delete_header(message, header)

View Source

Deletes a specific header key

Examples

iex> Mail.Message.delete_header(%Mail.Message{headers: %{"foo" => "bar"}}, :foo)
%Mail.Message{headers: %{}}
Link to this function

delete_headers(message, headers)

View Source

Deletes a list of headers

Examples

iex> Mail.Message.delete_headers(%Mail.Message{headers: %{"foo" => "bar", "baz" => "qux"}}, [:foo, :baz])
%Mail.Message{headers: %{}}
Link to this function

delete_part(message, part)

View Source

Delete a matching part

Will delete a matching part in the parts list. If the part is not found no error is raised.

Gets the boundary value from the content_type header

Will retrieve the boundary value. If one is not set a random one is generated.

Examples

iex> Mail.Message.get_boundary(%Mail.Message{headers: %{"content-type" => ["multipart/mixed", {"boundary", "foobar"}]}})
"foobar"

iex> Mail.Message.get_boundary(%Mail.Message{headers: %{"content-type" => ["multipart/mixed", {"boundary", "ASDFSHNEW3473423"}]}})
"ASDFSHNEW3473423"
Link to this function

get_content_type(message)

View Source

Gets the content_type from the header

Will ensure the content_type is always wrapped in a List

Examples

iex> Mail.Message.get_content_type(%Mail.Message{})
[""]

iex> Mail.Message.get_content_type(%Mail.Message{headers: %{"content-type" => "text/plain"}})
["text/plain"]

iex> Mail.Message.get_content_type(%Mail.Message{headers: %{"content-type" => ["multipart/mixed", {"boundary", "foobar"}]}})
["multipart/mixed", {"boundary", "foobar"}]
Link to this function

get_header(message, key)

View Source

Determines the message has any attachment parts

Returns a Boolean

Link to this function

has_header?(message, header)

View Source

Determines the message has any text (text/plain or text/html) parts

Returns a Boolean

Is the part an attachment or not

Returns Boolean

Is the message text based or not

Can be a message with a content_type of text/plain or text/html

Returns Boolean

Link to this function

match_body_text(message)

View Source
Link to this function

match_content_type?(message, string_or_regex)

View Source

Will match on a full or partial content type

Examples

iex> message = %Mail.Message{headers: %{"content-type" => ["text/plain", {"charset", "UTF-8"}]}}
iex> Mail.Message.match_content_type?(message, ~r/text/)
true

iex> message = %Mail.Message{headers: %{"content-type" => ["text/plain", {"charset", "UTF-8"}]}}
iex> Mail.Message.match_content_type?(message, "text/html")
false
Link to this function

put_attachment(message, path_or_file_tuple, opts \\ [])

View Source

Adds a new attachment part to the provided message

The first argument must be a Mail.Message. The remaining argument is described in build_attachment/1

Options

  • :headers - Headers to be merged

Examples

iex> message = Mail.Message.put_attachment(%Mail.Message{}, "README.md")
%Mail.Message{body: <<"# Mail\n", _::binary>>, headers: %{"content-type" => ["text/markdown"], "content-disposition" => ["attachment", {"filename", "README.md"}], "content-transfer-encoding" => :base64}} = message

iex> Mail.Message.put_attachment(%Mail.Message{}, {"README.md", "file contents"})
%Mail.Message{body: "file contents", headers: %{"content-type" => ["text/markdown"], "content-disposition" => ["attachment", {"filename", "README.md"}], "content-transfer-encoding" => :base64}}

Adding custom headers

iex> message =Mail.Message.put_attachment(%Mail.Message{}, "README.md", headers: [content_id: "attachment-id"])
%Mail.Message{body: <<"# Mail\n", _::binary>>, headers: %{"content-type" => ["text/markdown"], "content-disposition" => ["attachment", {"filename", "README.md"}], "content-transfer-encoding" => :base64, "content-id" => "attachment-id"}} = message

iex> message = Mail.Message.put_attachment(%Mail.Message{}, {"README.md", "file contents"}, headers: [content_id: "attachment-id"])
%Mail.Message{body: "file contents", headers: %{"content-type" => ["text/markdown"], "content-disposition" => ["attachment", {"filename", "README.md"}], "content-transfer-encoding" => :base64, "content-id" => "attachment-id"}} = message

Sets the body field on the part

Examples

iex> Mail.Message.put_body(%Mail.Message{}, "Some Data")
%Mail.Message{body: "Some Data", headers: %{}}
Link to this function

put_boundary(message, boundary)

View Source

Adds a boundary value to the content_type header

Will overwrite existing boundary key in the list. Will preserve other values in the list

Examples

iex> Mail.Message.put_boundary(%Mail.Message{}, "foobar")
%Mail.Message{headers: %{"content-type" => ["", {"boundary", "foobar"}]}}

iex> Mail.Message.put_boundary(%Mail.Message{headers: %{"content-type" => ["multipart/mixed", {"boundary", "bazqux"}]}}, "foobar")
%Mail.Message{headers: %{"content-type" => ["multipart/mixed", {"boundary", "foobar"}]}}
Link to this function

put_content_type(message, content_type)

View Source

Add a new content-type header

The value will always be wrapped in a List

Examples

iex> Mail.Message.put_content_type(%Mail.Message{}, "text/plain")
%Mail.Message{headers: %{"content-type" => ["text/plain"]}}

iex> Mail.Message.put_content_type(%Mail.Message{}, ["text/plain", {"charset", "UTF-8"}])
%Mail.Message{headers: %{"content-type" => ["text/plain", {"charset", "UTF-8"}]}}
Link to this function

put_header(message, key, content)

View Source

Add a new header key/value pair

Examples

iex> Mail.Message.put_header(%Mail.Message{}, :content_type, "text/plain")
%Mail.Message{headers: %{"content-type" => "text/plain"}}

The individual headers will be in the headers field on the %Mail.Message{} struct

Add new part

Examples

iex> Mail.Message.put_part(%Mail.Message{}, %Mail.Message{})
%Mail.Message{parts: [%Mail.Message{}]}