Cloud Functions for Firebase, sending email

Edigleysson Silva
5 min readMar 6, 2019

--

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.

The first steps are to create the design and configuration of the Firebase CLI. So if you have already done this you can go straight to Step 3.

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.

First install firebase-tools using the command:

npm install -g firebase-tools

If this command fails, you may need to check the permissions issue.

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

Once this command has been executed. A link will be displayed on your terminal. It is the authentication link, which will allow Firebase to identify the account and select the projects correctly.

So open this link in your browser and give access to your account.

After that, run the command:

firebase init functions

This command will create a Cloud Functions project in your folder (in my case, cloud-functions-sendmail). Once this command has been executed you will see a screen like:

Image 01 — Init Functions Project

As you can see. Here we will select a project. In my case, I will select the byte-girl project. Then you will choose the preferred language, between JavaScript and TypeScript. Someone like:

Image 02 — Selecting the language

Here we’ll use the JavaScript language.

Advancing you can tell if you will use ESLint and dependencies will also be installed. In this example, both of these options were set to NO.

After the command has finished executing. Some files will be created in your folder. We will only make use of the functions folder that contains the file index.js which is where we will put our functions.

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.

The packages are nodemailer that is to send e-mail, and cors that is to handle the CORS (Cross-Origin Resource Sharing) of requests.

I will not dwell here on any of these packages, for this is not the purpose of this article. See the references for more information.

To install these packages, you need to move to the functions folder and use the command:

npm install nodemailer cors

Yeee! Everything working, lets write our functions!

Step 5: Writing our functions

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

Image 03 — Generated index.js

Note that there is a commented code snippet. This excerpt shows an example of how you can define a function. Basically you only need to define a function for the exports object in such a way that this function is defined through functions. In our case it would look something like:

exports.sendMail = functions.https.onRequest((request, responde) => {});

functions.http.onRequest defines that this function will be executed through requests. But calm down. It still does not do the body of this function. Let’s do some things before.

First we will delete these comments and leave only the first line. Now we will import the firebase-admin, cors, and nodemailer modules. As shown in the image below.

Image 04 — Import modules

Note that in addition to importing we also initialize the app with admin.initializeApp ().

Now you need to create a transporter to send the email. With the help of nodmailer we will use a Gmail account to send emails. To create the transporter you only need to enter the credentials and the service, which in this case is Gmail.

Finally, the implementation of the function will use the transporter to send the e-mail. Setting options such as sender, subject, and e-mail content.

See how our index.js file was and how our function was defined.

Our function when executed will return the sent e-mail message, if all goes well and the error message in case there is a bug.

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

After executing this command the upload of your function will be started. In addition, the dependencies will be verified so that their function can execute correctly. See the output of this command:

Image 05 — Deploying

As you can see, our function can be invoked by a URL which in this case is https://us-central1-byte-girl.cloudfunctions.net/sendMail. To execute our function we just have the call with the dest parameter in the URL For example: https://us-central1-byte-girl.cloudfunctions.net/sendMail?dest=someemail@teste.com.

As you noticed by the code, the email sent is with an image of Pickle Rick (Rick and Morty Character). The sender email is picklerick.firebase@gmail.com. This email was created to perform these tests.

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!

See more about:

See my Github repo with many examples of Firebase

--

--