sendgrid-php: SendGrid\\Client::makeRequest(): Argument #3 ($body) must be of type ?array, SendGrid\\Mail\\Mail give

PHP 8.4

SendGrid 8.1.11

I get this error message when sending an email:

SendGrid\Client::makeRequest(): Argument #3 ($body) must be of type ?array, SendGrid\Mail\Mail give called in /var/www/html/my-project/vendor/sendgrid/php-http-client/lib/Client.php on line 662

My code:


 $fromEmail = "info@example.com";
        $fromName = "Example Solutions";

        $email = new Mail();
        $email->setFrom($fromEmail, $fromName);
        $email->addTo($Email, $Name);
        $email->setSubject("Subject");
        $email->addContent("text/plain", "Find attached the spec sheet.");
        $email->addContent(
            "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
        );

        
        $sendGrid = new \SendGrid($_ENV['SENDGRID_API_KEY']);

        $sendGridResponse = $sendGrid->send($email);
        if ($sendGridResponse->statusCode() === 202) {
         
        } else {
            throw new Exception("Caught exception: " . $sendGridResponse->body());
        }

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 6
  • Comments: 18 (4 by maintainers)

Most upvoted comments

I haven’t had the chance to check #1123. For now, I have temporarily forced sendgrid/php-http-client to 4.1.1 via composer on my repo to get around the issue until it gets patched.

composer require sendgrid/php-http-client "4.1.1"

From what I can tell, the issue is just a declaration problem in the makeRequest() method (see my previous notes). After rolling back to 4.1.1 everything appears to be working as expected.

Edit: just for refererence, I’m pretty sure this issue was triggered by the following commit (see line 511): https://github.com/sendgrid/php-http-client/commit/c8598582d2c136ec11b4e487c7bd25145c80a9b1

For those still mentioning their version of sendgrid/sendgrid, this is not the package which is actually causing the issue.

The workaround is specifically adding "sendgrid/php-http-client": "4.1.1" to the require section of composer.json, just make sure to remove it in a week or so when SendGrid has pushed the fix.

Its nice to see the SendGrid PHP libraries getting some much needed work, but its concerning such a serious breaking bug got through and had to be reported by end users.

I think we would like to figure out better ways to get the deprecation out as well as not breaking anything. So, will create a separate PR for new changes

I have raised a PR to fix this. Will get this merged and release the new version of php-http-client that should solve this issue. Thanks!

After digging through https://github.com/sendgrid/php-http-client repo, it appears this should be handled over there since the affected code is not in this repo: https://github.com/sendgrid/php-http-client/issues/170.

The error appears to be resulting from line 511 in SendGrid\Client::makeRequest()

public function makeRequest($method, $url, ?array $body = null, ?array $headers = null, $retryOnLimit = false)

probably should be:

public function makeRequest($method, $url, \SendGrid\Mail\Mail|array|null $body = null, ?array $headers = null, $retryOnLimit = false)

Note: the docblock above it is outdated as well on line 503

Can confirm. I am receiving the same error response when attempting to send messages. This was working on previous releases. Full error message below.

SendGrid\Client::makeRequest(): Argument #3 ($body) must be of type ?array, SendGrid\Mail\Mail given, called in /app/vendor/sendgrid/php-http-client/lib/Client.php on line 662