Send email without server using google script: unable to open file error

▼魔方 西西 提交于 2021-01-28 06:29:22

问题


I want to send an email to a location target@example.com using google script . I am following this - https://github.com/dwyl/html-form-send-email-via-google-script-without-server to setup the whole functionality in my website.

There is an html form in my website and I want to send email whenever someone clicks on the submit button on my site

here is the html of the form -

<div id="content">
		<h1>Contact Us</h1>
		<h4>Fill out the form below and a representative will
contact you shortly.</h4>
		<form id="gform" method="POST" action="https://script.google.com/macros/u/1/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec">
  <div class="form-group">
    <label for="exampleInputEmail1">Your Name (required)</label>
    <input type="text" class="form-control" id="exampleInputEmail1" required>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Your Email (required)</label>
    <input type="Email" class="form-control" id="exampleInputPassword1" required>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Subject</label>
    <input type="text" class="form-control" id="exampleInputPassword1">
  </div>
  <div class="form-group">
  	<label for="exampleInputPassword1">Message</label>
  	<textarea class="message"></textarea>
  </div>
  <button type="submit" class="btn btn-default">Submit</button>

Here is the script.gs file -

/******************************************************************************
 * This tutorial is based on the work of Martin Hawksey twitter.com/mhawksey  *
 * But has been simplified and cleaned up to make it more beginner friendly   *
 * All credit still goes to Martin and any issues/complaints/questions to me. *
 ******************************************************************************/

var TO_ADDRESS = "target@example.com"; // where to send form data

function doPost(e) {

  try {
    Logger.log(e); // the Google Script version of console.log see: Class Logger
    MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
                      JSON.stringify(e.parameters));
    // return json success results
    return ContentService
          .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);
  }
}

When I click on submit button after filling in the form I get this -

I get the same screen when I click on Test web app on your latest code.

What I've found out -

There is something I need to add to my html tags inside form as "name" attribute but its not very clear what to add.

Where am I going wrong in setting up this feature ?


回答1:


Okay, I think I have figured out your problem.

There seems to be two problem:

  • You seemed to be logged in with two gmail account: That is why /u/1 got appended to script address (we don't need it, may be a bug). So, https://script.google.com/macros/u/1/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec becomes https://script.google.com/macros/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec
  • You need to add a trigger to your script. When you hit the url, that trigger will call a particular function of your script. What you need to do is set a trigger that fires doPost() whenever submit is clicked. doPost() sends your email.

Notes:

  1. How to add trigger? Go to Edit > Current projects' trigger (or we can use the clock button too). Add your trigger there. Under run, select doPost(), under event select from spreadsheet -> then on form submit. Save it.
  2. When you access https://script.google.com/macros/s/AKfycbwYbJ5WvIRmizYMr8MMtNVdIodpdYcJHz4DuO97Oxnuw4lnu3k/exec from browser (without /u/1 :-) ) it will give error that doGet() function is not present which is true and your web browser sent a GET request.


来源:https://stackoverflow.com/questions/46997386/send-email-without-server-using-google-script-unable-to-open-file-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!