How-To

function false.post/4 is undefined: Swoosh Mailgun fix

This error means Swoosh's :api_client setting is false, so the Mailgun adapter ends up calling false.post/4. Set it to Swoosh.ApiClient.Finch with a running Finch process, or to Hackney, then fully restart the app. Below: where the false usually comes from, working config, and how to verify.

Sohail HussainSohail Hussain8 min read

Short answer

  • The error means Swoosh's :api_client setting is false, so the Mailgun adapter tried to call post/4 on a module named false instead of an HTTP client.
  • Set the :swoosh api_client option to Swoosh.ApiClient.Finch (with a Finch process running) or Swoosh.ApiClient.Hackney in the environment where you use Mailgun, then fully restart the app.
  • The request never reached Mailgun, so your API key and domain have not been tested yet.

You switched a Phoenix or plain Elixir app to the Mailgun adapter, tried to send, and got this:

(UndefinedFunctionError) function false.post/4 is undefined (module false is not available)

This page explains where the false comes from, how to replace it with a working HTTP client, and what usually fails next.

Why it happens

Swoosh's HTTP-based adapters, Mailgun included, do not make HTTP requests themselves. They pass the request to whatever module is set as the API client. The call has four arguments (roughly URL, headers, body and the email), which is why the error says post/4.

If the client setting is false, Elixir evaluates a call on a module called false. No such module exists, so you get UndefinedFunctionError. This is a local configuration problem. Mailgun was never contacted.

The causes, most common first:

  1. A leftover false from a generated config file. Phoenix generators typically write config :swoosh, :api_client, false into the dev and test environments, because the local and test adapters need no HTTP. If you point dev at Mailgun to try real delivery, the false is still there.
  2. Config load order. Files load in this order: config/config.exs, the environment file it imports, then config/runtime.exs. The last value wins, so a false in an environment file overrides a good value in config.exs.
  3. The client is set, but the app was not restarted. Swoosh config is read at boot, so a running node keeps the old value.
  4. No client dependency in mix.exs. This usually gives a different error once false is gone, but it is worth checking while you are in there.

A thread on the Elixir Forum reports this exact symptom with the Mailgun adapter. The thread does not show a confirmed fix, so the steps below come from how Swoosh dispatches requests. Check them against the Swoosh version in your mix.lock.

Step-by-step fixes

Fix 1: find the false and replace it

  1. Search your config for the setting:

    grep -rn "api_client" config/
    
  2. Look for a line like this in the environment you are running:

    config :swoosh, :api_client, false
    
  3. Replace it with a real client. Finch is the usual choice in a recent Phoenix app:

    config :swoosh, api_client: Swoosh.ApiClient.Finch, finch_name: MyApp.Finch
    
  4. Make sure the Mailgun adapter is configured for the same environment:

    # config/runtime.exs
    config :my_app, MyApp.Mailer,
      adapter: Swoosh.Adapters.Mailgun,
      api_key: System.fetch_env!("MAILGUN_API_KEY"),
      domain: System.fetch_env!("MAILGUN_DOMAIN")
    
  5. Replace MyApp and my_app with your application's names.

  6. Stop the app and start it again (mix phx.server or iex -S mix). Recompiling alone is not enough.

Restart, do not just recompile

Swoosh config is read at boot. A running node or an open IEx session keeps the old false value even after you edit the file.

Fix 2: start Finch in your supervision tree

Setting Swoosh.ApiClient.Finch without a running Finch process fails later with a different error. Add Finch to the children list in lib/my_app/application.ex:

children = [
  {Finch, name: MyApp.Finch},
  # ...the rest of your children
]

The finch_name in the Swoosh config must match this name. If your app already starts a Finch for other purposes, reuse its name instead of starting a second one. If :finch is not in your mix.exs deps, add it and run mix deps.get.

Fix 3: use Hackney instead

If you prefer Hackney, set it as the client and add the dependency:

config :swoosh, :api_client, Swoosh.ApiClient.Hackney
# mix.exs
{:hackney, "~> 1.9"}

Run mix deps.get and restart. Newer Swoosh releases also ship other clients, such as one based on Req. Check the Swoosh docs on HexDocs for what your installed version supports. For a Phoenix app I'd pick Finch, since it is usually already in the project.

Fix 4: confirm the setting took effect

In iex -S mix, run:

Application.get_env(:swoosh, :api_client)

You want a module name such as Swoosh.ApiClient.Finch, not false. Then check the mailer:

Application.get_env(:my_app, MyApp.Mailer)

It should show Swoosh.Adapters.Mailgun with your api_key and domain. If :api_client still returns false, a later config file is overriding your change. Run the grep again and look at runtime.exs too.

Cause, symptom and fix at a glance

CauseWhat you seeFix
api_client is false in the active envfalse.post/4 is undefinedSet Finch or Hackney (Fix 1)
Client set in config.exs, false in dev.exsSame error in dev onlyRemove or change the later false
Finch configured but not startedA different error about the Finch name or poolAdd {Finch, name: MyApp.Finch} (Fix 2)
Config edited, app not restartedSame error as beforeFull restart

Errors you may hit next

Once the client works, the request reaches Mailgun. Problems then come back as HTTP responses instead of a crash.

What you seeLikely cause
401 UnauthorizedWrong or missing api_key
404 or "domain not found"Wrong domain, or the domain lives in another Mailgun region
400 about the senderfrom address not on your verified Mailgun domain
Accepted but lands in spamDomain DNS not set up; see how to set up SPF, DKIM and DMARC

Region mismatches are the one people trip over most. Mailgun's help center lists separate API base URLs for the US, EU and AU regions, so a domain created in the EU region will not work against the US host. Swoosh's Mailgun adapter has a base_url option for this; check the adapter's docs for the exact value format. Mailgun's own overview of how to start sending email also notes that production sending needs a domain set up and verified through DNS.

Keep dev on the local adapter

Leave dev and test on the local and test adapters, where false is correct, and try Mailgun in a staging environment. That way nobody flips the client setting back and forth.

Checklist before you try again

  • grep -rn "api_client" config/ shows no false for the environment you are running.
  • The client dependency (:finch or :hackney) is in mix.exs and mix deps.get has run.
  • The finch_name in config matches the name started in application.ex.
  • The mailer config uses Swoosh.Adapters.Mailgun with api_key and domain set.
  • You stopped and restarted the app after the last config change.
  • Application.get_env(:swoosh, :api_client) in IEx returns a module, not false.

If nothing works

Swoosh is an open-source library, so the best place to ask is the Swoosh issue tracker or the Elixir Forum thread on this error. For delivery problems after the client works, contact Mailgun support. Include:

  • the full stack trace, not just the first line
  • your Swoosh and Elixir versions from mix.lock
  • the config/ files involved, with the API key removed
  • the output of the two Application.get_env checks above
Subject: Swoosh Mailgun adapter raises UndefinedFunctionError false.post/4

Hi,

Sending with Swoosh.Adapters.Mailgun raises:
(UndefinedFunctionError) function false.post/4 is undefined (module false is not available)

Swoosh version: {version from mix.lock}
Elixir/OTP: {versions}
Environment: {dev/test/prod}
Application.get_env(:swoosh, :api_client) returns: {value}

I already tried: setting :api_client to Swoosh.ApiClient.Finch, starting Finch
in the supervision tree, and fully restarting the app.

Full stack trace and sanitized config are attached.

Common questions

Why does Phoenix set api_client to false at all?

The default dev and test adapters, Swoosh.Adapters.Local and Swoosh.Adapters.Test, do not send HTTP requests, so they need no client. Setting it to false is harmless until you switch to an HTTP adapter like Mailgun.

Can I use the Mailgun SMTP adapter instead and skip the HTTP client?

Swoosh has an SMTP adapter that does not use an HTTP API client, so the error would not apply. You would then use Mailgun's SMTP credentials instead of the API key. Check the Swoosh docs for the extra dependency that adapter needs.

Did Mailgun reject my request?

No. The error is raised inside your app before any network call, so Mailgun never saw the request and its logs will show nothing for this attempt.

Do I need the client setting in every environment?

Only in environments that use an HTTP adapter. Production using Mailgun needs a real client; dev and test can keep false while they use the local and test adapters.

strategymailgun-swoosherror-codes
Share this article
Sohail Hussain

Sohail Hussain

Founder & CEO at Mailneo

Building Mailneo — AI-powered email marketing for growing businesses.

Ready to supercharge your email marketing?

Start sending smarter emails with AI-powered campaigns. No credit card required.

Get Started Free