mail v0.0.3 Mail.Message

Summary

Functions

Add attachment meta data to a Mail.Message

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

Functions

build_attachment(path_or_file_tuple)

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

  • :encoding - Valid values: :base64
  • :content_type - override the mimetype, will autodetermine based upon file extension otherwise

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"
build_html(body)

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"}}
build_text(body)

Build a new text message

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

Deletes a specific header key

Mail.Message.delete_header(%Mail.Message{headers: %{foo: "bar"}}, :foo)
%Mail.Message{headers: %{}}
delete_headers(message, headers)

Deletes a list of headers

Mail.Message.delete_headers(%Mail.Message{headers: %{foo: "bar", baz: "qux"}}, [:foo, :baz])
%Mail.Message{headers: %{}}
delete_part(message, part)

Delete a matching part

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

get_boundary(message)

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"
get_content_type(message)

Gets the content_type from the header

Will ensure the content_type is always wraped 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"]
get_header(message, key)
has_attachment?(parts)

Determines the message has any attachment parts

Returns a Boolean

has_text_part?(parts)

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

Returns a Boolean

is_attachment?(message)

Is the part an attachment or not

Returns Boolean

is_text_part?(message)

Is the message text based or not

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

Returns Boolean

match_content_type?(message, string_or_regex)

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
put_attachment(message, path_or_file_tuple)

Adds a new attachment part to the provided message

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

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}}
put_body(part, body)

Sets the body field on the part

Mail.Message.put_body(%Mail.Message{}, "Some data")
%Mail.Message{body: "Some Data", headers: %{}}
put_boundary(message, boundary)

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"]}}
put_content_type(message, content_type)

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"]}}
put_header(message, key, content)

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

put_part(message, part)

Add new part

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