Search⌘ K
AI Features

Secure Emails

Explore how to securely integrate Gmail into Node.js applications using Nodemailer and OAuth2. Understand how to obtain client credentials from Google Developer Console and generate access and refresh tokens using the OAuth2 Playground. Learn how to configure Nodemailer with these tokens to send emails safely and avoid Gmail blocking issues during deployment.

There are two methods that configure Gmail into Nodemailer and Node.js.

  1. We can use our email and password directly, like this:
Javascript (babel-node)
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "<your_email>@gmail.com",
pass: "<gmail_password>"
}
});

This method is less secure and may work if we are developing the application locally on our machine. But if we deploy the application, we’ll most likely be blocked by the Gmail client.

  1. We can use OAuth2 client credentials, like this:
Javascript (babel-node)
const Transport = nodemailer.createTransport({
service: "Gmail",
auth: {
type: "OAuth2",
user: "<your_email>@gmail.com",
accessToken: "<your_oauth_access_token>",
refreshToken: "<your_oauth_refresh_token>",
clientId: "<your_gdc_client_id>",
clientSecret: "<your_gdc_client_secret>"
},
});

We used the OAuth2 method ...