# Validate Mailgun Mailing Lists

URL: https://email-validation.org/docs/guides/mailgun/
Updated: 2026-11-28

> Documentation index: https://email-validation.org/llms.txt

Check the members of a Mailgun mailing list and write each verdict into five of the member's vars: `ev_status`,
`ev_reason`, `ev_action`, `ev_checked_at` and `ev_address_hash` (a hash of the address that was checked, never the
address). The member's other vars stay as they are. Nothing else in your account changes unless you ask for it: no
member is deleted or unsubscribed, and nothing is added to your suppressions. A script you run on your own computer
does it, with your own keys.

Download it: [email-validation-mailgun.zip](https://email-validation.org/downloads/mailgun/email-validation-mailgun.zip) holds the script
`mailgun.mjs` and the MIT licence.

## What you need

- An API key of ours from the [API keys page](https://email-validation.org/app/api-keys/) with **Bulk lists** (off by default: turn it on when
  you create the key).
- A Mailgun account API key, from Mailgun → Account Settings → API keys. A domain sending key will not work: it can
  only send messages. A key created with a role must be allowed to update mailing lists (Admin, Developer or
  Support; Analyst can only read), and to add unsubscribes or bounces if you use `--suppress`. A key's role cannot
  be changed later: make a new key.
- The region your Mailgun account is in (US or EU).
- Node 22.18 or later.

Our key goes only to our API, the Mailgun key only to your region's Mailgun host (`api.mailgun.net` or
`api.eu.mailgun.net`), and neither is written to a file or a log. The script prints counts and job ids, never a
member's address.

Two options name a region, and they are not the same: `--mailgun-region eu` is your Mailgun account's region (US by
default), and `--region eu` is where we validate and store your list (our data residency, US by default).

## Try it free first

1. Unzip the download and go into its folder, then list your mailing lists with their addresses and member counts:

   ```bash
   unzip email-validation-mailgun.zip
   cd email-validation-mailgun
   export EV_API_KEY=…  MAILGUN_API_KEY=…
   node mailgun.mjs --lists
   ```

   For an EU account, add `--mailgun-region eu` to this and every later command (or `export MAILGUN_REGION=eu`
   once). If it prints no list, check the region: Mailgun keeps lists in the account's own region.

2. In Mailgun, make a new mailing list for the try (for example `test@mg.example.com`) and add one member,
   `no_mailbox@sandbox.email-validation.org` (one of our free test addresses, on a domain that receives no mail).
   Mailgun mails a list's members when a message is sent to the list's address, so send nothing to this list.

3. Run the script on the test list only. Never try it on your real list: a run checks, and charges for, every member
   without a verdict.

   ```bash
   node mailgun.mjs --list test@mg.example.com
   ```

   The member gets `ev_status` `invalid` and `ev_action` `remove`. Checks of our test addresses are free. Then delete
   the test list in Mailgun, so that no later send can reach it.

## Clean your list

See what a run would check (nothing is uploaded, paid or written), then check the list and write the verdicts:

```bash
node mailgun.mjs --list newsletter@mg.example.com --dry-run
node mailgun.mjs --list newsletter@mg.example.com
```

Each checked address uses one credit; `unknown` results, malformed addresses, duplicates and sandbox addresses are
free. Members unsubscribed from the list are left out unless you pass `--include-unsubscribed`. Members that already
have a verdict for their current address are skipped unless you pass `--revalidate`; a member whose address was
changed in Mailgun since is checked again. Add `--region eu` to validate and store the list in the EU.

Use the vars in Mailgun templates as `%recipient.ev_status%` and in your own exports. Filter on `ev_action`: spam
traps and complainers are `ev_status` `valid` with `ev_action` `remove`.

The script reads the list 100 members a page and spaces its Mailgun requests (5 a second by default, `--rate`, at
most 25; Mailgun allows 300 a second per account). Each written member takes two requests, so a large list takes a
while to write back. Exit codes: `0` done, `1` a write failed or the run stopped (run again to resume), `2` wrong
arguments, `75` the bulk list was still running when the script stopped waiting (`--wait-minutes`, 240 by default).

## The addresses marked remove

Writing vars does not change who receives mail: a message sent to the list address still goes to every subscribed
member, the ones marked `remove` included. Act on them in Mailgun yourself, or let the script do one of these for
the statuses you name among `invalid`, `disposable`, `catch_all` and `unknown` (never `valid`). Both are off by
default:

| Option | What it does in Mailgun | How to undo it |
|---|---|---|
| `--unsubscribe invalid,disposable` | Sets those members to unsubscribed on this list. They stay on the list, with their vars, and mail sent to the list address skips them. | Subscribe them again in Mailgun. |
| `--suppress invalid` | Adds those addresses to your sending domain's suppressions: an unsubscribe from all mail, or a bounce with `--suppress-as bounce`. Mailgun then drops every message from that domain to them, through any list or none. | Delete the record in Mailgun (Suppressions). |

`--suppress` uses the list's own domain, or the one you name with `--suppress-domain`. Check that name first: the
domain is first used after the list was checked, and a wrong one stops the suppressions there (run again with the
right one: no new charge). With `unknown` named, a member whose `ev_reason` is `timeout` or `internal_error` is still
left as it is: that check failed on our side, and the next run checks the address again. Each option also acts on
members an earlier run marked with those statuses for their current address, at no cost. Spam traps and complainers
(`valid`, `remove`) are never touched by either option: act on those in Mailgun yourself.

## Re-runs and schedules

If a run stops (no credits, a closed laptop, an outage), run the same command again from the same folder: the script
keeps the bulk list's id in a small state file (ids and the list's address only; no member's address, no key), resumes the same bulk list and never
pays twice (within the 30 days a list is kept). Mailgun sends no webhook when a member is added
to a list, so run the script on a schedule (cron, daily or weekly): each run checks only the members without a verdict
for their address now, and those still `unknown`.

## Check before you send

To keep a single message away from an address that cannot receive mail, ask our API first and call Mailgun only when
the answer allows it. This example runs as it is in Node 22 or later (save it as `check.mjs`); it checks one of our
test addresses, and its Mailgun call is left as a comment for you to fill in with your own code:

```js
const to = 'no_mailbox@sandbox.email-validation.org'; // a free test address that receives no mail

async function mayReceive(address) {
  try {
    const response = await fetch('https://api.email-validation.org/v1/validate', {
      method: 'POST',
      headers: { Authorization: `Bearer ${process.env.EV_API_KEY}`, 'Content-Type': 'application/json' },
      body: JSON.stringify({ email: address, timeout: 5 }),
      redirect: 'error',
      signal: AbortSignal.timeout(6000),
    });
    if (!response.ok) {
      console.warn(`Not checked (HTTP ${response.status}): check your key and credits.`);
      return true; // fail open: a problem on our side never stops your mail
    }
    const result = await response.json();
    return result.status !== 'invalid';
  } catch {
    return true; // a timeout or a network error: fail open too
  }
}

if (await mayReceive(to)) {
  // Your Mailgun send goes here, for example:
  // await fetch(`https://api.mailgun.net/v3/${domain}/messages`, {
  //   method: 'POST',
  //   headers: { Authorization: `Basic ${btoa(`api:${process.env.MAILGUN_API_KEY}`)}` },
  //   body: new URLSearchParams({ from, to, subject, text }),
  // });
  console.log('Allowed: send it with Mailgun.');
} else {
  console.log('Not sent: this address cannot receive mail.');
}
```

```bash
EV_API_KEY=… node check.mjs   # prints "Not sent: this address cannot receive mail."
```

Only a definite `invalid` holds the message back; every other answer, and every failure, lets it through. Each check
uses one credit, every time (`unknown` results and sandbox addresses are free). Never send to a sandbox address or to an address you made
up, through Mailgun or anything else: a send to an address that cannot receive mail bounces and hurts your sending
reputation. For an EU account, Mailgun's host is `api.eu.mailgun.net`; for checks in the EU, our host is
`https://api-eu.email-validation.org`.

## More on Mailgun

- [Mailgun integration](https://email-validation.org/integrations/mailgun/)
