What Are We Going to Build?
We’re going to build a simple HTML form with a serverless function to save all the emails from your users without any hard limit.
Web services that provide you the possibility to save email are usually limited to 1,000 or 2,000 emails with the free tiers, and we will solve this issue easily.
With our combination, we will get up to 125,000 new emails every month for free using Netlify functions or similar serverless hosting providers.
Saving a simple email from your users shouldn’t require you to buy any expensive or external tool.
Netlify offers forms submission, too, but only 100 basic submissions per month.
The Tech Stack
- Free PostgreSQL database service like the one from ElephantSQL or from Heroku
- Static HTML file hosting for the form: Netlify or any other service like GitHub Pages
- Serverless functions hosting: Netlify or Zeit for our simple NodeJS function to save all emails in the database
The Back-End Code
We’re going to save the emails on a database — if you need a simple database with PostgreSQL, you can use the ElephantSQL free tier.
You can build a simple
email_signup table with this SQL code:
You’ll need to host this node-simple serverless function somewhere. I suggest you use Netlify functions.
For the code, we’re going to use just a few basic modules to enable you to check the email validity without any external API:
disposable-email-domains: It’s a public module that gives you a complete list of temporary emails to prevent a user from using disposable emails
pg: Official module to send data to the PostgreSQL database as a QUERY
url: Node modules to handle the URL from the request and to build a simple HTTP server to receive the HTTP requests with the user emails
Remember to replace the
connectionString with the connection URL from your database, in our case from ElephantSQL.
We also prevent SQL injection by using prepared statements with bound parameters in the
const insertQuery = “INSERT INTO email_signup(email) VALUES($1)”;
With this Query, your users won’t be able to submit any other value inside this SQL command thanks to the bound set by the
We also handle various response like a 403 status code with an error message if the user sends a temporary email or one without a domain after the @.
The Front-End Code
The front-end code is just a simple HTML form, but that will send the email to our serverless function and will display the message sent from the server inside the result div.
The good thing is that the server handles everything, and you won’t need to update the front-end code but just the server with different messages.