sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 623 forks source link

Dynamic Template, substitution bug #1031

Closed jshampur closed 3 years ago

jshampur commented 3 years ago

Issue Summary

Mixing a numeric/string variables in specific order in the personalization variable array results in an exception:

Code Snippet

$from = New From($fromEmail, $fromName);
$tos = [ New To($email, $name, $templateValues) ];
$emailObj = new Mail($from, $tos); // **This triggers the exception - see below**
....

if the $templateValues array is set like this:

$templateValues = [ "first_name" => "jay", "last_name" => "smith", "amount" => 20, "album" => "dark side of the moon" ]

The exception is triggered.

if I change the order to $templateValues = [ "first_name" => "jay", "last_name" => "smith", "album" => "dark side of the moon" , "amount" => 20, ]

it works. Just to be safe, I actually made "amount" =>"20" which works regardless of where it appears in the list.

The exception was not caught as I was following examples from the Sendgrid docs and did not wrap the code in try...catch block.

Exception/Log

Uncaught SendGrid\Mail\TypeException: $value must be of type string, array or object. in /var/www/html/api
/sendgrid-php/lib/mail/Substitution.php:88 

Technical details:

Took me a while to figure this out.

shwetha-manvinkurke commented 3 years ago

Hi @jshampur,

v7.3.0 is pretty outdated. The latest version is 7.9.1. I recommend you try with the latest version and see if you still face the same issue.

jshampur commented 3 years ago

Thanks - I'll give it a go.