April 16, 2026

·

3 min read

How to Extract Your Mailgun API Key Directly From the WordPress Database Using WP-CLI

How to Extract Your Mailgun API Key Directly From the WordPress Database Using WP-CLI

If you’ve managed enough WordPress sites over the years, you’ve probably run into a situation where you need to migrate an email setup from one plugin to another. Recently, I was working on a site where we were migrating from the official Mailgun API plugin over to the much more robust Post SMTP plugin.

The process is usually straightforward until you get to the setup wizard and it asks for your Mailgun API key.

If you are like me, tracking down login credentials for a client’s Mailgun account - or resetting passwords to get in - is a hassle and a waste of time. I knew the API key was already stored somewhere inside the WordPress database by the old Mailgun plugin. I just needed to get it.

Instead of dealing with resetting passwords or clicking around phpMyAdmin, I threw open my terminal and decided to extract the serialized data straight from the server using SSH and WP-CLI.

Here is exactly how I did it.

Step 1: SSH Into the Server

First, you need to SSH into your web server. Open your terminal and connect to your server.

ssh username@your-server-ip

Once you’re in, navigate to the root directory of your WordPress installation - the folder that contains your wp-config.php file.

cd /path/to/your/wordpress/installation

Step 2: Use WP-CLI to Pull the Serialized Option

The official Mailgun plugin stores all of its settings in the wp_options table under the option name mailgun. Because WordPress stores these arrays as serialized data, viewing it raw in the database looks like a messy, unreadable string.

Fortunately, WP-CLI is brilliant. It automatically unserializes option data for you when you run the wp option get command.

To make it even easier to read, I appended the JSON format flag:

wp option get mailgun --format=json

(Note: on some server setups where wp isn’t globally mapped but you have the phar file, you might run php wp-cli.phar option get mailgun --format=json)

Step 3: Grab Your Settings

Almost instantly, my terminal returned a cleanly formatted JSON object containing every setting the Mailgun plugin had saved:

{
  "region": "us",
  "useAPI": "1",
  "domain": "mg.yourdomain.com",
  "apiKey": "key-cf9e5d6d...",
  "secure": "1",
  "sectype": "tls",
  "track-clicks": "htmlonly",
  "track-opens": "1",
  "from-address": "[email protected]",
  "suppress_clicks": "no"
}

Right there in plain text was exactly what I needed: the apiKey, the domain, and the from-address.

Wrapping Up

With those three pieces of information, I jumped back into the WordPress dashboard, pasted them into the Post SMTP configuration wizard, and successfully routed the site’s email without ever having to log into the Mailgun dashboard.

WP-CLI is an incredible time-saver. If you understand how plugins store their data in wp_options, you can extract almost any locked-in configuration in a matter of seconds.

Subscribe To My YouTube Channel