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

An adapter that sends email using the Mailgun API.

For reference: Mailgun API docs


Mailgun adapter requires Plug to work properly.

Configuration options

  • :api_key - the API key used with Mailgun
  • :domain - the domain you will be sending emails from
  • :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")

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

Custom headers

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

Link to this section Summary

Link to this section Functions

Callback implementation for Swoosh.Adapter.validate_config/1.

Callback implementation for Swoosh.Adapter.validate_dependency/0.