Switching from Symfony HTTP client to Guzzle HTTP client in Mailgun PHP SDK

I’m working with Mailgun PHP SDK version 3.5.2 which uses symfony/http-client by default. I want to switch to using guzzlehttp/guzzle instead but I’m running into issues.

Here’s what I tried:

public function sendEmail() {
    // Initialize Guzzle client
    $httpClient = new \GuzzleHttp\Client();
    
    // Try to create Mailgun instance with Guzzle
    $mailgun = Mailgun::create('key-example123', $httpClient);
    
    // Send the email
    $mailgun->messages()->send('mydomain.com', [
        'from' => '[email protected]',
        'to' => '[email protected]', 
        'subject' => 'Test message',
        'text' => 'This is a test email message.'
    ]);
}

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",
        "php-http/client-common": "^2.2.1",
        "php-http/discovery": "^1.9.1",
        "guzzlehttp/guzzle": "^7.0"
    },
    "suggest": {
        "php-http/guzzle7-adapter": "HTTP client adapter"
    }
}

What’s the proper way to configure Guzzle as the HTTP client for this Mailgun version? How can I resolve this type error?

u need the guzzle adapter. install php-http/guzzle7-adapter, then use new \Http\Adapter\Guzzle7\Client($httpClient) instead of passing guzzle directly to mailgun::create(). the sdk expects a psr-18 client, not a guzzle object.

This happens because Mailgun SDK wants a PSR-18 compatible client, not raw Guzzle. Hit the same issue migrating from Symfony HTTP client last year. You need to wrap your Guzzle client with an adapter. Install php-http/guzzle7-adapter and change your code:

$httpClient = new \GuzzleHttp\Client();
$adapter = new \Http\Adapter\Guzzle7\Client($httpClient);
$mailgun = Mailgun::create('key-example123', $adapter);

Or just install the guzzle adapter package and call Mailgun::create('key-example123') without the second parameter. HTTP discovery will automatically find and use Guzzle through the adapter.