3

I am trying to set up Sendinblue's mail service with Google Apps Script as follows:

code.gs

function sendInBlue(){

eval(UrlFetchApp.fetch('https://cdn.rawgit.com/mailin-api/mailin-api-node-js/master/V2.0/mailin.js').getContentText());

var client = new Mailin("https://api.sendinblue.com/v2.0","your access key");

data = {
"to" : {"to@example.net":"to whom!"},
"from" : ["from@email.com", "from email!"],
"subject" : "My subject",
"html" : "This is the <h1>HTML</h1>"
}

client.send_email(data).on('complete',function(data){console.log(data);});

}

Error message: ReferenceError: "require" is not defined.

The sendinblue node.js library talks about a Restler library also being required, but I'm really not sure how to incorporate this library also? I am a novice so could be completely off-track here.

Any guidance is appreciated.

Documentation:

https://apidocs.sendinblue.com/tutorial-sending-transactional-email/

https://github.com/mailin-api/mailin-api-node-js/tree/master/V2.0

Grokify
  • 15,092
  • 6
  • 60
  • 81
beano
  • 932
  • 1
  • 15
  • 28
  • The code that you provided does not have the word "require" in it. But the error you are getting is about "require" not being defined. Node.js uses the word "require" so I'm guessing that you are trying to use Node.js code in Apps Script. You need to look at the "Curl" documentation, not Node.js. Then you need to translate that into what the HTTPS Request would be for the `UrlFetchApp.fetch(url)` code in Apps Script would be. You are a long way away from getting the code to work. – Alan Wells Oct 05 '17 at 13:31
  • You might consider posting about how to get this to work in the [Link to Apps Script Community](https://plus.google.com/communities/102471985047225101769) – Alan Wells Oct 05 '17 at 13:33

1 Answers1

4

The code to send an email using the Sendinblue's mail service should probably look something like this:

function sendEmailWithSendInBlue() {

var url = "https://api.sendinblue.com/v3/smtp/email";
var myAccessKey = "[enter your v3 access key]"

var data = {
    "sender":{"name":"Name","email":"name@domain.com"},
    "htmlContent":"this is the body",
    "subject":"subject",
    "replyTo":{"email":"name@domain.com","name":"Name"},
    "to":[{"email":"name@domain.com","name":"Name"}]
  }

  var options = {
    'method': 'post',
    'contentType': 'application/json',
    'payload': JSON.stringify(data),
    'headers': {'api-key':myAccessKey},
  };


  var response = UrlFetchApp.fetch(url, options);

  Logger.log(response.getResponseCode())
}
beano
  • 932
  • 1
  • 15
  • 28
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • Hi Sandy, thanks for the response. Unfortunately the above code is not working for me. I don't get an error though, so it must be close. Excuse my ignorance, but are you suggesting to try the curl code aswell ? – beano Oct 06 '17 at 11:49
  • 1
    When constructing a `UrlFetchApp.fetch(url)` request, the cURL documentation is the most generic explanation of what needs to be in the HTTPS Request. The documentation for the other languages, is specific to that language, which you don't need, and is just confusing. You can't use cURL in Apps Script. And even if someone wrote a library to convert cURL commands to `UrlFetchApp.fetch(url)` commands, in the end it's `UrlFetchApp.fetch(url)`. Did the code complete? Click the View menu and click Execution transcript after running the code. Also choose View - Logs. What is there? – Alan Wells Oct 06 '17 at 12:58
  • Hi Sandy, thanks for the cURL explanation. I have edited my original question with the logs after running your code. It is executing successfully, just no email is being sent, nor any trace of the email in my sendinblue account logs. – beano Oct 06 '17 at 19:23
  • Add `/email` to the end of the url. Was that left out? `https://api.sendinblue.com/v2.0/email` – Alan Wells Oct 07 '17 at 00:38
  • With /email at the end of the url I get a response code of 400. Without it, I get a response code of 200. My understanding is that 200 should mean that the message is being delivered (to sendinblue at least). Unfortunately no email is being sent though or nothing is seen in my sendinblue account. I will contact the sendinblue support team to see if they can assist. – beano Oct 07 '17 at 01:53
  • 1
    Hi Sandy, finally got it using API v3, including the content type, and after stringifying the payload (parameters). Thanks again for steering me in the right direction! – beano Oct 07 '17 at 02:43
  • Thank you for providing the final solution. – Alan Wells Oct 07 '17 at 15:23
  • @Fanpark If you have existing code, you can ask a separate question. If you are asking a general question, then there's a good chance that it would be considered "Off Topic." I'm not sure what you're asking, and it's difficult to answer your question in the comment section. – Alan Wells Jun 10 '19 at 15:21