View Source Mail.Message (mail v0.4.0)

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.

Mail.Message.build_attachment("README.md")
%Mail.Message{data: "base64 encoded", headers: %{content_type: ["text/x-markdown"], content_disposition: ["attachment", filename: "README.md"], content_transfer_encoding: :base64}}

Mail.Message.build_attachment({"README.md", "file contents"})
%Mail.Message{data: "base64 encoded", headers: %{content_type: ["text/x-markdown"], content_disposition: ["attachment", filename: "README.md"], content_transfer_encoding: :base64}}

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

Mail.Message.build_html("<h1>Some HTML</h1>")
%Mail.Message{body: "<h1>Some HTML</h1>", headers: %{content_type: "text/html"}}

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"}]}}

Options

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

build_text(body, opts \\ [])

View Source

Build a new text message

Mail.Message.build_text("Some text")
%Mail.Message{body: "Some text", headers: %{content_type: "text/plain"}}

Mail.Message.build_text("Some text", charset: "UTF-8")
%Mail.Message{body: "Some text", headers: %{content_type: ["text/plain", {"charset", "UTF-8"}]}}

Options

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

delete_header(message, header)

View Source

Deletes a specific header key

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

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.

Mail.Message.get_boundary(%Mail.Message{headers: %{content_type: ["multipart/mixed", {"boundary", "foobar"}]}})
"foobar"

Mail.Message.get_boundary(%Mail.Message{headers: %{content_type: ["multipart/mixed"]}})
"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

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

Mail.Message.get_content_type(%Mail.Message{content_type: "text/plain"})
["text/plain"]

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

Mail.Message.match_content_type?(message, ~r/text/)
true

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

Mail.Message.put_attachment(%Mail.Message{}, "README.md")
%Mail.Message{data: "base64 encoded", headers: %{content_type: ["text/x-markdown"], content_disposition: ["attachment", filename: "README.md"], content_transfer_encoding: :base64}}

Mail.Message.put_attachment(%Mail.Message{}, {"README.md", "file contents"})
%Mail.Message{data: "base64 encoded", headers: %{content_type: ["text/x-markdown"], content_disposition: ["attachment", filename: "README.md"], content_transfer_encoding: :base64}}

Adding custom headers

Mail.Message.put_attachment(%Mail.Message{}, "README.md", headers: [content_id: "attachment-id"])
%Mail.Message{data: "base64 encoded", headers: %{content_type: ["text/x-markdown"], content_disposition: ["attachment", filename: "README.md"], content_transfer_encoding: :base64, content_id: "attachment-id"}}

Mail.Message.put_attachment(%Mail.Message{}, {"README.md", data}, headers: [content_id: "attachment-id"])
%Mail.Message{data: "base64 encoded", headers: %{content_type: ["text/x-markdown"], content_disposition: ["attachment", filename: "README.md"], content_transfer_encoding: :base64, content_id: "attachment-id"}}

Sets the body field on the part

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

Mail.Message.put_boundary(%Mail.Message{}, "foobar")
%Mail.Message{headers: %{content_type: ["", {"boundary", "foobar"}]}}

Mail.Message.put_boundary(%Mail.Message{headers: %{content_type: ["multipart/mixed", {"boundary", "bazqux"}]}})
%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

Mail.Message.put_content_type(%Mail.Message{}, "text/plain")
%Mail.Message{headers: %{content_type: ["text/plain"]}}
Link to this function

put_header(message, key, content)

View Source

Add a new header key/value pair

Mail.Message.put_header(%Mail.Message{}, :content_type, "text/plain")

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

Add new part

Mail.Message.put_part(%Mail.Message{}, %Mail.Message{})