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

Template substitutions not being applied after the first email #243

Closed
jasonpang opened this issue Jun 17, 2016 · 5 comments
Closed

Template substitutions not being applied after the first email #243

jasonpang opened this issue Jun 17, 2016 · 5 comments
Labels
status: help wanted requesting help from the community type: question question directed at the library

Comments

@jasonpang
Copy link

jasonpang commented Jun 17, 2016

Referencing this issue, there still seems to be an issue when applying templates to an email. Rarely, one of my emails will have the templates applied (the first one I send after a while), but subsequent emails have the placeholder blank.

Here's the code I'm using. Is there something I'm doing wrong?

 var helper = sendgrid.mail;
 var from_email = new helper.Email("no-reply@site.com");
 var to_email = new helper.Email("recipient@gmail.com");
 var content = new helper.Content("text/html", "This is some content.");
 var mail = new helper.Mail(from_email, 'Subject', to_email, content);

 var username = "username";
 var email = "recipient@gmail.com";

 var personalization = new helper.Personalization();
 personalization.addTo(to_email);
 personalization.addSubstitution(new helper.Substitution("%username%", username));
 personalization.addSubstitution(new helper.Substitution("%email%", email));
 personalization.addSubstitution(new helper.Substitution("%confirmation_url%", `http://localhost/confirm`));
 mail.setTemplateId("2245cf86-6587-46c0-a384-6d8109491ee3");
 mail.addPersonalization(personalization);

 var sg = sendgrid.SendGrid(apiKey);
 var requestBody = mail.toJSON();
 var request = sg.emptyRequest();
 request.method = 'POST';
 request.path = '/v3/mail/send';
 request.body = requestBody;
 return await new Promise(resolve => {
     sg.API(request, function(response) {
         console.log(response);
         resolve(response);
     });
 });

My goal is to send transactional emails using a SendGrid template and substituting variables.

Using version 3.0.5 (latest) of the sendgrid library.

@BrandesEric
Copy link

This is happening for me as well, using the v2 apis and 2.0.0 of the Node library. For me it occurs EVERY time there are multiple recipients. The first recipient gets the replacement as expected, but subsequent emails have blank spaces instead of the replacement text.

@thinkingserious
Copy link
Contributor

@jasonpang @BrandesEric This issue is similar to this one: sendgrid/sendgrid-java#114 -- please read the solution there.

I'll officially address this ticket and make the same updates to the documentation as soon as I can.

@BrandesEric
Copy link

@thinkingserious I'm not sure I understand - we're not doing anything with multiple personalization objects. We're just creating a sendgrid.Email() object and setting substitutions. In our case, we have two recipients, but we specify a single replacement substitution (that is, we want the same value to be replaced in both emails)

@thinkingserious
Copy link
Contributor

@BrandesEric Ah, sorry I missed that you are using v2 apis. Could you please open a separate ticket and include the relevant source code? Thanks!

@thinkingserious thinkingserious added type: question question directed at the library status: help wanted requesting help from the community labels Jun 28, 2016
@thinkingserious
Copy link
Contributor

@jasonpang,

The substitutions used to be an array that matched index for index with the recipients array. Now, each substitution goes to a personalization.

Previously, you would do the following:

to: email1, email2, email3
substitutions: name = sam, jane, mike

Now, you would do the following:

personalization1:

  • email: email1
  • substitution: name = sam

personalization2:

  • email: email2
  • substitution: name = jane

personalization3:

  • email: email3

etc..

For more on the changes between v2 and v3, please review: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/how_to_migrate_from_v2_to_v3_mail_send.html

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: question question directed at the library
Projects
None yet
Development

No branches or pull requests

3 participants