bamboo v0.6.0 Bamboo.Formatter protocol

Converts data to email addresses.

The passed in options is currently a map with the key :type and a value of :from, :to, :cc or :bcc. This makes it so that you can pattern match and return a different address depending on if the address is being used in the from, to, cc or bcc.

Simple example

Let’s say you have a user struct like this.

defmodule MyApp.User do
  defstruct first_name: nil, last_name: nil, email: nil
end

Bamboo can automatically format this struct if you implement the Bamboo.Formatter protocol.

defimpl Bamboo.Formatter, for: MyApp.User do
  # Used by `to`, `bcc`, `cc` and `from`
  def format_email_address(user, _opts) do
    fullname = "#{user.first_name} #{user.last_name}"
    {fullname, user.email}
  end
end

Now you can create emails like this, and the user will be formatted correctly

user = %User{first_name: "John", last_name: "Doe", email: "me@example.com"}
Bamboo.Email.new_email(from: user)

Customize formatting based on from, to, cc or bcc

This can be helpful if you want to add the name of the app when sending on behalf of a user.

defimpl Bamboo.Formatter, for: MyApp.User do
  # Include the app name when used in a from address
  def format_email_address(user, %{type: :from}) do
    fullname = "#{user.first_name} #{user.last_name}"
    {fullname <> " (Sent from MyApp)", user.email}
  end

  # Just use the name for all other types
  def format_email_address(user, _opts) do
    fullname = "#{user.first_name} #{user.last_name}"
    {fullname, user.email}
  end
end

Summary

Functions

Receives data and opts and should return a string or a 2 item tuple {name, address}

Types

opts :: %{type: :from | :to | :cc | :bcc}
t :: term

Functions

format_email_address(data, opts)

Specs

format_email_address(any, opts) :: Bamboo.Email.address

Receives data and opts and should return a string or a 2 item tuple {name, address}

opts is a map with the key :type and a value of :from, :to, :cc or :bcc. You can pattern match on this to customize the address.