Swoosh v0.4.0 Swoosh.Mailer

Defines a mailer.

A mailer is a wrapper around an adapter that makes it easy for you to swap the adapter without having to change your code.

It is also responsible for doing some sanity checks before handing down the email to the adapter.

When used, the mailer expects :otp_app as an option. The :otp_app should point to an OTP application that has the mailer configuration. For example, the mailer:

defmodule Sample.Mailer do
  use Swoosh.Mailer, otp_app: :sample
end

Could be configured with:

config :sample, Sample.Mailer,
  adapter: Swoosh.Adapters.Sendgrid,
  api_key: "SG.x.x"

Most of the configuration that goes into the config is specific to the adapter, so check the adapter’s documentation for more information.

Note that the configuration is set into your mailer at compile time. If you need to reference config at runtime you can use a tuple like {:system, "ENV_VAR"}.

config :sample, Sample.Mailer,
  adapter: Swoosh.Adapters.SMTP,
  relay: "smtp.sendgrid.net"
  username: {:system, "SMTP_USERNAME"},
  password: {:system, "SMTP_PASSWORD"},
  tls: :always

Examples

Once configured you can use your mailer like this:

# in an IEx console
iex> email = new |> from("tony@stark.com") |> to("steve@rogers.com")
%Swoosh.Email{from: {"", "tony@stark.com"}, ...}
iex> Mailer.deliver(email)
:ok

You can also pass an extra config argument to deliver/2 that will be merged with your Mailer’s config:

# in an IEx console
iex> email = new |> from("tony@stark.com") |> to("steve@rogers.com")
%Swoosh.Email{from: {"", "tony@stark.com"}, ...}
iex> Mailer.deliver(email, domain: "jarvis.com")
:ok

Summary

Functions

Parses the OTP configuration at compile time

Parses the OTP configuration at run time

Functions

deliver(adapter, email, config)
parse_config(mailer, opts)

Parses the OTP configuration at compile time.

parse_runtime_config(config)

Parses the OTP configuration at run time.

This function will transform all the {:system, “ENV_VAR”} tuples into their respective values grabbed from the process environment.