How to Send Email with the Gmail API and Node.js – Digital Inspiration

In a previous tutorial, we used a service account to connect to the google drive api from a node.js application. we can’t use a service account to impersonate a gmail account, but we can use the gmail api with node.js and nodemailer to send email from a user’s own gmail or google workspace account.

However, you can use external services, such as amazon’s ses or twilio’s sendgrid, to send email from a service account.

In this tutorial, we will describe how to send emails from a gmail account using the gmail api and a node.js application. Please note that Gmail imposes a sending limit of 2,000 messages per day with a total limit of 10,000 recipients per day. the email quota automatically resets at midnight pacific time.

1. create a google cloud project

Go to cloud.google.com and create a new google cloud project. give your project a name, change the project id and click the create button.

Create Google Cloud Project

2. enable google api

choose apis & services in the left menu and click enable apis and services to enable the gmail api. the gmail api allows you to view and manage gmail mailbox data such as threads, messages, and labels.

3. configure authentication consent screen

in the apis and services section, click the authentication consent screen and set the user type to internal. this will allow the app to access the gmail api without having to go through the lengthy authentication verification process that can take several weeks. click save and continue.

See Also:  Why Do Attachments Disappear In Gmail? - Universal QA

OAuth Consent Screen

4. oauth 2.0 scopes

On the consent screen, enter a name for your app and provide your email address where google can contact you if there are any changes on the consent screen.

On the next screen, you need to provide one or more oauth 2.0 scopes for google api’s. click the add or remove scopes button and add https://www.googleapis.com/auth/gmail.send to the list of scopes as we only want to send emails from gmail and not read any user data. click save and continue.

Gmail API Scopes

4. create gmail authentication client

in the api & services section, click credentials and click create credentials > oauth client id to create a new client id that will be used to identify your app to google’s oauth servers.

4. application type

Set the application type to desktop application, give your authentication client a recognizable name, and then click create to generate the credentials. Your oauth 2.0 client name is only used to identify the client in the google cloud console and will not be displayed to app users.

Node Application Type

click the json download button to download the credentials to your computer. it is recommended that you use node environment variables to store your credentials and not push this file to your github repository.

OAuth Client ID

5. get authorization code

The oauth authorization sequence begins when your application redirects the user to a google url containing the oauth client id and the requested scopes. google handles user authentication and returns an authorization code, which the app can exchange for an access token and a refresh token.

See Also:  How to Ship Succulents: A Detailed Guide

Open your command prompt and run the following command. you will be redirected to the google authorization page.

6. create an authorized oauth2 client

The browser generates an authorization code that you can paste into token.js to generate an access token and a refresh token. the access token will be valid for 1 hour and the app will use the refresh token to get a new access token when it expires.

run the following command to generate the access token and refresh token.

this will add a new token.json file to your project directory containing the access token and refresh token.

7. email sender library

We are using the popular nodemailer library to generate rfc822 formatted email messages that can be transmitted to smtp. you can also create a mime message manually, but the former is easier to use.

8. send email with gmail api

This is the last step. create a mail options object that defines the different fields of the message, including sender name, recipients, attachments, html body, and subject. You can also add headers to the message and these are useful for adding tracking information to the message.

for attachments, you can directly attach any file from the local file system to the gmail message or even extract an attachment from a remote url.

send personalized emails

if you want to send personalized emails with gmail and google sheets, you can use mail merge for gmail.

See Also:  How much does it cost to mail golf clubs

Leave a Reply

Your email address will not be published. Required fields are marked *