View Source Swoosh.Adapters.Mailgun (Swoosh v1.16.3)

An adapter that sends email using the Mailgun API.

For reference: Mailgun API docs

This adapter requires an API Client. Swoosh comes with Hackney, Finch and Req out of the box. See the installation section for details.


Mailgun adapter requires Plug and :multipart to work properly.

Configuration options

  • :api_key - the API key used with Mailgun
  • :domain - the domain you will be sending emails from. For sandbox domains, make sure to use the sandbox address, for example: then you should set domain: "".
  • :base_url - the url to use as the API endpoint. For EU domains, use


# config/config.exs
config :sample, Sample.Mailer,
  adapter: Swoosh.Adapters.Mailgun,
  api_key: "my-api-key",
  domain: ""

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

Using with provider options

import Swoosh.Email

|> from({"T Stark", ""})
|> to({"Steve Rogers", ""})
|> to("")
|> reply_to("")
|> cc({"Bruce Banner", ""})
|> cc("")
|> bcc({"Clinton Francis Barton", ""})
|> bcc("")
|> subject("Hello, Avengers!")
|> html_body("<h1>Hello</h1>")
|> text_body("Hello")
|> put_provider_option(:custom_vars, %{"key" => "value"})
|> put_provider_option(:recipient_vars, %{"": %{var1: 123}, "": %{var1: 456}})
|> put_provider_option(:sending_options, %{dkim: "yes", tracking: "no"})
|> put_provider_option(:tags, ["worldwide-peace", "unity"])
|> put_provider_option(:template_name, "avengers-templates")
|> put_provider_option(:template_options, %{version: "initial"})

Provider options

  • :custom_vars (map) - used to translate to v:my-var, now h:X-Mailgun-Variables, add custom data to email

  • :recipient_vars (map) - recipient-variables, assign custom variable for each email recipient

  • :sending_options (map) - o:my-key, all the sending options

  • :tags (list[string]) - o:tag, was added in before :sending_options, kept for backward compatibility, use :sending_options instead

  • :template_name (string) - template, name of template created at Mailgun

  • :template_options (map) - version, text, variables and/or any future possible values

Custom headers

Headers added via Email.header/3 will be translated to (h:) values that Mailgun recognizes.



Link to this function

deliver(email, config \\ [])

View Source

Callback implementation for Swoosh.Adapter.deliver/2.

Callback implementation for Swoosh.Adapter.validate_config/1.

Callback implementation for Swoosh.Adapter.validate_dependency/0.