sendinblue / APIv3-nodejs-library

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

Bad request when setting 'content' of SendSmtpEmailAttachment #58

Closed thibautvdu closed 5 years ago

thibautvdu commented 5 years ago

Hello everyone,

The present documentation for SendSmtpEmailAttachment (used in SendTransacEmail) says the 'content' variable should be set to a 'Blob'. There is no such type in Node.js, so I assumed it was a base64 string, but the api answer with a Bad request error (working fine if I use url instead of content). Related code :


 const pdfBuffer = await generatePdf(statementHash);
  const blob = pdfBuffer.toString('base64');

  var apiInstance = new SibApiV3Sdk.SMTPApi();
  var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail(); // SendSmtpEmail | Values to send a transactional email
  var attachment = new SibApiV3Sdk.SendSmtpEmailAttachment();
  attachment.name = "crypto-constat.pdf";
  attachment.content = blob;
  //attachment.url = "https://docs.n26.com/legal/04+FR/06+Black/fr/03_1black-allianz-insurance-tncs-Nov16-Sept17-fr.pdf";
  var recipient = new SibApiV3Sdk.SendSmtpEmailTo();
  recipient.name = "Thibaut Dumont";
  recipient.email = "thibautvdumont@gmail.com";
  sendSmtpEmail.templateId = 1;
  sendSmtpEmail.attachment = [attachment];
  sendSmtpEmail.to = [recipient];

  var data = await apiInstance.sendTransacEmail(sendSmtpEmail);

pdfBuffer is a buffer object. Did anyone successfully send content on the fly with this API ? Any help would be welcomed !

thibautvdu commented 5 years ago

Update for future reference, that seems to be a limitation with the REST api itself, when using a templateId and the content parameter. Here is the error output using the REST api :

  "code": "invalid_parameter",
  "message": "Please don't pass attachment content & templateId in same request, instead use attachment url only"

That's quite inconvenient though as those are two very different things...

ekta-slit commented 5 years ago

Hi @thibautvdu

We do not allow attachment generated on the fly (attachment.content & attachment.name) and templateId together for now, but its a planned improvement for 2nd quarter of this year. Currently, with templateId attachment url (attachment.url) is expected. Attachment generated on the fly id expected with htmlContent or textContent

Thanks

fprevost commented 5 years ago

Hey,

As a workaround, you can use the method getSmtpTemplate of the SMTPApi to get template's description (replyTo, subject, sender, htmlContent, ...) and then use these informations to initialise an email and fill its attachment property wit Base64 encoding.

thibautvdu commented 5 years ago

@fprevost Thanks for the workaround !

sonoogenesys commented 3 years ago

@fprevost Thanks for the workaround !

Can you please help me how you it solved