Javascript 使用google web app发送电子邮件表单

Javascript 使用google web app发送电子邮件表单,javascript,html,ajax,web-applications,google-apps-script,Javascript,Html,Ajax,Web Applications,Google Apps Script,我正在尝试创建一个联系人表单,通过电子邮件发送所有数据。我在php等方面遇到了各种问题,所以我决定使用另一个google MailApp。 问题开始于我想在电子邮件中附加一个文件。 当我运行表单将数据发送到脚本时,我得到一个错误,这表明文件不能作为参数在事件中发送。 以下是mailApp脚本: var TO_ADDRESS = "someone@gmail.com"; // change this ... function doGet(e){ doPost(e); } function f

我正在尝试创建一个联系人表单,通过电子邮件发送所有数据。我在php等方面遇到了各种问题,所以我决定使用另一个google MailApp。 问题开始于我想在电子邮件中附加一个文件。 当我运行表单将数据发送到脚本时,我得到一个错误,这表明文件不能作为参数在事件中发送。 以下是mailApp脚本:

var TO_ADDRESS = "someone@gmail.com"; // change this ...

function doGet(e){ 
doPost(e);
}

function formatMailBody(obj) { // function to spit out all the keys/values from the form in HTML
  var result = "";
  for (var key in obj) { // loop over the object passed to the function
    result += "<h4 style='text-transform: capitalize; margin-bottom: 0'>" + key + "</h4><div>" + obj[key] + "</div>";
    // for every key, concatenate an `<h4 />`/`<div />` pairing of the key name and its value, 
    // and append it to the `result` string created at the start.
  }
      return result; // once the looping is done, `result` will be one long string to put in the email body
}

function doPost(e) {

  try {
    Logger.log(e); // the Google Script version of console.log see: Class Logger
    record_data(e);

    var mailData = e.parameters; // just create a slightly nicer variable name for the data
   var blob = e.parameter.fileToUpload;
   MailApp.sendEmail({
    to: TO_ADDRESS,
    subject: "Contact form submitted",
    htmlBody: formatMailBody(mailData),
   attachments: [blob]
 });

  return ContentService    // return json success results
       .createTextOutput(
         JSON.stringify({"result":"success",
                           "data": JSON.stringify(e.parameters) }))
       .setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
    Logger.log(error);
    return ContentService
      .createTextOutput(JSON.stringify({"result":"error", "error": e}))
         .setMimeType(ContentService.MimeType.JSON);
  }
}


/**
 * record_data inserts the data received from the html form submission
 * e is the data received from the POST
 */
function record_data(e) {
  Logger.log(JSON.stringify(e)); // log the POST data in case we need to debug it
  try {
  var doc     = SpreadsheetApp.getActiveSpreadsheet();
   var sheet   = doc.getSheetByName('responses'); // select the responses sheet
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
   var row     = [ new Date() ]; // first element in the row should always be a timestamp
    // loop through the header columns
   for (var i = 1; i < headers.length; i++) { // start at 1 to avoid Timestamp column
      if(headers[i].length > 0) {
       row.push(e.parameter[headers[i]]); // add data to row
      }
    }
    // more efficient to set values as [][] array than individually
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
 }
  catch(error) {
    Logger.log(e);
  }
  finally {
    return;
 }

}
以下是发送信息的表单:

<form name ="frm"
     method ="post"
     action="https://script.google.com/macros/s/AKfycbwnz1pa4p8tzf5wPqdlvgy7YVU1qPWV_ZQvX84ecSetjMX8pL8/exec" 
     enctype="multipart/form-data" 
      > 
   <table> 
        <tr>

            <td><p align="right" style="width:90%;"><input name="email" type="email" required placeholder="your.name@email.com" class="form-control"></p></td>
            <td>email</td>
        </tr>
        <tr>

            <td><p align="right" style="width:90%;"><input name="subject" type="text" placeholder="subject" class="form-control"></p></td>
            <td>subject</td>
        </tr>
        <tr>

            <td><p  align="right" style="width:90%;"><textarea rows="15" cols="40" name="comment" placeholder="write the message" class="form-control" ></textarea></p></td>
            <td>comment</td>
        </tr>
        <tr>

            <td><input type="file" name="fileToUpload" id="fileToUpload"/></td>
            <td>file</td>
        </tr>
        <tr>


            <td><input type ="submit" name="submit" class="btn btn-primary" value ="send"/></td>
            <td><input type ="reset" class="btn btn-primary" value ="reset" /></td>
        </tr>
    </table>
  </form>

我非常感谢您的帮助,因为这对我来说是一件全新的事情。

您是否仔细检查过您的blob变量是否被视为一个变量?这就是我们所需要的,有用吗?我应该添加一个答案,还是您想自己发布一个包含详细信息的答案如果你能补充一个答案,那就太好了。实际上我转到了php,这就是为什么我没有发布答案的原因。