Giter Site home page Giter Site logo

basharath / formeasy Goto Github PK

View Code? Open in Web Editor NEW
145.0 3.0 14.0 49 KB

A simple open source library to receive contact forms for static sites

Home Page: https://devapt.com/formeasy

License: MIT License

JavaScript 65.75% HTML 34.25%
apps-script apps-script-lib formeasy forms apps-script-form

formeasy's Introduction

FormEasy

FormEasy licence FormEasy forks FormEasy stars FormEasy issues FormEasy pull-requests

FormEasy is a free and open source apps script library that lets you receive forms from your static sites very easily.

Script ID: 1CAyzGbXdwMlko81SbJAjRp7ewxhyGKhDipDK4v8ZvlpYqrMAAzbFNccL

Adding FormEasy library to Apps Script

  1. Open a new Google sheets file(this is where your form data gets stored)
  2. From the menu bar click Extensions > Apps Script and it opens up a new apps script file
  3. In the left bar of apps script file click + button beside Libraries
  4. Add the Script ID listed above and click Look up button and select the latest version. Note the identifier it is going to be used to invoke the functions in the library and finally click Add button.

Now you can use FormEasy object inside the apps script file.

Usage

Clear the default function if any in the apps script file and add the below function.

Simplest case

function doPost(req) {
  FormEasy.setEmail('[email protected]'); // To receive email notification(optional)
  return FormEasy.action(req); // Mandatory to return action method
}

The default data fields are: name, email and message. To add more, use setFields method as shown below.

With more customizations

function doPost(req) {
  FormEasy.setSheet('Sheet1'); // Optional
  FormEasy.setEmail('[email protected]'); // To receive email notification(optional)
  FormEasy.setSubject('Email subject'); // Optional
  FormEasy.setFormHeading('Form heading inside email'); // Optional
  FormEasy.setFields('name', 'email', 'website', 'message', ...); // Optional(name, email, messsage are default)
  return FormEasy.action(req); // It should be at the end and return it
}

After adding the above function click the Deploy button at top right corner and select New deployment and select type to Web app from the gear icon.

Select/fill the below options

  • Description(optional),
  • Execute as Me(you email)
  • Who has access Anyone

Click Deploy button(authorize the script if you haven't done before) and you will get a URL under Web app, copy that and it is going to be the end point for submitting the POST request.

Note: You need not make New deployment everytime if you want to use the same web app URL. Select Manage deployments and update the version to keep the same URL.

Form submission using fetch

const data = {
  name: 'John',
  email: '[email protected]',
  message: 'Receiving forms is easy and simple now!',
};

const url = 'https://script.google.com/macros/s/<Deployment ID>/exec';

fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'text/plain;charset=utf-8',
  },
  body: JSON.stringify(data),
})
  .then((res) => res.json())
  .then((data) => console.log('data', data))
  .catch((err) => console.log('err', err));

Note: The keys of the data object should match with the fields that are set using setFields method in the apps script file. The default keys are name, email and message.

Article: https://devapt.com/formspree-alternative-formeasy

Demo submission with live Google sheet

Here is the demo code and the live Google sheet to get an idea on how this FormEasy library helps in receiving forms.

Captcha validation

FormEasy supports multiple captcha providers to allow you to prevent unverified submissions by robots. Each provider is unique and requires a unique configuration. Please refer to the documentation below to enable a specific captcha provider.

Google reCAPTCHA V2

  1. Register a site and get your secret key, and site key: https://www.google.com/recaptcha/admin/create

  2. In your apps script file, inside function doPost, add the following configuration:

function doPost(req) {
  // ...
  FormEasy.setRecaptcha('YOUR_SECRET_KEY'); // To validate reCAPTCHA
  // ...
  return FormEasy.action(req); // Mandatory to return action method
}
  1. On your website, add the reCAPTCHA library at the end of the <head> tag:
<head>
  <!-- ... -->

  <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
  1. Add reCAPTCHA input into your form:
<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
  1. You should see I am not a robot box on your site. If you don't, please refer to reCAPTCHA Docs for debugging.

  2. Inside your fetch() method, add a reCAPTCHA response from the input:

const data = {
  // ...
  gCaptchaResponse: document.getElementById('g-recaptcha-response').value,
};

// ...

Google reCAPTCHA V3

Steps 1 & 2 same as above.

  1. On your website, add the reCAPTCHA library at the end of the <head> tag:
<head>
  <!-- ... -->

  <script src="https://www.google.com/recaptcha/api.js?render=<YOUR_SITE_KEY>" async defer></script>
</head>
  1. Read the form data, reCAPTCHA V3 response token and send the request.
const siteKey = '<YOUR_SITE_KEY>';

const url = 'https://script.google.com/macros/s/<Deployment ID>/exec';

function handleSubmit(event) {
  event.preventDefault();

  // Make an API call to get the reCAPTCHA token
  grecaptcha.ready(function () {
    grecaptcha.execute(siteKey, { action: 'submit' }).then(function (token) {
      // Add the reCAPTCHA token to the form data
      data.gCaptchaResponse = token;
      data.name = document.getElementById('name').value;
      data.website = document.getElementById('website').value;
      data.email = document.getElementById('email').value;
      data.message = document.getElementById('message').value;

      fetch(url, {
        method: 'POST',
        headers: {
          'Content-Type': 'text/plain;charset=utf-8',
        },
        body: JSON.stringify(data),
      })
        .then((res) => res.json())
        .then((data) => console.log('data', data))
        .catch((err) => console.log('err', err));
    });
  });
}

document.getElementById('<YOUR_FORM_ID>').addEventListener('submit', handleSubmit);

Video instructions

To see all the above instructions step by step, check this quick demo video.

FAQs

1. Is it safe to grant permission to the apps script file while using FormEasy library? Yes, it is completely safe.

FormEasy code doesn't interact with any remote servers. You can check the source code of the FormEasy library using its ScriptID.

Google shows it unsafe because it hasn't verified the script. Even if you write your own script and grant permission the same message will be shown.

2. Can I customize FormEasy script?

Yes. You're free to customize any part of the FormEasy script and deploy on your own to reflect the same.

If you want even others to use your customizations then you can contribute your code and once verified it will be pushed to the main script. You can check contributing guidelines.

3. What are the limitations of FormEasy?

There are no specific limitations for FormEasy library.

But Google Apps Script limits the email to 100/day and script run time to 6min/execution. You can see more about those here

Contributing

Pull Requests are always welcome!

If you wish to contribute using Github, you can work on any feature ideas you have or any bug fixes if you have noticed.

After your PR gets merged, you'll be apparing on the contributors page.

License

FormEasy is distributed using the MIT License. Check the License details.

Support

If you found this library helpful, please give a star ⭐️

If you like this open source work, consider supporting with a coffee ↓

formeasy's People

Contributors

aykarageorge avatar basharath avatar meldiron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

formeasy's Issues

Blocked by CORS policy

I've followed every step, and made sure that who can access it is set to "anyone", but I still get blocked by CORS. Any ideas?

blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

works in postman but not in my html site

my site

on my site if i send the form the text fields values are not send only the headers

image

<div class="form-container"> <form action="https://script.google.com/macros/s/AKfycbzrq4fNAtdprIhWNuG9jKUZWhQzvJs6vBU4RvbFlvQY8yYGmWJapgjEg8F3cyyzgJzp/dev" method="POST" class="cf"> <div class="form-field"> <input type="text" name="name" id="input-name" placeholder="Naam" required> </div> <div class="form-field"> <input type="email" name="email" id="input-email" placeholder="Emailadres" required> </div> <div class="form-field"> <input type="text" name="subject" id="input-subject" placeholder="Onderwerp" required> </div> <div class="form-field"> <textarea name="message" id="input-message" placeholder="Bericht" required></textarea> </div> <div class="form-field button-group"> <input type="submit" value="Verstuur" id="input-submit"> <a href="https://wa.me/639614391029" class="whatsapp-btn"> <i class="fab fa-whatsapp"></i> +63 961 439 10299 </a> </div> </form>

"function doPost(req) {
Logger.log(req);
FormEasy.setSheet('haha'); // Optional, replace with the name of the sheet you want to use
FormEasy.setEmail('[email protected]'); // Replace with your email if you want to receive email notifications
FormEasy.setSubject('New form submission'); // Optional, this will be the subject of the email notification
FormEasy.setFormHeading('Form Submission Details'); // Optional, this will be the heading in the email notification
FormEasy.setFields('name', 'email', 'subject', 'message' , 'message'); // Replace these with the names of the fields in your form
return FormEasy.action(req); // This should be at the end and return it
}
" Could some one point me in the right direction?

🚀 Feature: Captcha verification

To prevent spamming, some contact forms implement human verification tools such as reCAPTCHA v2, hCaptcha, and others.

I am proposing a feature in FormEasy to allow communication with these captcha servers to validate form submissions.

One way of implementing it I can see is to update FormEasy.action(req); function with new options parameter:

const options = {
  captcha: "recaptcha_v2",
  captchaConfig: { secretKey: "........." }
};

FormEasy.action(req, options);

FormEasy would then look for proper implementation of specific captcha on a website.. That can be verified by looking at JSON data, and for instance, looking for g-recaptcha-response key in the case of Recaptcha V2.

Finally FormEasy talks to API servers to verify the token from captcha.

(all of that properly documented, of course)

Would you be interested in this feature inside FormEasy? I would love to contribute to this feature in my free time.

Email and subject not working

I setup everything but I don't get the email notifications and I also don't get the subjects. What did I do wrong?

image
image
image
image
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.