Mail.Message (mail v0.2.3) View Source
Link to this section 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
Add new part
Link to this section Types
Specs
t() :: Mail.Message
Link to this section Functions
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"
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
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
Deletes a specific header key
Mail.Message.delete_header(%Mail.Message{headers: %{foo: "bar"}}, :foo)
%Mail.Message{headers: %{}}
Deletes a list of headers
Mail.Message.delete_headers(%Mail.Message{headers: %{foo: "bar", baz: "qux"}}, [:foo, :baz])
%Mail.Message{headers: %{}}
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"
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"}]
Determines the message has any attachment parts
Returns a Boolean
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
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
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: %{}}
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"}]}}
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"]}}
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{})