Serverless contact form handling for static websites

Jul 29, 2019 · 3 min read

When you’re making a static website, it means you don’t want to manage the server or want to save on costs. However, a static website can’t process contact forms, meaning you need a backend server to handle contact form requests. But if you need a server, then what is the logic of making a static website? So the simple solution is serverless contact forms.

Why go serverless?

There are many benefits of making serverless applications, but in short, the main benefits are easy setup, no management, and less cost (free).

Platform Selection

There are many ways to run serverless code like aws lambda, Google cloud functions, cloudflare workers. But I have selected the easiest way to run serverless code i.e. Google cloud functions.

Google Cloud functions allows you to trigger a function via HTTPS Request. It can also be done with aws lambda, but you need to set up API Gateway additional to trigger the function.


Google cloud offers free 200,000 GHz-seconds of compute time, 5GB of Internet egress traffic, and 2 million invocations per month.

I’m using 256MB/0.4GHz runtime and it takes ~800ms to process a form.

This free quota is too much to handle a contact form submission.

0.4 x 0.8s = 0.32 GHz-seconds per invocation

200,000 / 0.32 = 625,000

You can handle 625,000 Successful form submissions per month for free*.

*This is just a sample calculation and does not include email cost. Free resources are subjected to Google cloud policies.


Step 1: Create Project

(If you don’t have a Google cloud account, create one then create project)

Step 2: Create Function

Click on the menu icon on the left top corner. Scroll down and find Cloud Function under COMPUTE section.

Then click on the ‘Create Function’ button.

Step 3: Fill up the form

Set function name (contact-form).

Allocate Memory (256MB is enough)

Set Trigger ‘HTTP’ — it works for https also.

Authentication is not required, so keep it checked to accept unauthenticated requests.

Source code can be added inline or ZIP file can be uploaded. You can find zip file here

I have selected inline editor option here.

Step 4: Add code to editor

Open the repository

Copy the code of index.js and package.json files and paste it to respective code editor tabs.

In the field Functions, to execute type onSubmit

Step 5: Set Environment Variables

All the Environment Variables Related to SMTP are required. The rest of the variables are optional. Check the configuration details

Click on ‘Create’ button and your function is ready.

Note: Instead of setting environment variables, you can also change the config values in index.js file.

Get http endpoint

Once function is deployed, click on the function name (contact-form) then Trigger tab to find the url.

Your url will look like this.

Now you can set this url in action attribute in your HTML form

Here is a working example of contact form with Google cloud function

Note: This code allows to accept requests from any domain. To restrict this with your domain only, you can also change ‘Access-Control-Allow-Origin’ value from ‘*’ to ‘’.

Level Up

Level up your tech skills.


Written by


Maker of

Level Up

Level Up

Level up your tech skills.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade