NodeJs v10.15.3 test codes for sending out emails via GMail API.
GMail API: send emails with NodeJs.
This post is a continuation of this
GMail API: quick start with
Python and NodeJs post of mine – I’ve now done the codes for sending out emails in
NodeJs v10.15.3 .
We’ll need nodemailer
library. Please run the below command to install it, in the same directory where
we’ve run:
npm install googleapis@39 –save
before.
I’m using the starting codes provided by
Google’s API Node.js quickstart: https://developers.google.com/gmail/api/quickstart/nodejs .
I’m replacing:
function listLabels ( auth )
with
async function sendEmail ( auth )
The codes in this sendEmail( … )
function is a condensed and cut down version of the codes in this post
How to Send Email with the Gmail API and Node.js .
File: gmail-api-sendmail.js
const fs = require ( ' fs ' );
const readline = require ( ' readline ' );
const { google } = require ( ' googleapis ' );
const MailComposer = require ( ' nodemailer/lib/mail-composer ' );
// If modifying these scopes, delete token.json.
const SCOPES = [ ' https://www.googleapis.com/auth/gmail.send ' ];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = ' token.json ' ;
// Load client secrets from a local file.
fs . readFile ( ' client_secret_oauth_gmail.json ' , ( err , content ) => {
if ( err ) return console . log ( ' Error loading client secret file: ' , err );
// Authorize a client with credentials, then call the Gmail API.
authorize ( JSON . parse ( content ), sendEmail );
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize ( credentials , callback ) {
const { client_secret , client_id , redirect_uris } = credentials . installed ;
const oAuth2Client = new google . auth . OAuth2 (
client_id , client_secret , redirect_uris [ 0 ] );
// Check if we have previously stored a token.
fs . readFile ( TOKEN_PATH , ( err , token ) => {
if ( err ) return getNewToken ( oAuth2Client , callback );
oAuth2Client . setCredentials ( JSON . parse ( token ) );
callback ( oAuth2Client );
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getNewToken ( oAuth2Client , callback ) {
const authUrl = oAuth2Client . generateAuthUrl ({
access_type : ' offline ' ,
scope : SCOPES ,
});
console . log ( ' Authorize this app by visiting this url: ' , authUrl );
const rl = readline . createInterface ({
input : process . stdin ,
output : process . stdout ,
});
rl . question ( ' Enter the code from that page here: ' , ( code ) => {
rl . close ();
oAuth2Client . getToken ( code , ( err , token ) => {
if ( err ) return console . error ( ' Error retrieving access token ' , err );
oAuth2Client . setCredentials ( token );
// Store the token to disk for later program executions
fs . writeFile ( TOKEN_PATH , JSON . stringify ( token ), ( err ) => {
if ( err ) return console . error ( err );
console . log ( ' Token stored to ' , TOKEN_PATH );
});
callback ( oAuth2Client );
});
});
}
/**
* Send a test email.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
async function sendEmail ( auth ) {
const options = {
from : ' Email_Used_In_Google_Cloud_Platform_Project@gmail.com ' ,
to : ' behai_nguyen@hotmail.com ' ,
cc : ' behai_nguyen@protonmail.com ' ,
replyTo : ' Email_Used_In_Google_Cloud_Platform_Project@gmail.com ' ,
subject : ' Message via GMail API written in NodeJs ' ,
text : ' Hello, this email is sent from GMail using GMail API OAuth written in NodeJs. ' ,
html : ' <h1>Hello, this email is sent from GMail using GMail API OAuth written in NodeJs.</h1> ' ,
textEncoding : ' base64 '
};
const gmail = google . gmail ({ version : ' v1 ' , auth : auth });
const mailComposer = new MailComposer ( options );
const message = await mailComposer . compile (). build ();
const rawMessage = Buffer . from ( message ). toString ( ' base64 ' ). replace ( / \+ /g , ' - ' ). replace ( / \/ /g , ' _ ' ). replace ( /=+$/ , '' );
const { data : { id } = {} } = await gmail . users . messages . send ({
userId : ' me ' ,
resource : {
raw : rawMessage ,
},
});
console . log ( id );
};
This is my command to run it:
"C:\Program Files\nodejs\node.exe" gmail-api-sendmail.js
The sent email as seen in my Hotmail account.
The sent email as seen in my Proton Mail account.
This Google link
OAuth 2.0 Scopes for Google APIs
lists all OAuth 2.0 Scopes.
The scope used in the codes above is:
const SCOPES = [ ' https://www.googleapis.com/auth/gmail.send ' ];
That’s all for this post. Thank you for visiting and I do
hope you find it useful.