mail v0.0.4 Mail

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

Find the html part of a given mail

Find the text part of a given mail

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

all_recipients(message)

Returns a unique list of all recipients

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

build()

Build a single-part mail

build_multipart()

Build a multi-part mail

get_html(message)

Find the html part of a given mail

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

get_text(message)

Find the text part of a given mail

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

put_attachment(message, path)

Add an attachment part to the message

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

Each call will add a new attachment part.

put_bcc(message, recipients)

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"}
put_cc(message, recipients)

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"}
put_from(message, sender)

Add a new from header

Mail.put_from(%Mail.Message{}, "user@example.com")
%Mail.Message{headers: %{from: "user@example.com"}}
put_html(message, body)

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.

put_reply_to(message, reply_address)

Add a new reply-to header

Mail.put_reply_to(%Mail.Message{}, "user@example.com")
%Mail.Message{headers: %{reply_to: "user@example.com"}}
put_subject(message, subject)

Add a new subject header

Mail.put_subject(%Mail.Message{}, "Welcome to DockYard!")
%Mail.Message{headers: %{subject: "Welcome to DockYard!"}}
put_text(message, body)

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.

put_to(message, recipients)

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"}
render(message, renderer \\ Mail.Renderers.RFC2822)

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