I get a 502 bad getway reponse
$sendgrid = new SendGrid(config('services.sendgrid.key'));
$mail = new Mail();
$mail->setFrom('[email protected]', 'AppName');
$mail->setTemplateId("template-id");
foreach ($invitations as $invit) {
$pers = new Personalization();
$pers->addTo(new To($invit->email, $invit->fullname));
$pers->setSubject(new Subject("Some subject heere"));
$pers->addSubstitution("-username-", $invit->fullname);
$pers->addSubstitution("-link-", "https://domain.com/" . $invit->slug . "?invit=" . $invit->uuid);
$mail->addPersonalization($pers);
}
try {
$response = $sendgrid->send($mail);
dd($response);
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
What does dd() do? It seems to be an issue over at Sendgrid since 502 is not a response it should return in normal cases
@martijnmelchers dd() is just a wrapper for var_dump(). So i should send a support message to sendgrid ?
Is the error still happening?
If so, the error is described here as an issue at Sendgrid. You should try to email them
Ok @martijnmelchers, i just issued a support request, waiting for their answer
Cool :)
@yadounis I had the same issue. I fixed it by inserting an instance of From immediately in the constructor. This prevents an empty instance of Personalization from being added, which causes the 502 bad gateway error. So, in your case:
new Mail(new From('[email protected], 'AppName'));
Also: You will probably run into the "Substitutions may not be used with dynamic templating" error next, which I fixed by creating my own Personalization class:
class CustomPersonalization extends Personalization
{
/**
* @var array
*/
protected $dynamicTemplateData = [];
public function setDynamicTemplateData($key, $value)
{
$this->dynamicTemplateData[$key] = $value;
}
public function getDynamicTemplateData()
{
return $this->dynamicTemplateData;
}
/**
* Return an array representing a Personalization object for the SendGrid API
*
* @return null|array
*/
public function jsonSerialize()
{
return array_filter(
[
'to' => $this->getTos(),
'cc' => $this->getCcs(),
'bcc' => $this->getBccs(),
'subject' => $this->getSubject(),
'headers' => $this->getHeaders(),
'custom_args' => $this->getCustomArgs(),
'send_at' => $this->getSendAt(),
'substitutions' => $this->getSubstitutions(),
'dynamic_template_data' => $this->getDynamicTemplateData(),
],
function ($value) {
return $value !== null;
}
) ?: null;
}
}
@yadounis,
What is your support ticket number?
@martijnmelchers, @koenreiniers,
Thanks for helping out!
@koenreiniers You saved me !
Most helpful comment
@yadounis I had the same issue. I fixed it by inserting an instance of
Fromimmediately in the constructor. This prevents an empty instance ofPersonalizationfrom being added, which causes the 502 bad gateway error. So, in your case:new Mail(new From('[email protected], 'AppName'));Also: You will probably run into the "Substitutions may not be used with dynamic templating" error next, which I fixed by creating my own
Personalizationclass: