I’m trying to create a new domain using the Mailgun API but keep getting an error saying my parameters are invalid. My API key should be correct so I’m not sure what’s going wrong.
Here’s the code I’m using:
$mailgunClient = new Mailgun($this->fetchAPIKey());
$domainName = $this->getDomainName();
// Making the API request
$response = $mailgunClient->post("domains", array(
'name' => 'mail.example-site.com',
'smtp_password' => 'mySecretPass123'));
I’m wondering if I need to set up the SMTP password beforehand or if this API call should create it automatically? The error message isn’t very helpful and I can’t figure out what parameter is causing the issue.
It seems the issue may stem from your authentication and configuration settings. Ensure that your Mailgun client is correctly set up for your selected region, especially if it’s outside the US. Confirm that you’re using the private API key, which should begin with ‘key-’. I experienced a similar issue; initializing the client with the full base URL often resolves it. For instance, use new Mailgun($apiKey, new HttpAdapter(), 'https://api.mailgun.net')
. Importantly, you can omit the smtp_password parameter as Mailgun can generate it automatically, which shouldn’t lead to invalid parameters.
I ran into this exact same problem a few months back and it turned out to be a combination of issues. The main culprit was that I was hardcoding the domain name instead of using the variable I had set up. In your code you’re setting $domainName = $this->getDomainName();
but then using ‘mail.example-site.com’ directly in the array. Try using the variable instead. Also check that your domain name format is correct - it should just be the domain without any protocol prefixes. Another thing that caught me was the API version in the client initialization. Make sure you’re using the correct version for your Mailgun client library. The invalid parameters error usually means one of your field names is wrong or the value format is unexpected.
hmm looks like your missing the endpoint url in your post call. should be something like $mailgunClient->post("https://api.mailgun.net/v3/domains", array(...))
instead of just “domains”. also try removing the smtp_password parameter first and let mailgun generate it automaticaly.