View Source Mail (mail v0.4.0)

Mail primitive for composing messages.

Build a mail message with the Mail struct

mail =
  Mail.build_multipart()
  |> put_subject("How is it going?")
  |> Mail.put_text("Just checking in")
  |> Mail.put_to("joe@example.com")
  |> Mail.put_from("brian@example.com")

Summary

Functions

Returns a unique list of all recipients

Build a single-part mail

Build a multi-part mail

Walks the message parts and collects all attachments

Retrieves the recipients from the bcc header

Retrieves the recipients from the cc header

Retrieves the from header

Find the last html part of a given mail

Retrieves the reply-to header

Retrieve the subject header

Find the last text part of a given mail

Retrieves the list of recipients from the to header

Determines the message has any attachment parts

Determines the message has any text parts

Add an attachment part to the message

Add new recipients to the bcc header

Add new recipients to the cc header

Add a new from header

Add an HTML part to the message

Add a new reply-to header

Add a new subject header

Add a plaintext part to the message

Add new recipients to the to header

Functions

Returns a unique list of all recipients

Will collect all recipients from to, cc, and bcc and returns a unique list of recipients.

Build a single-part mail

Build a multi-part mail

Link to this function

get_attachments(message)

View Source

Walks the message parts and collects all attachments

Each member in the list is {filename, content}

Retrieves the recipients from the bcc header

Retrieves the recipients from the cc header

Retrieves the from header

Find the last html part of a given mail

RFC 2046, §5.1.4 “In general, the best choice is the LAST part of a type supported by the recipient system's local environment.”

If single part with content-type "text/html", returns itself If single part without content-type "text/html", returns nil If multipart with part having content-type "text/html" will return that part If multipart without part having content-type "text/html" will return nil

Retrieves the reply-to header

Retrieve the subject header

Find the last text part of a given mail

RFC 2046, §5.1.4 “In general, the best choice is the LAST part of a type supported by the recipient system's local environment.”

If single part with content-type "text/plain", returns itself If single part without content-type "text/plain", returns nil If multipart with part having content-type "text/plain" will return that part If multipart without part having content-type "text/plain" will return nil

Retrieves the list of recipients from the to header

Link to this function

has_attachments?(message)

View Source

Determines the message has any attachment parts

Returns a Boolean

Link to this function

has_text_parts?(message)

View Source

Determines the message has any text parts

Returns a Boolean

Link to this function

parse(message, parser \\ Mail.Parsers.RFC2822)

View Source

Primary hook for parsing

You can pass in your own custom parse module. That module must have a parse/1 function that accepts a string or list of lines

By default the parser will be Mail.Parsers.RFC2822

Link to this function

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

View Source

Add an attachment part to the message

Mail.put_attachment(%Mail.Message{}, "README.md")
Mail.put_attachment(%Mail.Message{}, {"README.md", data})

Each call will add a new attachment part.

Link to this function

put_bcc(message, recipients)

View Source

Add new recipients to the bcc header

Recipients can be added as a single string or a list of strings. The list of recipients will be concated to the previous value.

Mail.put_bcc(%Mail.Message{}, "one@example.com")
%Mail.Message{headers: %{bcc: ["one@example.com"]}}

Mail.put_bcc(%Mail.Message{}, ["one@example.com", "two@example.com"])
%Mail.Message{headers: %{bcc: ["one@example.com", "two@example.com"]}}

Mail.put_bcc(%Mail.Message{}, "one@example.com")
|> Mail.put_bcc(["two@example.com", "three@example.com"])
%Mail.Message{headers: %{bcc: ["one@example.com", "two@example.com", "three@example.com"]}}

The value of a recipient must conform to either a string value or a tuple with two elements, otherwise an ArgumentError is raised.

Valid forms:

  • "user@example.com"
  • "Test User <user@example.com>"
  • {"Test User", "user@example.com"}
Link to this function

put_cc(message, recipients)

View Source

Add new recipients to the cc header

Recipients can be added as a single string or a list of strings. The list of recipients will be concated to the previous value.

Mail.put_cc(%Mail.Message{}, "one@example.com")
%Mail.Message{headers: %{cc: ["one@example.com"]}}

Mail.put_cc(%Mail.Message{}, ["one@example.com", "two@example.com"])
%Mail.Message{headers: %{cc: ["one@example.com", "two@example.com"]}}

Mail.put_cc(%Mail.Message{}, "one@example.com")
|> Mail.put_cc(["two@example.com", "three@example.com"])
%Mail.Message{headers: %{cc: ["one@example.com", "two@example.com", "three@example.com"]}}

The value of a recipient must conform to either a string value or a tuple with two elements, otherwise an ArgumentError is raised.

Valid forms:

  • "user@example.com"
  • "Test User <user@example.com>"
  • {"Test User", "user@example.com"}
Link to this function

put_from(message, sender)

View Source

Add a new from header

Mail.put_from(%Mail.Message{}, "user@example.com")
%Mail.Message{headers: %{from: "user@example.com"}}
Link to this function

put_html(message, body, opts \\ [])

View Source

Add an HTML part to the message

Mail.put_html(%Mail.Message{}, "<span>Some HTML</span>")

If a text part already exists this function will replace that existing part with the new part.

Options

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

put_reply_to(message, reply_address)

View Source

Add a new reply-to header

Mail.put_reply_to(%Mail.Message{}, "user@example.com")
%Mail.Message{headers: %{reply_to: "user@example.com"}}
Link to this function

put_subject(message, subject)

View Source

Add a new subject header

Mail.put_subject(%Mail.Message{}, "Welcome to DockYard!")
%Mail.Message{headers: %{subject: "Welcome to DockYard!"}}
Link to this function

put_text(message, body, opts \\ [])

View Source

Add a plaintext part to the message

Shortcut function for adding plain text part

Mail.put_text(%Mail.Message{}, "Some plain text")

If a text part already exists this function will replace that existing part with the new part.

Options

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

put_to(message, recipients)

View Source

Add new recipients to the to header

Recipients can be added as a single string or a list of strings. The list of recipients will be concated to the previous value.

Mail.put_to(%Mail.Message{}, "one@example.com")
%Mail.Message{headers: %{to: ["one@example.com"]}}

Mail.put_to(%Mail.Message{}, ["one@example.com", "two@example.com"])
%Mail.Message{headers: %{to: ["one@example.com", "two@example.com"]}}

Mail.put_to(%Mail.Message{}, "one@example.com")
|> Mail.put_to(["two@example.com", "three@example.com"])
%Mail.Message{headers: %{to: ["one@example.com", "two@example.com", "three@example.com"]}}

The value of a recipient must conform to either a string value or a tuple with two elements, otherwise an ArgumentError is raised.

Valid forms:

  • "user@example.com"
  • "Test User <user@example.com>"
  • {"Test User", "user@example.com"}
Link to this function

render(message, renderer \\ Mail.Renderers.RFC2822)

View Source

Primary hook for rendering

You can pass in your own custom render module. That module must have render/1 function that accepts a Mail.Message struct.

By default the renderer will be Mail.Renderers.RFC2822