Mailgun API only sends emails to primary account address

I’m trying to set up an email subscription form using PHP and the Mailgun API. The problem is that it only works with the main email address I used to create my Mailgun account. When I use that email in the form, I get a confirmation letter. But it doesn’t work for any other email addresses. What’s going on?

Here’s a simplified version of my code:

// Config
$mailgun_key = 'your_mailgun_key';
$mailgun_domain = 'your_sandbox_domain.mailgun.org';
$mailing_list = 'list@your_sandbox_domain.mailgun.org';

// Initialize Mailgun
$mg = new Mailgun($mailgun_key);

// Form submission
if ($_POST) {
  $name = $_POST['name'];
  $email = $_POST['email'];

  // Validate email
  $valid = $mg->validateEmail($email);

  if ($valid) {
    // Send confirmation email
    $mg->sendMessage($mailgun_domain, [
      'from' => '[email protected]',
      'to' => $email,
      'subject' => 'Confirm your subscription',
      'html' => "Hi {$name}, please confirm your subscription."
    ]);

    // Add to mailing list
    $mg->addListMember($mailing_list, [
      'name' => $name,
      'address' => $email,
      'subscribed' => 'no'
    ]);
  }
}

Am I missing something in the configuration or API usage? Why aren’t other email addresses working?

I’ve been through this exact issue before, and it’s definitely frustrating. The problem is indeed the sandbox domain, as alexr1990 mentioned. When I was setting up my first Mailgun integration, I spent hours trying to figure out why emails weren’t going through to other addresses.

Here’s what worked for me: I had to set up a custom domain and verify it with Mailgun. It’s a bit of a process, but totally worth it. You’ll need to add some DNS records to your domain, which might sound daunting if you’re not familiar with it, but Mailgun’s documentation walks you through it step-by-step.

Once I had my custom domain set up, emails started flowing to all addresses without a hitch. Just remember to update your $mailgun_domain variable in your code to reflect your new custom domain. Also, don’t forget to switch your API calls from the sandbox URL to the production URL.

Trust me, taking the time to set this up properly will save you a lot of headaches down the line, especially when you’re ready to scale your mailing list.

hey mate, sounds like ur using mailgun sandbox domain. those r restricted to authorized recipients only. switch to a custom domain to send to any email. u gotta verify ur domain first tho. check mailgun docs for setup steps. good luck!

As someone who’s implemented Mailgun in several projects, I can confirm that the sandbox domain is the culprit here. It’s a common stumbling block for newcomers.

To resolve this, you’ll need to set up a custom domain with Mailgun. This involves adding specific DNS records to your domain’s configuration. Once verified, you can send emails to any address.

Remember to update your code with the new domain and switch to the production API endpoint. Also, ensure you’re using the correct API key - there are separate keys for sandbox and production environments.

Lastly, consider implementing email validation and handling bounce notifications to maintain a clean recipient list. This will improve your email deliverability in the long run.