Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addSubstitution() fails when replaced with an integer #339

Closed
CyberPunkCodes opened this issue Dec 27, 2016 · 3 comments
Closed

addSubstitution() fails when replaced with an integer #339

CyberPunkCodes opened this issue Dec 27, 2016 · 3 comments
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@CyberPunkCodes
Copy link

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:

// 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
@wittfabian
Copy link

Why do you use "%%id%%" instead of "%id%"?
I am not sure, but I think the documentation provides an example with the "%id%" syntax.

@thinkingserious
Copy link
Contributor

@WadeShuler,

Looks like you have been bitten by a variant of this bug: #337

I'll make sure that we cover all such cases when we push the fix. Thanks!

@thinkingserious thinkingserious added status: help wanted requesting help from the community type: bug bug in the library labels Apr 28, 2017
@thinkingserious
Copy link
Contributor

This has been moved here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

3 participants