I am using the recent version of Mailgun v 3.5.2 that comes with symfony/http-client when you install Mailgun using composer.
I have tried the code below but it is not working.
public function send (){
//client
$client = new \GuzzleHttp\Client();
// First, instantiate the SDK with your API credentials
$mg = Mailgun::create('key-****',$client); // For US servers
// Now, compose and send your message.
// $mg->messages()->send($domain, $params);
$mg->messages()->send('example.com', [
'from' => '[email protected]',
'to' => '[email protected]',
'subject' => 'The PHP SDK is awesome!',
'text' => 'It is so simple to send a message.'
]);
}
}
I am getting this error
Fatal error: Uncaught TypeError: Argument 2 passed to Mailgun\Mailgun::create() must be of the type string, object given
I have also updated the composer json in mailgun php as seen below
{
"name": "mailgun/mailgun-php",
"description": "The Mailgun SDK provides methods for all API functions.",
"license": "MIT",
"authors": [
{
"name": "Travis Swientek",
"email": "[email protected]"
}
],
"require": {
"php": "^7.3 || ^8.0",
"php-http/client-common": "^2.2.1",
"php-http/discovery": "^1.9.1",
"php-http/multipart-stream-builder": "^1.1.2",
"psr/http-client": "^1.0.1",
"webmozart/assert": "^1.9.1"
},
"require-dev": {
"nyholm/nsa": "^1.2.1",
"nyholm/psr7": "^1.3.1",
"phpunit/phpunit": "^9.3",
"guzzlehttp/guzzle": "^7.0"
},
"suggest": {
"nyholm/psr7": "PSR-7 message implementation",
"php-http/guzzle7-adapter": "HTTP client"
},
"autoload": {
"psr-4": {
"Mailgun\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mailgun\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
}
}
How do I fix that error or which is the correct way of changing http client in this version of Mailgun.
You try to set the client in the create
method, but the method signature expects a url as the second parameter
public static function create(string $apiKey, string $endpoint = 'https://api.mailgun.net'): self
use the HttpClientConfigurator->setHttpClient()
instead which gets injected to the contructor of the MailGun
class
public function __construct(
HttpClientConfigurator $configurator,
Hydrator $hydrator = null,
RequestBuilder $requestBuilder = null
) {
$this->requestBuilder = $requestBuilder ?: new RequestBuilder();
$this->hydrator = $hydrator ?: new ModelHydrator();
$this->httpClient = $configurator->createConfiguredClient();
$this->apiKey = $configurator->getApiKey();
$this->responseHistory = $configurator->getResponseHistory();
}
For example
$configurator = new HttpClientConfigurator();
$configurator->setHttpClient(new YourClientImplementation());
$configurator->setApiKey('your api key');
$mg = new Mailgun($configurator);
// Now, compose and send your message.
// $mg->messages()->send($domain, $params);
$mg->messages()->send('example.com', [
'from' => '[email protected]',
'to' => '[email protected]',
'subject' => 'The PHP SDK is awesome!',
'text' => 'It is so simple to send a message.'
]);