bamboo v0.3.2 Bamboo.Email
Contains functions for creating emails.
Bamboo separates composing emails from delivering them. This separation emails easy to test and makes things like using a default layout, or a default from address easy to do. This module is for creating emails. To actually send them, use Bamboo.Mailer.
The from, to, cc and bcc addresses accept a string, a 2 item tuple {name, address}, or anything else that you create that implements the Bamboo.Formatter protocol. The to, cc and bcc fields can also accepts a list of any combination of strings, 2 item tuples or anything that implement the Bamboo.Formatter protocol. See Bamboo.Formatter for more info.
Simplest way to create a new email
defmodule MyApp.Email do
import Bamboo.Email
def welcome_email(user) do
new_email(
from: "me@app.com",
to: user,
subject: "Welcome!",
text_body: "Welcome to the app",
html_body: "<strong>Welcome to the app</strong>"
)
end
end
Using functions to extract common parts
Let’s say you want all emails to have the same from address. Here’s how you could do that
defmodule MyApp.Email do
import Bamboo.Email
def welcome_email(user) do
struct!(base_email,
to: user,
subject: "Welcome!",
text_body: "Welcome to the app",
html_body: "<strong>Welcome to the app</strong>"
)
# or you can use functions to build it up step by step
base_email
|> to(user)
|> subject("Welcome!")
|> text_body("Welcome to the app")
|> html_body("<strong>Welcome to the app</strong>")
end
def base_email do
new_email(from: "me@app.com")
end
end
Summary
Functions
Sets the bcc on the email
Sets the cc on the email
Sets the from on the email
Sets the html_body on the email
Used to create a new email
Adds a header to the email
Adds a key/value to the private key of the email
Sets the subject on the email
Sets the text_body on the email
Sets the to on the email
Functions
Used to create a new email
If called without arguments it is the same as creating an empty
%Bamboo.Email{}
struct. If called with arguments it will populate the struct
with given attributes.
Example
# Same as %Bamboo.Email{from: "support@myapp.com"}
new_email(from: "support@myapp.com")
Adds a header to the email
Example
put_header(email, "Reply-To", "support@myapp.com")
Adds a key/value to the private key of the email
This is mostly used to implement specific functionality for a particular adapter. It will rarely be used directly from your code. Internally this is used to set Mandrill specific params for the Mandrill Adapter and it’s also used to store the view module, template and layout when using Bamboo.Phoenix.
Example
put_private(email, :tags, "welcome-email")