Automailler for Firebase Cloud Function with Nodemailer

Esat Kemal Ekren
5 min readDec 16, 2019

--

Hello Everyone!

After a long time break, I finally got time to share with you something I learned new. In this article, I will try to explain with detail codes about how Firebase Cloud Function works with Nodemailler when new data inserted your realtime database. I hope when you read everything you could able to send mail for free and automated.

Before I start I need to tell you something I’m an iOS Developer and I’m not an expert about JavaScript so If there is a greater or proper solution which you already know please share your thought below.

Let’s get the start. We need 4things to accomplish this project.

1- Terminal ( iTerm, etc)

2- Code Editor ( Visual Studio Code, Sublime, Atom. etc )

3- Firebase Account

4- Basic JavaScript knowledge

Also, you need to install npm on your computer you can get help from this link.

STEP-1

First, we need to open https://console.firebase.google.com/ then create a new project. I named it Cloud-Function but you can give any name you want.

Creating a Project Image

Before we start coding you need to enable functions like the image below.

Function’s Page

We need to press the Get Started button then follow the instructions. First, we need to install firebase tools to our computer with command in the below

npm install -g firebase-tools

Then you need to Login Firebase with this command.

firebase login

You will be re-directed to Google Accounts page after you successfully enter your credentials you are good to go for the next step.

STEP-2

After we installed all tools and logged in now it’s time to initialize our firebase. You need to create a folder and open that from the terminal.You can see my folder name is Cloud-Functions

Then initialize firebase with this command.

firebase init

When you successfully run this command you should see something like this. Choose the third option which is “Functions: Configure and deploy Cloud Functions then you need to choose a project that you created at the beginning. In my case Cloud-Function

Tip: If you want to do the same process on all your cloud function projects. You could just write a shortcut function in the below instead of “firebase init

firebase init functions

After that, we have to choose what type of language we will be using in the cloud function. You can choose Typescript or JavaScript. In my case, I’ll choose JavaScript

Then, you will see Do you want to use ESLint to catch probable bugs and enforce style? I prefer N for this.

Lastly, you will see Do you want to install dependencies with npm now? I say yes on this.

Now we are all set for firebase. But because we are going to use Nodemailler for sending mail we need to implement that dependency too. Before write install command for nodemailler. We need to open the functions folder from the terminal. Then you can safely run this command.

npm install nodemailer cors

Okay, we are all set now. It’s time to open our code editor and start writing our sending mail function. Open file index.js from functions file. You should see something like this. I prefer Visual Studio Code.

I will delete all boilerplate code than write it from scratch. First, we need to set the required constants.

Then initialize our app. I’m using the Gmail provider for sending mail. I strongly recommend allowing Less secure apps from Gmail settings. You can find more detail in this link

It’s time to create the main function for sending mail. This function main purpose handles and gets data that is created in the realtime database. So we can access new elements of data details and put them in the HTML template we created.

You can see two variables in code one of is val.title and other val.subject these values belongs firebase database sub-child name which is shown in the below

Then we ready to deploy our function into Firebase. You need to just save index.js file and write this simple command ‘firebase deploy’. Make sure to run this command in the functions folder.

After you see the message deploy complete! We are on test stage.

Let’s test it!

Here is the template we sent:

I manually added data for making this article keep it simple. So thank you for reading this article I hope I could help you a little.

Before I finished I need to thank you Edigleysson Silva. I read his article and learn a lot from this link you should check it out too.

See more about:

If you have any further question I’d be happy to help.

See you later!

--

--