Cloud Functions for Firebase, sending email

Cloud Functions is a Google Cloud feature that allows functions to run in the cloud. In this article I’ll show you the use of Cloud Functions in Firebase, creating an e-mail sending function that will work through web requests.

Step 1: Creating the project in Firebase Console

To create your project you must have a Google account and access the https://console.firebase.google.com link. You will see the Firebase welcome screen. On that same page you can see the “Add Project” button. Click on this button and fill in the information for your project. Okay, your project is set up, let’s move on.

Step 2: Installing the Firebase CLI

From this step the operations will all be on your local machine. Let’s then configure the environment by installing the Firebase CLI. This tool allows us to do a lot of things, but here we will deal only with Cloud Functions.

npm install -g firebase-tools

Step 3: Using the CLI

Our environment is already set up and now it’s time to start developing. Create a folder called cloud-functions-sendmail (or the name that you want). In this folder run the command:

firebase login
firebase init functions
Image 01 — Init Functions Project
Image 02 — Selecting the language

Step 4: Installing dependencies

The purpose here is to create an e-mail sending function. But for this we will need some packages, so let’s install them.

npm install nodemailer cors

Step 5: Writing our functions

Lets see our index.js file. The content is someone like:

Image 03 — Generated index.js
exports.sendMail = functions.https.onRequest((request, responde) => {});
Image 04 — Import modules

Step 6: Deploy

Once you have created the function, just send to the cloud To do this, inside the functions folder execute the command:

firebase deploy
Image 05 — Deploying

References

Anyway, we’re done. You now have a mail sending function running in the cloud. Expand your horizons even more, integrate other Firebase tools into Cloud Functions, other modules, anyway. The sky is the limit!

Fullstack developer with expertise in web and mobile development with Flutter, Firebase, Ionic, PHP, C#, JS and Python. Now I’m trying to learn Erlang/Elixir :D