How to send dynamic template mail via guzzle HTTP in PHP

I’m trying to send an email via Dynamic template in SendGrid using Guzzle HTTP in PHP.

But I was not able to send mail. As I get only the error like below without any reason in it.

Type: GuzzleHttpExceptionClientException

Message: Client error: `POST https://api.sendgrid.com/v3/mail/send` resulted in a `400 Bad Request` response: {"errors":[{"message":"Bad Request","field":null,"help":null}]}

My PHP example code:

require __DIR__.'../../vendor/autoload.php';
$CLIENT = new GuzzleHttpClient();

$response = $CLIENT->request('POST', 'https://api.sendgrid.com/v3/mail/send', [
    "headers" => [
        "Authorization" => "Bearer my-api-key",
        "Content-Type" => "application/json"
    ],
    'data' => '{
      "from": {
        "email": "[email protected]"
      },
      "personalizations": [{
        "to": [{
          "email": "[email protected]"
        }],
        "dynamic_template_data": {
          "email_data": [{
            "id": "2",
            "title": "Artificial Intelligence in Health Care",
            "image": "https://example.com//uploads/3663581583995181_0724.jpg",
            "description": "Immediate application of AI in the Health Care domains."
          }, {
            "id": "199",
            "title": "Aesthetics Skill Discussion 3 by Jranand",
            "image": "",
            "description": "Aesthetics Skill Discussion 3 by Jranand"
          }]
        }
      }],
      "template_id": "my-template-id"
    }',
]);

echo $response->getStatusCode();

I was able to send an email via the dynamic template test method in SendGrid with the same dynamic_template_data. But trying this with Guzzle HTTP. I m not able to find the reason for the error.

Able to send an email with dynamic JSON data in the testing.

test method

Can anyone help me out to solve the problem?

Thanks in advance.

Answer

There is no data request option in json, you need to change your code either you can use json request option from guzzle(will need lesser effort) or you can directly right your body.

try{
  
  $CLIENT = new GuzzleHttpClient();

$data = [
        "from" => [
                "email" => "[email protected]"
            ],
        "personalizations" => [
                [
                    "to" =>  [
                        [
                          "email" => "[email protected]"
                        ]    
                    ],
                    "dynamic_template_data" => [
                        "email_data" => [
                            [
                                "id" => "2",
                                "title" => "Artificial Intelligence in Health Care",
                                "image" => "https://example.com//uploads/3663581583995181_0724.jpg",
                                "description"=> "Immediate application of AI in the Health Care domains." 
                            ],
                            [
                                "id" => "199",
                                "title" => "Aesthetics Skill Discussion 3 by Jranand",
                                "image" => "",
                                "description" => "Aesthetics Skill Discussion 3 by Jranand"
                            ]
                        ]
                    ]
                ]
            ],
        "template_id" => "my-template-id"
            
    ];
$response = $CLIENT->request('POST', 'https://api.sendgrid.com/v3/mail/send', [
    "headers" => [
        "Authorization" => "Bearer my-api-key",
        "Content-Type" => "application/json"
    ],
    'json'  => $data,
]);

    if ($guzzleResponse->getStatusCode() == 200) {
         $response = json_decode($guzzleResponse->getBody(),true);
         //perform your action with $response 
    } 
}
catch(GuzzleHttpExceptionRequestException $e){
   // you can catch here 400 response errors and 500 response errors and log those errors
   
}catch(Exception $e){
   //other errors 
}