0
votes

I'm using sendgrid/sendgrid-php the repo on github to send transnational emails. Today I've updated the library, the new one uses the API v3 whereas I used v2 before. I've changed the code as per their examples, here is a dump of my SendGrid\Mail object:

enter image description here

The problem is that I'm constantly receiving the 400 BAD REQUEST error without any additional info:

enter image description here

What am I doing wrong? The mail object seems to be correct.

I'm trying to send the email the following way:

$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$request_body = [creating the mail object];
$response = $sg->client->mail()->send()->post($request_body);
1
Just to be clear, does [creating the mail object]; mean new SendGrid\Mail($from, $subject, $to, $content); ? Or are you doing without the Helper class? - Justin Steele
@JustinSteele I use the helper as per this example github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/…, see the kitchenSink function. I.e. I first create the mail object, then add emails, personalisations and attachments and invoke $sg->client->mail()->send()->post($mail); finally - super.t
I don't think the custom_args param takes a JSON array. I think it should be a JSON object. Like this: {"md_email_id":26921}. This might be an issue with the library. It could be useful to show how you're building the email object. - Sebastian-Laurenţiu Plesciuc

1 Answers

1
votes

I was having the same issue and discovered all personalizations need to be strings with double quotes. In your example:

$md_email_id = (string)26921

More info https://github.com/sendgrid/sendgrid-php/issues/264