0

My Code.gs file in my apps script project has this code (given below) whose task is to send an email using AWS SES Javascript SDK. -

function doGet() {
    var self=globalThis;
    var url = "https://sdk.amazonaws.com/js/aws-sdk-2.1206.0.min.js";
    eval(UrlFetchApp.fetch(url).getContentText());

        const data1 = "sample@gmail.com | John Doe";
        const data = data1.split(" | ");
        const email = data[0];
        const name = data[1];
        
        const configure = {
            accessKeyId: "<mykeyid>",
            secretAccessKey: "<mykeysecret>",
            apiVersion: '2010-12-01',
            region: 'us-east-1'
        };
        
        const ses = new AWS.SES(configure);
        const parameters = {
            Destination:{
                ToAddresses:[
                    email
                ]
            },
            Message: {
                Body: {
                    Html: {
                        Charset: "UTF-8",
                        Data: `<!DOCTYPE html><html>Hi, ` + name + `!</html>`
                    }
                },
                Subject: {
                    Charset: "UTF-8",
                    Data: "Here's a sample email."
                }
            },
            Source: 'Sample Sender <example@gmail.com>'
        }

        ses.sendEmail(parameters,(err,data)=>{
            if(err) {
                document.write(err);
            } else {
                document.write("Done");
            }
        });
}

When I run the doGet() function, it shows me this error -

Why did this error show up and what should I do? Also I don't have any other code except this one. I searched for this error, and I found out that setTimeout is not defined error comes when there is some typo/mistake in using it - but my code doesn't seem to have any setTimeout function - what could be wrong? I've just started with JS & google apps script, hence I am a bit confused... Kindly advice... Thanks! :)

P.S. The whole AWS code works properly when tested in a proper JS file externally.

kartik
  • 550
  • 1
  • 6
  • 19
  • 1
    From your showing script, the script of `https://sdk.amazonaws.com/js/aws-sdk-2.1206.0.min.js` is for Javascript. In the case of Google Apps Scrtipt, unfortunately, not all functions of Javascript cannot be used. In the case of your question, `setTimeout` cannot be used. And also, `document.write` cannot be used. Please be careful this. [Ref1](https://developers.google.com/apps-script/guides/services), [Ref2](https://stackoverflow.com/q/17252409), [Ref3](https://stackoverflow.com/q/29659404) – Tanaike Sep 02 '22 at 06:42
  • @Tanaike thanks for replying. What do you mean by `In the case of your question, setTimeout cannot be used` - i am not using `settimeout` in my code; also I've switched up `document.write` with `Logger.log` just for now, but still the same error. What method should I use if I still want to use the aws js sdk in my apps script? or is it not doable? – kartik Sep 02 '22 at 06:47
  • 1
    Thank you for replying. I apologize for the inconvenience. About `What do you mean by In the case of your question, setTimeout cannot be used - i am not using settimeout in my code`, I think that `setTimeout` is used in the script of `https://sdk.amazonaws.com/js/aws-sdk-2.1206.0.min.js`. When you will check the script, I think that you can see it. – Tanaike Sep 02 '22 at 06:48
  • 1
    About `What method should I use if I still want to use the aws js sdk in my apps script? or is it not doable?`, in this case, I think that there might be 2 patterns. 1. Modify the script of `https://sdk.amazonaws.com/js/aws-sdk-2.1206.0.min.js` for Google Apps Script. 2. Directly request to the endpoint of the API using Google Apps Script. – Tanaike Sep 02 '22 at 06:51
  • thanks for replying. the aws-sdk does contain setTimeout, and now the error makes sense. In the second pattern, do you mean the AWS SES API? – kartik Sep 02 '22 at 06:53
  • 1
    Thank you for replying. About `In the second pattern, do you mean the AWS SES API?`, if you want to send an email using Amazon SES API, I think that it's yes. – Tanaike Sep 02 '22 at 06:55
  • By the way, in your situation, how is the Web Apps accessed? – Tanaike Sep 02 '22 at 06:57
  • thanks for replying. the whole script is supposed to run when a google form is submitted - i've setup the OnChange() trigger, which would retrieve the data submitted in the form, and send email to the entered email address. At the moment I am in testing phase, hence I am testing this all in a separate google sheets function, using the run & log method of GAS. – kartik Sep 02 '22 at 06:59
  • also, I am not familiar with the AWS SES API (is it a REST API?) - do you know any introductory articles? and will I be able to integrate that rest api in google apps script? – kartik Sep 02 '22 at 07:00
  • Thank you for replying. About `the whole script is supposed to run when a google form is submitted`, in your script, `doGet` is used. This function is run by accessing Web Apps. In your situation, you are using `doGet` function when Google Form is submitted. Is my understanding correct? – Tanaike Sep 02 '22 at 07:02
  • 1
    About `do you know any introductory articles? and will I be able to integrate that rest api in google apps script?`, although I'm not sure whether I could correctly understand your goal, is this document useful? https://docs.aws.amazon.com/ses/latest/dg/send-email-api.html – Tanaike Sep 02 '22 at 07:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247745/discussion-between-kartik-and-tanaike). – kartik Sep 02 '22 at 07:04

0 Answers0