Sendgrid-php: 502 bad gateway

Created on 27 Jul 2018  路  9Comments  路  Source: sendgrid/sendgrid-php

Issue Summary

I get a 502 bad getway reponse

What's wrong with my code ?

$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";
}

Technical details:

  • sendgrid-php Version: master
  • PHP Version: 7.1 (as of May 17, 2018)
unknown or a help wanted question

Most helpful comment

@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;
    }
}

All 9 comments

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 !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FilipLukac picture FilipLukac  路  4Comments

peluprvi picture peluprvi  路  5Comments

rsalunga29 picture rsalunga29  路  4Comments

izhukovich picture izhukovich  路  4Comments

rainman0607 picture rainman0607  路  4Comments