Manipulate Mailgun Bounce Lists: Show, Add, and Delete Email Addresses. All from the terminal.
I recently came across a situation where a client reached their disk usage limit. As a result, they were unable to receive emails. This went un-noticed for a couple days (I didn’t manage the server at the time, I do now).
This client has a couple different WordPress sites with several employees receiving various notification emails. All their sites use Mailgun and the Mailgun WordPress plugin for sending emails. During the time they were unable to receive email, a few employee email addresses got placed on a Mailgun bounce list with a status of 550 Administrative prohibition.
For some background, here’s how Mailgun describes a bounce, as found in the Mailgun documentation:
Bounce list stores events of delivery failures due to permanent recipient mailbox errors such as non-existent mailbox. Soft bounces (for example, mailbox is full) and other failures (for example, ESP rejects an email because it thinks it is spam) are not added to the list.
Subsequent delivery attempts to an address found in a bounce list are prevented to protect your sending reputation.
I first noticed the bounce issue in the logs, like in the image below. After not being able to find a way to manage email addresses on the bounce list from the browser, I hit up Google and was pleased to find that you can interact with Mailgun bounce lists via their API.
Show Email Addresses in the Mailgun Bounce List
To list email addresses on the bounce list, do something like this on the terminal/command line, replacing key-xxx-xxx with your actual Mailgun API key:
curl -s --user 'api:key-xxx-xxx' -G https://api.mailgun.net/v3/mg.longrendev.io/bounces
You can find your Mailgun API key on the Mailgun dashboard, under API Keys. The Mailgun API will return JSON, which is a bit difficult to read in the terminal. I usually copy the output and paste it into this JSON formatter, which makes the data much easier to read, as you can see in the screenshot above.
Even when the formatted JSON in it’s raw form is easier to read. See, here’s the returned JSON, in it’s original form:
Now here’s the pretty, formatted JSON as raw text:
Much easier to read, right? Those of you using REST clients like Postman will have your results automatically prettified, removing the need using a site like the JSON formatter I typically use.
Delete an Email Address from the Mailgun Bounce List
If you’ve found an email address you’d like to remove from the Mailgun bounce list, or already know the email you want to remove, do this from a terminal and replace email@example.com with the real email address to delete. And of course, replace key-xxx-xxx with your actual Mailgun API key:
curl -s --user 'api:key-xxx-xxx' -H "Accept: application/json" -X DELETE https://firstname.lastname@example.org
Add an Email Address to the Mailgun Bounce List
Sometimes you may wish to manually add an email address to the Mailgun bounce list. This can be done very easily with the CURL command below. It will add email@example.com to the Mailgun bounce list, so make sure to change that to the email you really want to add to the list.
curl -s --user 'api:key-7g0wl66k2hxonzq5-0nbzhw68r2oc8n8' https://api.mailgun.net/v3/mg.longrendev.io/bounces -F firstname.lastname@example.org'
Not much concerning Mailgun bounce lists specifically. It’s possible to add multiple addresses to a bounce list at once, but that gets a little more difficult from the terminal as it requires sending JSON to the Mailgun API. Using a client like Postman would be a better option if you intend on sending much data.
The Mailgun API can be used to do all sorts of stuff, like pull stats and to create new domains. It’s a powerful API and one of my favorites to work with.