Swoosh v0.25.4 Swoosh.Mailer View Source

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.

Per module configuration is also supported, it has priority over mix configs:

defmodule Sample.Mailer do
  use Swoosh.Mailer, otp_app: :sample,
    adapter: Swoosh.Adapters.Sendgrid,
    api_key: "SG.x.x"
end

System environment variables can be specified with {:system, "ENV_VAR_NAME"}:

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@example.com") |> to("steve.rogers@example.com")
%Swoosh.Email{from: {"", "tony.stark@example.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@example.com") |> to("steve.rogers@example.com")
%Swoosh.Email{from: {"", "tony.stark@example.com"}, ...}
iex> Mailer.deliver(email, domain: "jarvis.com")
:ok

Link to this section Summary

Functions

Interpolate system environment variables in the configuration.

Parse configs in the following order, later ones taking priority

Link to this section Functions

Link to this function

deliver_many(emails, config)

View Source
Link to this function

interpolate_env_vars(config)

View Source

Interpolate system environment variables in the configuration.

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

Link to this function

parse_config(otp_app, mailer, mailer_config, dynamic_config)

View Source

Parse configs in the following order, later ones taking priority:

  1. mix configs
  2. compiled configs in Mailer module
  3. dynamic configs passed into the function
  4. system envs