How-To

bad_cert,hostname_check_failed: Fix for Brevo in Elixir

{bad_cert,hostname_check_failed} means Erlang's TLS client received a certificate that does not cover the hostname you connected to. When calling Brevo from Elixir, set verify_peer, cacerts from :public_key.cacerts_get(), a matching server_name_indication and the HTTPS hostname match function, then confirm the host you dial.

Sohail HussainSohail Hussain10 min read

Short answer

  • The error means the certificate Brevo's server presented does not list the hostname your client checked it against. In practice the name you dial, the SNI value and the hostname check disagree, or something on your network is replacing the certificate.
  • Pass complete ssl options: verify_peer, cacerts from the OS trust store, and a server_name_indication that equals the host in the URL. Then confirm what the server presents with openssl s_client.
  • Do not set verify_none to make the error disappear. It also removes the check that you are talking to Brevo.

You are calling Brevo from Elixir and the request fails with this:

{:tls_alert, {:handshake_failure, 'TLS client: In state certify ... generated CLIENT ALERT: Fatal - Handshake Failure - {bad_cert,hostname_check_failed}'}}

Sometimes it is wrapped in an HTTPoison %HTTPoison.Error{} or a Swoosh delivery error, but the reason {bad_cert,hostname_check_failed} is the same.

Why it happens

The error comes from Erlang's ssl application, not from Brevo's API and not from HTTPoison. HTTPoison passes the request to hackney, and hackney hands the handshake to ssl. Two checks run on the certificate, and only one of them is failing here:

  • Chain validation: is the certificate signed by a CA you trust? Failures look like unknown_ca or selfsigned_peer.
  • Hostname validation: does the certificate's subject alternative name (SAN) list the host you connected to? This is the check that fails with hostname_check_failed.

Your CA bundle is therefore probably fine. The name is what does not match. Per the identity-check rules standards bodies have long used, a client compares the hostname it used to open the connection (or the TLS server name) against the names in the certificate; one of the SAN names has to match. This applies to HTTPS and to SMTP over TLS alike (RFC 7817 covers the email-protocol version).

The most common causes, roughly in order:

  1. A partial ssl options list. People on the Elixir community forum report that the first error after upgrading OTP is {:options, :incompatible, [verify: :verify_peer, cacerts: :undefined]}. They add cacerts, then hit the hostname check next, often because a server_name_indication or hostname function was copied in wrong.
  2. SNI and URL host disagree. If you set server_name_indication to a name other than the one in the URL, the certificate is checked against the wrong name. A binary instead of a charlist is another way to get it wrong.
  3. Wrong or outdated host. An old tutorial may point at a legacy hostname, an internal alias, or an IP address. A certificate issued for a hostname does not match an IP.
  4. Something re-signing traffic. A corporate proxy, antivirus HTTPS scanning or a container egress filter presents its own certificate, which does not list Brevo's hostname.
  5. The wrong relay hostname for SMTP. The API host and the SMTP relay host are different names. Using the API host's SNI for the relay (or the reverse) fails.

Step-by-step fixes

Fix 1: pass complete ssl options to HTTPoison

:public_key.cacerts_get/0 needs OTP 25 or newer. Check with System.otp_release().

  1. Open the module that calls Brevo and find the HTTPoison.post/4 (or request/5) call.
  2. Add an ssl: option with the full list below. Explicit ssl: options replace library defaults, so a partial list is what causes the first error.
  3. Make the SNI value identical to the host in the URL.
host = "api.brevo.com"

ssl_opts = [
  verify: :verify_peer,
  cacerts: :public_key.cacerts_get(),
  server_name_indication: String.to_charlist(host),
  customize_hostname_check: [
    match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
  ]
]

HTTPoison.post(
  "https://#{host}/v3/smtp/email",
  Jason.encode!(payload),
  [{"api-key", api_key}, {"content-type", "application/json"}],
  ssl: ssl_opts
)

Brevo's help center lists sending a transactional email through the v3 API as one of its documented routes, and the API is the call this snippet makes.

  1. Use a charlist for the SNI value (String.to_charlist/1 or ~c"..."), because ssl expects one.
  2. Keep the host in one variable, as above, so the URL and SNI cannot drift apart.
  3. Never put an IP address in the URL.

Try the smaller change first

If your only error was the cacerts one, add just verify and cacerts and leave the SNI and hostname options out. hackney normally derives the server name from the URL, so a hand-written SNI is one more place for a mismatch.

Fix 2: Swoosh or gen_smtp through Brevo's SMTP relay

If the error comes from Swoosh's SMTP adapter, the TLS options live under tls_options. The SNI value has to be the relay host, not the API host. Brevo's help center gives smtp-relay.brevo.com as the SMTP server and says to use an SMTP key, not an API key, as the password.

config :my_app, MyApp.Mailer,
  adapter: Swoosh.Adapters.SMTP,
  relay: "smtp-relay.brevo.com",
  port: 587,
  username: System.get_env("BREVO_SMTP_LOGIN"),
  password: System.get_env("BREVO_SMTP_KEY"),
  ssl: false,
  tls: :always,
  auth: :always,
  tls_options: [
    verify: :verify_peer,
    cacerts: :public_key.cacerts_get(),
    server_name_indication: ~c"smtp-relay.brevo.com",
    customize_hostname_check: [
      match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
    ]
  ]

Port 587 upgrades a plain connection with STARTTLS, which is why tls: :always matters. Brevo's page says to select SSL or TLS encryption only for port 465, which is implicit TLS; that mode goes through the adapter's ssl: true setting, so check the Swoosh and gen_smtp docs for where the options go in that case. For how the modes differ, see our explainer on SSL/TLS for email and SMTP encryption. The Brevo guide to sending with SMTP has the current server, port and credential details, and it is worth checking before you trust an old config.

Fix 3: check what the server actually presents

If the options look right and it still fails, ask the server which names its certificate covers.

  1. From the same machine or container that fails, run:
openssl s_client -connect api.brevo.com:443 -servername api.brevo.com </dev/null 2>/dev/null \
  | openssl x509 -noout -ext subjectAltName
  1. Compare the DNS: names with the host in your error and your config.
  2. For SMTP, repeat with the relay: openssl s_client -starttls smtp -connect smtp-relay.brevo.com:587 -servername smtp-relay.brevo.com.
  3. Look at the issuer line too (openssl x509 -noout -issuer). An issuer that names your company, a security vendor or a proxy means traffic is being intercepted.
SymptomLikely causeFix
Config uses an old or misspelled hostName not on the certificateUse the host from Brevo's current docs
Host is an IP or internal aliasA hostname certificate cannot match itUse the public hostname
SNI differs from the URL hostCheck runs against the wrong nameDerive both from one variable
Issuer is a proxy or security productTraffic is re-signedTest from a clean network; add that CA only if you control it
Works with curl, fails in ElixirSNI type or options wrongRe-check Fix 1
cacerts_get() raises or returns nothingSlim image without CA certificatesInstall ca-certificates in the image

Do not ship verify_none

Advice to set verify: :verify_none does stop the error, because it stops all certificate checking. Your Brevo API key would then be sent to whoever answers. Use it only for a one-off local test, and remove it afterward.

Fix 4: a proxy or antivirus is in the path

If the SAN list is wrong for every Brevo host, something between you and Brevo is presenting its own certificate. Test from a network without the proxy, or from a phone hotspot. If the proxy is yours and intended, add its root CA to the trust store the app reads, rather than turning verification off. Apple's developer documentation makes the same point for its platforms: allow a mismatched name only for a certificate you control.

Checklist before you try again

  • OTP is 25 or newer, or you pass cacerts another way (for example a CA bundle package).
  • verify: :verify_peer and cacerts are both present in the same options list.
  • The URL host and server_name_indication come from one variable.
  • The SNI is a charlist.
  • The host is a DNS name, not an IP.
  • The API host is used for API calls and the relay host for SMTP.
  • The container image has CA certificates installed.
  • openssl s_client from the same machine shows the expected SAN names and a public CA as issuer.

How to keep it from coming back

Pin your OTP version in CI and in production images. Verification defaults changed across OTP releases, which is how code that worked in 2022 breaks after an upgrade. Keep hackney and HTTPoison updated. Put the Brevo hostnames in config instead of scattering string literals through the code. Hostnames and certificates can change, so as of November 2026 check Brevo's documentation for the current API and SMTP hosts.

If nothing works

If openssl s_client shows a certificate that does not match the official Brevo hostname even from a clean network, contact Brevo support through your account. If it matches from a clean network and fails only from yours, the cause is a proxy, a DNS override or your own config, and Brevo cannot fix that. Include the exact error text, the hostname and port, the full s_client output, the OTP, Elixir, hackney and HTTPoison versions, and the time of the test (with timezone).

Subject: TLS certificate hostname mismatch on {api.brevo.com or smtp-relay.brevo.com}

Hello,

My Elixir client fails the TLS handshake with:
{bad_cert,hostname_check_failed}

Host and port: {host}:{port}
Time of test: {timestamp with timezone}
OTP / Elixir / hackney / HTTPoison versions: {versions}
Network: {office, cloud region, or clean network}

openssl s_client output (SAN names and issuer) is attached. From a network without
any proxy the certificate shows: {names}.

Steps already tried: verify_peer with cacerts from :public_key.cacerts_get(),
SNI set to the URL host, tested from a second network.

Can you confirm the certificate for this hostname is expected to list it?

Thank you

Common questions

Why did this start after upgrading Erlang or Elixir?

Recent OTP releases default to verify_peer, so connections that were silently unverified now get checked. Code that passed no ssl options, or a partial list, starts failing until it supplies a CA source and a matching name.

Is it safe to use verify_none just for Brevo?

No. It disables chain and hostname checks, so you cannot tell Brevo from an attacker on the path. Use it at most as a short local test to separate TLS problems from credential problems.

Do I need cacerts if I use certifi?

If your hackney version already supplies certifi's bundle and you pass no ssl: options, you may not. Once you pass your own ssl: list with verify_peer, you must supply a CA source yourself, and :public_key.cacerts_get() reads the operating system's store.

Is this error a Brevo outage?

Unlikely. A handshake failure on certificate names is a client-side mismatch or interception in most cases. Check Brevo's status page if you want to rule out a provider incident, then run the openssl s_client test from a clean network.

strategybrevo-elixir-httpoisonerror-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