Simple service that provides an email backend for contact forms and other applications. The services accepts HTML form data (x-www-form-urlencoded) and sends it to a SMTP mail server using Nodemailer. The service can be deployed as Google Cloud Function, which are usually very cheap to run.
This script is written in JavaScript and requires Node.js.
- Clone the repository:
git clone git@github.com:stekhn/email-backend-function.git - Navigate to to to folder:
cd email-backend-function - Install dependencies:
npm install - Create a configuration file
- Deploy to Google Cloud Functions
Add a .env configuration by copying .env.template:
cp .env.template .envOpen the .env file and add your SMTP server settings and credentials. Example:
EMAIL_SMTP_HOST=smtp.my-mail-provider.com
EMAIL_SMTP_PORT=465
EMAIL_USER=my.name@my-mail-provider.com
EMAIL_PASSWORD=password123This assumes, that your SMTP server can communicate securely using SSL. If your mail provider or service doesn't support SSL, you might need to update the Nodemailer configuration in the the script (index.js).
These instructions assume that you already have a Google Cloud account and set up a billing account. To use the commands below, you'll need to install gcloud (CLI) and link it to your account.
Create a new Google Cloud project, if you don't have a project already:
gcloud projects create my-projectSet the new project as your current project:
gcloud config set project my-projectEnable Google Cloud Functions for the current project:
gcloud services enable cloudfunctions.googleapis.comSelect the region (data center) where your function should be deployed:
gcloud config set functions/region europe-west3Deploy the function as a Node.js application with an HTTP trigger:
gcloud functions deploy email-backend-function \
--region=europe-west3 \
--runtime=nodejs18 \
--entry-point=send \
--trigger-http \
--security-level=secure-always \Allow public HTTP requests to the function by answering y (yes)):
Allow unauthenticated invocations of new function [githubGreenWave]? (y/N)?Setting up the functions may take a minute. Once the function is created successfully, you'll get an URL that can be used in your form or application. Example: https://europe-west3-my-project.cloudfunctions.net/email-backend
If happen to fork this repository, it might be wise to test the changes to your code on your local machine first. The best way to test the script locally is to use the included Google Functions Framework.
Install the Google Functions Framework and other dependencies:
npm installStart the function by running:
npm run startThis should start a local server on http://localhost:8080. Use the email form in the test folder to validate if the function is working correctly.