sendinblue / APIv3-nodejs-library

SendinBlue's API v3 Node.js Library
ISC License
102 stars 47 forks source link

TransactionalEmailsApi.sendTemplate doesn't passes attributes to template #112

Closed yamalight closed 1 year ago

yamalight commented 2 years ago

I've tried sending a templated email and it seems like TransactionalEmailsApi.sendTemplate currently doesn't handle attributes passed to it.

Template was pretty simple:

Name: {{ params.name }}
Test: {{ params.test }}

Following code results in empty values:

  const sibEmails = new SibApiV3Sdk.TransactionalEmailsApi();

  const sendEmail = new SibApiV3Sdk.SendEmail();
  sendEmail.emailTo = [email];
  sendEmail.attributes = {name: "Test Name", test: "123"};

  const data = await sibEmails.sendTemplate(templateId, sendEmail);
  console.log('API called successfully. Returned data: ', data);
  // This results in an email with template without any values filled

I also want to note that official docs for sendTemplate API method do not include attributes, params or any other props - does server just not process this?

The solution that worked for me was to use TransactionalEmailsApi.sendTransacEmail instead while passing templateId, i.e.:

  const sibEmails = new SibApiV3Sdk.TransactionalEmailsApi();

  const emailData = new SibApiV3Sdk.SendSmtpEmail();
  emailData.to = [{ email }];
  emailData.templateId = templateId;
  emailData.params = {name: "Test Name", test: "123"};

  const data = await sibEmails.sendTransacEmail(emailData);
  console.log('API called successfully. Returned data: ', data);
  // This results in an email with template with all values filled correctly

Am I misunderstanding the use of sendTemplate or is it currently broken?

shubhamUpadhyayInBlue commented 1 year ago

Hi @yamalight

Please use the following code and it should work fine:

let defaultClient = SibApiV3Sdk.ApiClient.instance;

let apiKey = defaultClient.authentications['api-key'];

apiKey.apiKey = 'YOUR_API_KEY';

let apiInstance = new SibApiV3Sdk.TransactionalEmailsApi();

let sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail();

sendSmtpEmail.subject = "My {{params.subject}}";
sendSmtpEmail.sender = {"name":"John Doe","email":"shubham.upadhyay@sendinblue.com"};
sendSmtpEmail.to = [{"email":"shubham.upadhyay@sendinblue.com","name":"Jane Doe"}];

sendSmtpEmail.params= {
  firstname: "John",
  lastname: "Doe"
}

sendSmtpEmail.templateId = 54;

apiInstance.sendTransacEmail(sendSmtpEmail).then(function(data) {
  console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}, function(error) {
  console.error(error);
});

Kindly remember to replace the placeholder i.e. YOUR_API_KEY.

Also, the template 54 HTML content is as following:

<html><head></head><body><h1>

Hello! This is production test for the template 54. 
firstname: {{params.firstname}}, lastname: {{params.lastname}}

</h1></body></html>
yamalight commented 1 year ago

@shubhamUpadhyayInBlue I'm no longer involved with the client who used your SDK (and I think they've ended up using different provider) so cannot test the code, but if it works - feel free to close the issue.