You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When replacing a tag with the user's ID, the email failed to send. I narrowed it down to being due to passing the user's ID as an integer.
Example:
// simulate from SQL query
$users = [
['id' => 0, 'name' => 'Joe Smith', 'email' => 'joesmith@example.com'],
['id' => 1, 'name' => 'Jane Doe', 'email' => 'janedoe@example.com']
];
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
foreach ( $users as $user )
{
$mail = new \SendGrid\Mail();
$from = new SendGrid\Email(FROM_NAME, FROM_EMAIL);
$mail->setFrom($from);
$mail->setSubject('Testing out SendGrid');
$content = new \SendGrid\Content('text/plain', 'Hello, your user id is %%id%%.');
$mail->addContent($content);
$email = new \SendGrid\Email(null, $user['email']);
$personalization = new \SendGrid\Personalization();
$personalization->addTo($email);
$personalization->addSubstitution("%%id%%", $user['id']); // <-- ERROR HERE
$mail->addPersonalization($personalization);
$response = $sg->client->mail()->send()->post($mail);
}
I actually have my code generating batches in 500 emails per batch. The above is a simplified example of what I have.
if I replace $personalization->addSubstitution("%%id%%", $user['id']);
with $personalization->addSubstitution("%%id%%", (string) $user['id']);
it magically works.
Alternatively, I force the user ID to a string at the top when building the $users array, but the above shows off the bug better ;)
Technical details:
sendgrid-php Version: master
PHP Version: 5.6
The text was updated successfully, but these errors were encountered:
Issue Summary
When replacing a tag with the user's ID, the email failed to send. I narrowed it down to being due to passing the user's ID as an integer.
Example:
I actually have my code generating batches in 500 emails per batch. The above is a simplified example of what I have.
if I replace
$personalization->addSubstitution("%%id%%", $user['id']);
with
$personalization->addSubstitution("%%id%%", (string) $user['id']);
it magically works.
Alternatively, I force the user ID to a string at the top when building the
$users
array, but the above shows off the bug better ;)Technical details:
The text was updated successfully, but these errors were encountered: