Skip to content

stekhn/email-backend-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Email Backend (Google Cloud Function)

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.

Basic usage

This script is written in JavaScript and requires Node.js.

Configuration file

Add a .env configuration by copying .env.template:

cp .env.template .env

Open 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=password123

This 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).

Google Cloud deployment

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 project

Create a new Google Cloud project, if you don't have a project already:

gcloud projects create my-project

Set the new project as your current project:

gcloud config set project my-project

Deploy function

Enable Google Cloud Functions for the current project:

gcloud services enable cloudfunctions.googleapis.com

Select the region (data center) where your function should be deployed:

gcloud config set functions/region europe-west3

Deploy 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

Local testing

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 install

Start the function by running:

npm run start

This 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.

About

Email backend for HTML forms that runs as a Google Cloud Function

Resources

Stars

Watchers

Forks