Mailgun only sends emails to account owner address

I’m building a newsletter signup form using PHP and the Mailgun service. I have a weird problem where emails only get delivered to the email address I used when signing up for my Mailgun account. When I test the form with my main email, everything works fine and I receive the confirmation message. But when I try using different email addresses, nothing happens.

Here’s my configuration file:

<?php
require_once 'vendor/autoload.php';

define('MG_API_KEY', 'key-3df51g6f34d91c1e777g4f....');
define('MG_PUBLIC_KEY', 'pubkey-9dg8236007....');

define('MG_DOMAIN', 'sandbox2b4fbff8785d5b0185ggb9e72956eeg6.mailgun.org');
define('SUBSCRIBER_LIST', '[email protected]');
define('MG_SECRET_KEY', '...');

$mgClient = new Mailgun\Mailgun(MG_API_KEY);
$mgValidator = new Mailgun\Mailgun(MG_PUBLIC_KEY);
$mgOptInHandler = $mgClient->OptInHandler();
?>

And here’s my main processing script:

<?php
require_once 'config.php';

if(isset($_POST['user_name'], $_POST['user_email']))
{
    $userName = $_POST['user_name'];
    $userEmail = $_POST['user_email'];

    $emailCheck = $mgValidator->get('address/validate', [
        'address' => $userEmail
    ])->http_response_body;

    if($emailCheck->is_valid)
    {
        $confirmHash = $mgOptInHandler->generateHash(SUBSCRIBER_LIST, MG_SECRET_KEY, $userEmail);

        $mgClient->sendMessage(MG_DOMAIN, [
            'from' => '[email protected]',
            'to' => $userEmail,
            'subject' => 'Confirm your newsletter subscription',
            'html' => "Hi {$userName}<br><br>Thanks for subscribing! Please click below to confirm"
        ]);

        $mgClient->post('lists/' . SUBSCRIBER_LIST . '/members', [
            'name' => $userName,
            'address' => $userEmail,
            'subscribed' => 'no'
        ]);

        header('Location: http://localhost:8888/project/index.php');
    }
}
?>

What could be causing this issue? Is there some setting I’m missing?

Sandbox domains in Mailgun are locked down hard - they’ll only send to pre-approved email addresses. I banged my head against this for hours thinking my code was broken, but it’s just how sandboxes work. You’ve got two options: Add authorized recipients manually in your Mailgun dashboard (fine for testing a few addresses), or set up your own verified domain and swap out that sandbox domain in your MG_DOMAIN constant. The domain verification needs some DNS records, but once that’s done you can email anyone without restrictions. Definitely go with option two if you’re building a real newsletter signup.

yep, you’re hitting the sandbox domain limit. Mailgun lets you send to just your account emails. add more emails in your dashboard or get a custom domain to send to all.

It’s the sandbox domain restriction. Mailgun’s sandbox domains only deliver emails to addresses you’ve authorized in your dashboard - they’re just for testing. You need to either add the recipient emails as authorized recipients in your Mailgun control panel, or upgrade to a verified custom domain. I hit this same issue when I started using Mailgun for a client. Their docs don’t explain the sandbox limitation well, so it catches tons of developers. Once you verify your own domain and update MG_DOMAIN in your config, you can send to any email without restrictions.