I get the following error when trying to implement near direct copy of the
Array
(
[0] => HTTP/1.1 502 Bad Gateway
[1] => Server: nginx
[2] => Date: Fri, 07 Sep 2018 00:45:53 GMT
[3] => Content-Type: text/html
[4] => Content-Length: 166
[5] => Connection: keep-alive
[6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io
[7] => Access-Control-Allow-Methods: POST
[8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
[9] => Access-Control-Max-Age: 600
[10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
[11] =>
[12] =>
)
Copied the example:
//-----------------------------------------------------------
// Build Message
//-----------------------------------------------------------
$email = new \SendGrid\Mail\Mail();
$email->setFrom($this->msg_from[0], $this->msg_from[1]);
$email->setSubject($this->msg_subject);
foreach ($this->msg_from as $tmp_msg_to) {
$email->addTo($tmp_msg_to[0], $tmp_msg_to[1]);
}
if ($this->msg_body) {
$email->addContent("text/plain", $this->msg_body);
}
if ($this->msg_html_body) {
$email->addContent("text/html", $this->msg_html_body);
}
//-----------------------------------------------------------
// Send Message
//-----------------------------------------------------------
$sendgrid = new \SendGrid($this->api_key);
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
}
catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage() ."\n";
}
Hello @cclark61,
It looks like there may be a communication issue between your server and ours. Could you please try running a cURL request from the command line of your server to verify?
With Best Regards,
Elmer
I'm getting the exact same issue. I made sure my API key has all the privileges.
I did a manual request using Postman, tried one with Curl as well. I dumped everything straight from the Client::makeRequest() method and replicated all the variables. Same result.
Request Headers
Content-Type: application/json
Authorization: Bearer <My API Key>
User-Agent: sendgrid/7.2.0;php
Accept: application/json
Raw JSON Payload of the request
{
"personalizations": [
null
],
"from": {
"name": "Some Name",
"email": "[email protected]"
},
"subject": "Some Subject",
"content": [
{
"type": "text\/html",
"value": "<html><body>Hello world!</body></html>"
}
]
}
Technical Details
After doing a little bit of digging in the issues I found that apparently what's causing this is a null personalizations property in the body of the request.
This comment by @koenreiniers may be able to help you as well @cclark61
https://github.com/sendgrid/sendgrid-php/issues/650
This is terrible behavior for the record @thinkingserious because the Hello Email example in the docs will produce this behavior. Please fix the docs and make a note about needing at least one personalization to be able to send an email successfully.
Thanks @l0gicgate. It seems #686 is the same issue.
Thanks for all the great feedback everyone!
Note to me: the problem is likely here.
I had the same error, for me it was caused by setting an empty Content.
$fromEmail = '[email protected]';
$fromName = 'Test A';
$toEmail = '[email protected]';
$toName = 'Test B';
$subject = 'Amazing subject name';
$message = '';
$from = new SendGrid\Mail\From($fromEmail, $fromName);
$to = new SendGrid\Mail\To($toEmail, $toName);
$content = new SendGrid\Mail\Content('text/html', $message);
$this->mail = new SendGrid\Mail\Mail(
$from,
$to,
$subject, // subject
$content
);
// Execute other magic code here
$message was '' for me. which caused the issue. I do add a second content to the mail, which is filled but it doesn't like empty content I guess. I am upgrading from the 6 .0 version to the latest 7 version, which causes these issues to appear.
Thanks for the additional feedback @Yihka!
Most helpful comment
I had the same error, for me it was caused by setting an empty Content.
$message was '' for me. which caused the issue. I do add a second content to the mail, which is filled but it doesn't like empty content I guess. I am upgrading from the 6 .0 version to the latest 7 version, which causes these issues to appear.