sendinblue / APIv3-nodejs-library

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

'content' is not valid in attachment #144

Closed Kinzi closed 1 year ago

Kinzi commented 1 year ago

I need to send a transactional email with 2 files. One is a url the other a csv string.

This is my code:

sendSmtpEmail.attachment = [{
          content: csvString.toString("base64"),
          name: "file.csv"
        }, { url: fileURL, name: "invoice.pdf" }]

Based on this: https://github.com/sendinblue/APIv3-nodejs-library/blob/master/docs/SendSmtpEmailAttachment.md

I get the following error:

{"code":"invalid_parameter","message":"'content' is not valid in attachment"}

If I only use the url attachment, everything works.

Is this a bug or am I doing anything wrong?

shubhamUpadhyayInBlue commented 1 year ago

Hi @Kinzi I will check.

shubhamUpadhyayInBlue commented 1 year ago

Hi @Kinzi Please try this out it works well for me. Don't forget to replace your API key in the script.

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.htmlContent = "<html><body><h1>This is my first transactional email {{params.parameter}}</h1></body></html>";
sendSmtpEmail.sender = { "name": "shubham", "email": "shubham.upadhyay@sendinblue.com" };
sendSmtpEmail.to = [
  { "email": "shubham.upadhyay@sendinblue.com", "name": "Ram shyam" }
];
sendSmtpEmail.replyTo = { "email": "shubham.upadhyay@sendinblue.com", "name": "shubham upadhyay" };
sendSmtpEmail.headers = { "Some-Custom-Name": "unique-id-1234" };
sendSmtpEmail.params = { "parameter": "My param value", "subject": "New Subject" };
sendSmtpEmail.attachment = [
  {
    "name": "somename.csv",
    "content": "Sm9obixEb2UsMTIwIGplZmZlcnNvbiBzdC4sUml2ZXJzaWRlLCBOSiwgMDgwNzUKSmFjayxNY0dpbm5pcywyMjAgaG9ibyBBdi4sUGhpbGEsIFBBLDA5MTE5CiJKb2huICIiRGEgTWFuIiIiLFJlcGljaSwxMjAgSmVmZmVyc29uIFN0LixSaXZlcnNpZGUsIE5KLDA4MDc1ClN0ZXBoZW4sVHlsZXIsIjc0NTIgVGVycmFjZSAiIkF0IHRoZSBQbGF6YSIiIHJvYWQiLFNvbWVUb3duLFNELCA5MTIzNAosQmxhbmttYW4sLFNvbWVUb3duLCBTRCwgMDAyOTgKIkpvYW4gIiJ0aGUgYm9uZSIiLCBBbm5lIixKZXQsIjl0aCwgYXQgVGVycmFjZSBwbGMiLERlc2VydCBDaXR5LENPLDAwMTIz"
  },
  {
    "url": "https://www.africau.edu/images/default/sample.pdf",
    "name": "sample.pdf"
  }
]

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

Thanks!

Kinzi commented 1 year ago

@shubhamUpadhyayInBlue thanks will check today! But the only difference I see is that you pass an encoded string instead of a blob I guess? How did you create the base64 string exactly? Maybe that's the problem?

Kinzi commented 1 year ago

Was able to get it to work using btoa

const btoa = require("btoa");

sendSmtpEmail.attachment = [{
          content: btoa(csvString),
          name: "mydata.csv"
        }]
shubhamUpadhyayInBlue commented 1 year ago

Okay so the issue was with conversion to base64. Shall I close the issue now?

Kinzi commented 1 year ago

@shubhamUpadhyayInBlue For some reason the pdf is now send as an empty file. But only when the csv is also attached). If I only send the pdf it works.

Are you sure your example works with both files?

Kinzi commented 1 year ago

@shubhamUpadhyayInBlue So basically:

This works:

const attachThis = [
      // {
      //   content: btoa(attachments["csv"]),
      //   name: "bookings.csv"
      // },
      { url: attachments["invoice"], name: "invoice.pdf" }
    ];
    sendSmtpEmail.attachment = attachThis;

This does NOT work:

const attachThis = [
      {
        content: btoa(attachments["csv"]),
        name: "bookings.csv"
       },
      { url: attachments["invoice"], name: "invoice.pdf" }
    ];
    sendSmtpEmail.attachment = attachThis;

The 2nd one will attach a working csv and a pdf, but the pdf is broken (has no information).

Do you want me to make a new issue for this?

Kinzi commented 1 year ago

It also works if I add the pdf twice or the csv twice. Only the combination breaks the pdf.

Kinzi commented 1 year ago

My workaround now is to locally fetch the file and pass the pdf also as a base64 string.

However, I would consider this a bug and the workaround quite annoying and inelegant.