Dynamic mail id in mail service app of Google apps

2019-08-21 12:42发布

问题:

I need to send a mail with a default message to the mail id specified by the user in the email field of an html form.I tried the below code which doesn't work.Is anything like URL queuing needed here.

<form method="POST" action="https://script.google.com/macros/s/AKfycbwIzz6c_PL7D9ioCrzDRvTdmBj2nZR-2ekUZ9pjgHeG3b3Simg/exec">
    <label for="mail">Enter your mailID:</label>
    <input type="email" id="mail" required>
    <input type="submit">
</form>

Google script API:

 function doPost(e){
  var addr = JSON.stringify(e);
  MailApp.sendEmail(addr,
                   "Recovery email for TracerSC",
                   "Your one time password ");

}

This is the error that I get

Invalid email: {"parameter":{},"contextPath":"","contentLength":0,"queryString":"","parameters":{}} (line 5, file "Code", project "email")

The below code which has the default mail id works fine for me

function doPost(){

  MailApp.sendEmail("example@abc.com",
                   "Recovery email for TracerSC",
                   "Your one time password ");

}

回答1:

You can retrieve values by adding the name attribute to the input tag. The modified HTML is as follows. In this sample, you can use the value of example@abc.com using e.parameter.addr.

Modified HTML :

<form method="POST" action="https://script.google.com/macros/s/AKfycbwIzz6c_PL7D9ioCrzDRvTdmBj2nZR-2ekUZ9pjgHeG3b3Simg/exec">
    <label for="mail">Enter your mailID:</label>
    <input type="email" id="mail" name="addr" required>
    <input type="submit">
</form>

Value e of doPost(e)

{
  "parameter": {
    "addr": "example@abc.com"
  },
  "contextPath": "",
  "contentLength": 22,
  "queryString": "",
  "parameters": {
    "addr": [
      "example@abc.com"
    ]
  },
  "postData": {
    "type": "application/x-www-form-urlencoded",
    "length": 22,
    "contents": "addr=example%40abc.com",
    "name": "postData"
  }
}

If I misunderstand your question, I'm sorry.