I’m working with Mailgun PHP SDK version 3.5.2 which comes with Symfony HTTP client by default. I want to replace it with Guzzle but running into issues.
Here’s what I tried:
public function sendEmail() {
// Initialize Guzzle client
$httpClient = new \GuzzleHttp\Client();
// Create Mailgun instance with API key and client
$mailgun = Mailgun::create('key-example123', $httpClient);
// Send email message
$mailgun->messages()->send('mydomain.com', [
'from' => '[email protected]',
'to' => '[email protected]',
'subject' => 'Test Email',
'text' => 'This is a test message using Guzzle client.'
]);
}
But I get this error:
Fatal error: Uncaught TypeError: Argument 2 passed to Mailgun\Mailgun::create() must be of the type string, object given
I also modified my composer.json to include Guzzle:
{
"require": {
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.0",
"php-http/client-common": "^2.2.1"
}
}
What’s the proper way to configure Guzzle as the HTTP client for this Mailgun version?
Had this exact headache a few months back when switching clients. The error occurs because Mailgun::create() expects either a string for the endpoint URL as the second parameter, or you need to use a different constructor approach entirely. Instead of passing the Guzzle client directly to create(), try using the Mailgun constructor with a proper HTTP client adapter. Install the guzzle adapter first with composer require php-http/guzzle7-adapter, then use: $httpClient = new \GuzzleHttp\Client(); $mailgun = new Mailgun(‘key-example123’, new \Http\Adapter\Guzzle7\Client($httpClient), ‘https://api.mailgun.net’); This approach worked for me when the create() method wasn’t cooperating with custom clients.
The issue stems from version compatibility between your Mailgun SDK and the HTTP client interface. Mailgun 3.5.2 expects an HTTP client that implements PSR-18 standards, not the raw Guzzle client object. You need to wrap your Guzzle client properly. First, ensure you have the correct dependencies installed: composer require php-http/guzzle7-adapter php-http/message. Then modify your code like this: $httpClient = new \GuzzleHttp\Client(); $adapter = new \Http\Adapter\Guzzle7\Client($httpClient); $mailgun = Mailgun::create(‘key-example123’, $adapter); I ran into this same problem last year when migrating from Symfony’s client. The adapter acts as a bridge between Guzzle and Mailgun’s expected interface. Make sure your Guzzle version is compatible with the adapter version you’re using.
you’re passing the guzzle client directly but mailgun needs an http adapter. install php-http/guzzle7-adapter then use $adapter = new Http\Adapter\Guzzle7\Client($httpClient); and pass that adapter to mailgun::create() instead of the raw guzzle client.