简体   繁体   English

如何将变量从外部php脚本传递到Google App脚本

[英]How to Pass Variables from an External php script to Google App script

I'm really new to Google Appscript, so please forgive me if this question sounds stupid. 我真的是Google Appscript的新手,所以如果这个问题听起来很愚蠢,请原谅我。

I am trying to past variables from a PHP script into a Google App Script to utilise Gmail to send emails. 我试图将变量从PHP脚本传递到Google App脚本中,以利用Gmail发送电子邮件。 I'm not quite sure if I am going about it the right way, but I'm trying to pass the variables to the appscript via a get request in the URL and access them that way. 我不确定是否要正确处理,但是我试图通过URL中的get请求将变量传递给appscript并以这种方式访问​​它们。

//get the variables from the url and send email

function doGet(e){

      var first_name = e.parameter.first_name;
      var last_name = e.parameter.last_name;
      var email = e.parameter.email;
      var phone = e.parameter.phone;

      GmailApp.sendEmail(first_name,last_name + email, phone);

    }

    doGet();

I tested it's ability to get the variables from the url via a get request by deploying it as a web app and passing some test variables in the URL: 我测试了它是否可以通过将请求部署为Web应用程序并在URL中传递一些测试变量来通过get请求从url中获取变量的功能:

https://script.google.com/macros/s/AKfycbzk14ZKDdsofFqx0vU2_kFIXLTduAMvy_G_9MyuS_d046MZIGQb/exec?first_name=testname&last_name=testname&phone=testname&email=testname

However, I get the following error on the page: 但是,我在页面上收到以下错误:

TypeError: Cannot read property "parameter" from undefined. (line 24, file "Code", project "email 6")

Could you please tell me what I am doing wrong here or is there an even better way to solve this sort of problem. 您能告诉我我在做什么错吗,还是有一种更好的方法来解决这种问题。

Thanks! 谢谢!

Remove the call to doGet() that you've placed after the function definition. 删除对函数定义之后放置的doGet()的调用。

doGet() will be called automatically when the HTTP request is received, you don't need to call it explicitly. 收到HTTP请求时,将自动调用doGet(),您无需显式调用它。

When the HTTP request is received, any code not within a function is executed before doGet() is triggered. 收到HTTP请求时,在doGet()触发之前,将执行函数内未包含的任何代码。 This means your current code is running doGet() with no parameters, thus hitting the "e" is undefined error, before the actual HTTP request is ever passed to doGet. 这意味着您当前的代码正在运行不带参数的doGet(),因此在实际的HTTP请求传递给doGet之前,单击“ e”是未定义的错误。

Otherwise the code in your function looks correct. 否则,函数中的代码看起来正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM