0

I am trying to send an email from a Google Apps Script function (using Amazon SES Javascript SDK) using the below code -

function sendemail(email,name,code,date,expiry,version) {
  eval(UrlFetchApp.fetch("https://sdk.amazonaws.com/js/aws-sdk-2.1206.0.min.js").getContentText());

  const configure = {
      accessKeyId: "<mykeyid>",
      secretAccessKey: "<myaccesskey>",
      apiVersion: '2010-12-01',
      region: 'us-east-1'
  };
  
  const ses = new AWS.SES(configure); //AWS is not defined error

  //... rest of code
}

Now when I run the sendemail() function, it says "AWS is not defined" - which means clearly there is something wrong with the way I am referencing the external file, hence its not able to find AWS. What am I doing wrong? I know it might sound like a silly question, but I am a beginner with JS hence a bit confused on what is wrong... please guide... Thanks! :)

TheMaster
  • 45,448
  • 6
  • 62
  • 85
kartik
  • 550
  • 1
  • 6
  • 19

1 Answers1

1

Depending on how the external script executes, the loaded AWS global object may not be properly exported. In this case, the script tests whether self(as well as window and module) is available and exports itself there. The global object in apps script can be referenced by globalThis. So adding,

const self = globalThis;

would help export AWS object on to globalThis. It's also preferable that you also use indirect eval and not direct eval. Therefore eval should be outside any function and that prevents the script from accessing your local variables. You may also overload global variables using proxies to avoid the script from accessing privileged data.

Even after all this, the script might require certain objects only available in browser like setTimeout. You may need to emulate those object's behavior using functionality already present in apps script. For example, setTimeout may be emulated using Utilities.sleep. It's a step by step manual process of figuring out all the "missing" dependencies and emulating each one of them.

TheMaster
  • 45,448
  • 6
  • 62
  • 85