View Source Swoosh.Adapters.Mua (Swoosh v1.16.5)

An adapter that sends email using the SMTP protocol.

Dependency

Underneath this adapter uses Mua, and Mail, and Castore libraries, add them to your mix.exs file.

Example

# mix.exs
def deps do
  [
   {:swoosh, "~> 1.3"},
   {:mua, "~> 0.1.0"},
   {:mail, "~> 0.3.0"},
   {:castore, "~> 1.0"}
  ]
end

# config/config.exs for sending email directly
config :sample, Sample.Mailer,
  adapter: Swoosh.Adapters.Mua

# config/config.exs for sending email via a relay
config :sample, Sample.Mailer,
  adapter: Swoosh.Adapters.Mua,
  relay: "smtp.matrix.com",
  port: 1025,
  auth: [username: "neo", password: "one"]

# lib/sample/mailer.ex
defmodule Sample.Mailer do
  use Swoosh.Mailer, otp_app: :sample
end

For supported configuration options, please see option()

Sending email directly

When relay option is omitted, this adapter will send email directly to the receivers' host. All receivers must be on the same host, otherwise Swoosh.Adapters.Mua.MultihostError is raised.

In this configuration, you need to ensure that your application can make outgoing connections to port 25 and that your sender domain has appropriate DNS records set, e.g. SPF or DKIM.

Short-lived connections

Note that each deliver call results in a new connection to the receiver's email server.

Sending email via a relay

When relay option is set, this adapter will send email through that relay. The relay would usually require authentication. For example, you can use your own GMail account with an app password.

Short-lived connections

Note that each deliver call results in a new connection to the relay. This is less efficient than gen_smtp which reuses the long-lived connection. Further versions of this adapter might fix this if it ends up being a big problem ;)

CA certificates

By default CAStore.file_path/0 is used for :cacertfile, but you can provide your own or use the system ones and supply them in as :cacerts

:ok = :public_key.cacerts_load()
[_ | _] = cacerts = :public_key.cacerts_get()

config :sample, Sample.Mailer,
  adapter: Swoosh.Adapters.Mua,
  transport_opts: [
    cacerts: cacerts
  ]

Note that when using :cacertfile option, the certificates are decoded on each new connection. To cache the decoded certificates, set :persistent_term for :mua to true.

config :mua, persistent_term: true

Summary

Types

@type option() :: Mua.option() | {:relay, Mua.host()}

Functions

@spec deliver(Swoosh.Email.t(), [option()]) ::
  {:ok, Swoosh.Email.t()}
  | {:error, Mua.error() | Swoosh.Adapters.Mua.MultihostError.t()}

Callback implementation for Swoosh.Adapter.deliver/2.

Callback implementation for Swoosh.Adapter.validate_config/1.

Callback implementation for Swoosh.Adapter.validate_dependency/0.