'SendGrid email substitutions does not work for Gmail emails

I am sending emails using SendGrid API and I am using Dynamic Email templates.

My email template looks like this.

enter image description here

Emails are sent to both Gmail and to emails created using my domain such as [email protected].

And I am using email substitutions to replace the {{substitutables}} in the template. The substitutions seem to work for emails from my domain but not for Gmail emails.

Here is what Gmail email looks like. I am using web browser for my Gmail emails. enter image description here

And here is what my domain emails look like. I am using MS Outlook as my mail client for domain emails. enter image description here

Following is the code that I am using to send emails.

public function sendEmail(
    string $templateId,
    array $recipiants,
    array $substitutions = [],
    array $ccs = [],
    array $bccs = [],
    array $replyTo = [],
    array $attachments = []
){

    $emailParams = $this->params->get('email');

    $apiKey = $emailParams['sendgrid']['api_key'];

    $sendgrid = new \SendGrid($apiKey);

    $email = new Mail();

    if (!$recipiants || !is_array($recipiants) || count($recipiants) == 0){
        throw new NotFoundHttpException("Email Recipients Not Found!");
    }

    $email->setTemplateId($templateId);
    $email->setFrom(new From($emailParams['from']['email'], $emailParams['from']['name']));

    // adding recients to the emial
    foreach ($recipiants as $recipiant){
        $personalization = new Personalization();
        $toEmail = new To($recipiant['email'], $recipiant['name']);
        $personalization->addTo($toEmail);
        $email->addPersonalization($personalization);
    }

    // adding CCs to email
    if (count($ccs) > 0) {
        foreach ($ccs as $cc) {
            $ccEmail = new Cc($cc['email'], $cc['name']);
            $email->addCc($ccEmail);
        }
    }

    // adding BCCs to email
    if (count($ccs) > 0) {
        foreach ($bccs as $bcc) {
            $bccEmail = new Bcc($bcc['email'], $bcc['name']);
            $email->addBcc($bccEmail);
        }
    }

    // adding template substitutions
    if (count($substitutions) > 0) {
        $email->addDynamicTemplateDatas($substitutions);
    }

    // sending email
    /** @var Response $response */
    $response = $sendgrid->send($email);

    if ($response->statusCode() != 202){
        throw new AccessDeniedHttpException("Failed to Send Email!");
    }
}

Can someone point me what I am doing wrong and why it is behaving this way? Thank you for your time.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source