In this article, we will learn how to send mail using Node.js, Nodemailer, SMTP, Gmail, and OAuth2 with the help of the google cloud platform.
This article is divided into two parts in the first part we will see how to get credentials and in the second part, we will use that credentials to send mail.
Let’s start by understanding what is OAuth?
OAuth is an open-standard authorization protocol or framework that provides applications the ability to secure designated access.OAuth allows you to share information between services without exposing your password. It provides authorization flows for web and desktop applications, as well as mobile devices.
Let’s go through the steps to get a credential.
First, go to Google Developer Console to create a project. A project is needed so that we can create the necessary API credentials. Once in the console, click on Select a project in the top left corner.
Then click on the New Project in the top right corner.
Then, Enter the project name and click on the Create button.
To get the client secret and client id, we need to create OAuth credentials. For that go to APIs & Services -> Credentials in the top left corner.
After clicking Credentials, click on the create credentials, and a dropdown will appear. In the dropdown, select OAuth client ID.
After that, click on the Configure consent screen.
Select external for the User Type and then click on the create button.
Next, fill out the required fields for each step and click on Save And Continue button.
Next, click on Add Or Remove Scope and select scopes click on the Update button and then click on Save and Continue.
Now you need to add a user for app verification, click on the Add users, enter your current email id and then click on the Add button. After the user added successfully click on Save and Continue.
Next, you can see the Summary page, which displays all information and you can also edit it. And then click on the Back To Dashboard button.
Go to Credentials and click on create credentials -> OAuth client Id an application type dropdown should appear. Select Web application and you can see the following page.
Copy the client ID and client secret as shown on the screen and save it for later.
Now, We also need a refresh token that can be generated from the client id and client secret.
First, go to https://developers.google.com/oauthplayground and click the setting icon in the top right and check the Use your own OAuth credentials box. Then enter your client id and client secret.
On the left, under the Select & authorize APIs section, enter https://mail.google.com/ this URL into the input box and click Authorize APIs.
After clicking authorize APIs you should get the above screen, click on continue, and if the following pages appear, click on Continue and you should get redirected back to the OAuth page.
Now go to Step 2 of the sidebar on the left and click the Exchange authorization code for tokens. Once the refresh and access token is generated, copy the refresh token and save it for later.
Now enable your Gmail API. For this go to Library of the sidebar on the left and search Gmail API and enable it.
For Sending mail using OAuth we will see in part 2.