Firebase Functions: Send Email using Google OAuth2

firebase functions: send mail using Gmail

I was working with firebase functions recently for sending emails using Gmail. I found a very simple solution provided in firebase samples for sending an email. But when I followed the documentation, I found that be able to send emails with your Gmail account: enable access to Less Secure Apps and Display Unlock Captcha. I felt this method is not secure for sending emails to my large set of customers.

After a lot of digging, I found another way to send mail which was using OAuth2 in node.js code using nodemailer. After using methods from 2–3 different articles I was finally able to send an email through firebase functions with Node.js using nodemailer, OAuth2, SMTP.

Let's go through the steps to setup OAuth2 :

We need to create a new project in the Google Developer Console to set up OAuth2. Click on the “Select a project” button from the top left corner of the navigation bar as highlighted in the screenshot below.

Click on Select a project in Google Developer Console

On clicking that button a new popup will appear as displayed in the image below. Click on “NEW PROJECT” button in the top right corner to create a new project.

Click on “NEW PROJECT” to create a new project

Later you’ll be taken to a new screen to enter project details. Enter your project name & hit “CREATE”. It’ll take you to dashboard page of the newly created project (It’ll take 20–30 seconds), If not then go to select a project & select the newly created project.

Create a new project

Now you should be in the project dashboard screen as shown in the image below. Our project needs credentials to access API’s, in this section we will create new credentials for our project. For that click on the “API’s & Services” from the right navigation bar & select “Credentials”.

Select API’s & Services and then Credentials

Now you’ll be on the screen shown below, click on “Create credentials” & select “OAuth client ID” from the dropdown menu. Please refer screenshot given below.

Create credentials for out project

You’ll be asked to configure consent screen now in order to create OAuth client ID. Please click on “Configure consent screen”.

Create OAuth client ID screen

Enter the Application name, scroll down to the bottom of the page & click “Save” in the consent screen, It’ll automatically take you to credentials screen or just click on the Credentials tab. Please refer image shown below.

OAuth consent screen

You’ll on create OAuth client ID screen as shown below. Select “Web Application” type from Application type, Enter Name of the application and in Authorized redirect URI’s section enter “https://developers.google.com/oauthplayground” and press “CREATE”.

Creating OAuth client ID

You must’ve got a small popup window in the middle of your screen saying OAuth Client. Save the “Client ID” & “Client Secret” from the given screen. You’ll need this information in our next step & in our node.js code.

If you accidentally clicked out of popup it’ll fade away, Don’t worry pal, you can retrieve this info from credentials screen by clicking on the download icon displayed to the right of the application we just created. Search for “Client ID” & “Client Secret” in the downloaded json file.

Hurry! we’ve successfully completed step 2: Creating OAuth credential.

We’ve reached the final step of our configuration part. We just need a few more things to set up before using it in node.js app.

  • Go to this link https://developers.google.com/oauthplayground and click on the “settings icon” from the top right corner as shown in the image below.
  • Check the box saying “ Use your own OAuth credentials” and enter the “Client ID” & “Client Secret” we received in the last step.
  • Now on the left side menu enter https://mail.google.com/ in the ‘input your own scopes’ textbox or search “Gmail API v1” from the list & select https://mail.google.com/.
  • Press the “Authorize APIs” button.
OAuth 2.0 Playground screen: step 1

It’ll take you to the login screen (if you have multiple Gmail accounts on your device) or directly take you to the warning screen 😮 saying “This app isn’t verified” which will look like this.

Warning screen for the app

Don’t worry about it, Google hasn’t verified our app yet & considers our application unsafe to use (because we just created it). You can completely ignore the warning & click on “Advance” & hit “Go to <yourApp> (unsafe)

Now a small popup window will appear on the screen requesting permission to read, compose blah blah blah … just press “Allow”. Don’t worry about it because only you are going to use this application & I’m sure you’ll never delete your Gmail data 😄

It may again ask you to confirm your choice (Google thinks a lot about your privacy😅) press allow on that screen too. and it’ll take you to “OAuth 2.0 Playground” screen but now in step 2 of it.

The screen will look like this, Click on “Exchange authorization code for tokens” and it’ll display Refresh token & Access token of our app. Save the Refresh token, we will need it in our node.js code.

OAuth 2.0 Playground screen: step 2

TAADAA!!! We’ve completed our OAuth2 setup part. 🎉
In next section we will write node.js code for our firebase function

Code part 💻

Now coming to the very interesting task… WRITING CODE … 😊

But if you are an android developer and never seen node.js code before this can be a bit difficult for you. No worry, we are developers & we can learn any language. 😄

I’ve created a git repo for this node.js code which you can copy, enter your details (Remember I told you to copy a few things in the previous part 🙄 ). and you good to go.

If you are new to node.js then follow this documentation for firebase function.

Don’t forget to install the libraries required for this app.

npm install nodemailer googleapis

After deploying the function on the server you call the function from your app directly. Follow this guide for your platform.

This is my first medium post, I’m expecting your feedback on this to encourage me to write more.

Please rate my post on the scale of 1 to 10 by hitting that many claps👏.

If you didn’t like any part of it or need more clarity on any part, let me know in the comments section below I’ll try to resolve it asap.

Thank you for reading !!! 🙏🙏🙏

Android Lover, Tech enthusiast, UI/UX♥️, Flutter follower😍, Software Developer.