sendinblue / APIv3-nodejs-library

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

custom fields in transaction emails #41

Closed brajeshawasthi closed 5 years ago

brajeshawasthi commented 6 years ago

how to add custom fields in transaction emails?

Vadorequest commented 6 years ago

Same question... the api v2 supports attr option, see https://apidocs.sendinblue.com/template/#1

For v3, there is a similar option but I don't see it here

Vadorequest commented 6 years ago

@ekta-slit ping :)

Vadorequest commented 6 years ago

The docs are incomplete, there is no full example on how to use attributes when sending an email.

https://github.com/sendinblue/APIv3-nodejs-library/blob/master/docs/SMTPApi.md#sendTemplate

var sendEmail = new SibApiV3Sdk.SendEmail(); // SendEmail | 

This doesn't show how to pass the parameters. I tried looking at the source code:

https://github.com/sendinblue/APIv3-nodejs-library/blob/17755930d14b3ff954f79a11600fbd4ac49ff5e0/src/api/SMTPApi.js#L559 https://github.com/sendinblue/APIv3-nodejs-library/blob/17755930d14b3ff954f79a11600fbd4ac49ff5e0/src/api/SMTPApi.js#L514 https://github.com/sendinblue/APIv3-nodejs-library/blob/17755930d14b3ff954f79a11600fbd4ac49ff5e0/src/ApiClient.js#L371

And I figured out the following:

const defaultClient = SibApiV3Sdk.ApiClient.instance;

  // Configure API key authorization: api-key
  const apiKey = defaultClient.authentications['api-key'];
  apiKey.apiKey = process.env.SIB_API_KEY;
  // Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
  //apiKey.apiKeyPrefix = 'Token';

  const apiInstance = new SibApiV3Sdk.SMTPApi();

  const templateId = 107; // Number | Id of the template

  const emailConfig = new SibApiV3Sdk.SendEmail.constructFromObject({
      emailTo: ['test@example.com'],
      attributes: {
        MESSAGE: 'test message',
      },
    },
  ); // SendTestEmail |
  console.log('emailConfig', emailConfig)

  apiInstance.sendTemplate(templateId, emailConfig).then(function () {
    res.send({ success: 'true' });
  }, function (error) {
    console.error(error);
  });

The console.log displays the following:

emailConfig exports {
  emailTo: [ 'test@example.com' ],
  attributes: { MESSAGE: 'test message' } }

I get the email in my inbox, but the variables aren't replaced as I'd expect: image

I added additional logging at https://github.com/sendinblue/APIv3-nodejs-library/blob/17755930d14b3ff954f79a11600fbd4ac49ff5e0/src/ApiClient.js#L426 and I'm sure my attributes are properly provided, but they don't seem to be applied.

Vadorequest commented 5 years ago

@bawasthi Using node.js, here is my working implementation

const data = {
  to: [
    {
      name: 'Contact',
      email: '',
    }
  ],
  params: {
    MESSAGE: 'Essai 42',
    FIRST_NAME: '',
    LAST_NAME: '',
    EMAIL: ''
  },
  replyTo: {
    email: '',
    name: '',
  },
  templateId: 111,
  tags: ['contact-form']
};
console.log('--data--', JSON.stringify(data, null, 2));

app.get('/sendEmail', (req, res) => {
  var request = require('request');

  var options = {
    method: 'POST',
    headers: {
      'api-key': process.env.SIB_API_KEY,
      'Content-Type': 'application/json',
    },
    url: 'https://api.sendinblue.com/v3/smtp/email',
    body: data,
    json: true,
  };

  request(options, function (error, response, body) {
    if (error) {
      throw new Error(error);
    }

    // console.log('--response--', response);
    console.log('--response body--', body);
    res.json({
      status: true
    })
  });
});
ekta-slit commented 5 years ago

Hi @Vadorequest

{MESSAGE} is written incorrect to replace a variable.

Use this api https://github.com/sendinblue/APIv3-nodejs-library/blob/master/docs/SMTPApi.md#sendTransacEmail by passing templateId and params to get attributes replaced.

refer: https://help.sendinblue.com/hc/en-us/articles/360000268730-SendinBlue-s-New-Template-Language-to-create-email-templates-and-campaigns-NEW- api doc: https://developers.sendinblue.com/v3.0/reference#sendtransacemail

Thanks

agencelarsen commented 1 year ago

Hi, if anyone using Sendinblue/Brevo with WooCommerce has succeeded syncing custom fields in real time between both, I'm really interested. I am using "Points and Rewards" and I would like Brevo/Sendinblue WC plugin to import the "user points balance" and "points balance value" fields that I have to calculate using a custom PHP function but I don't know how to hook such a function and map it with Brevo/SendInblue using the API.