A Step-by-Step Guide to Sending Emails with Node.js and Nodemailer Via Gmail.

Ayush Nandanwar
3 min readMar 24, 2024

--

Sending emails programmatically using Node.js can be a powerful tool for automating various tasks, such as sending notifications, newsletters, or transactional emails. In this tutorial, we’ll explore how to set up a Node.js script using the Nodemailer package to send emails via the Gmail service.

Prerequisites:
-
Basic understanding of JavaScript and Node.js

Steps to set up an app password in Gmail.

  1. To send mail using nodemailer via Gmail service you must have 2-step verification enabled in your Google account.
  2. Go to Google Account. You will find a 2-step verification option in the Security tab

3. Click on 2-Step Verification.

4. There you will find App passwords click on it.

5. It will redirect you to create an app and give a name to your app.

6. Click on the Create button.

7. You will see a window showing your app password.

Please note this password as it appears only once.

Step 1: Set Up a Node.js Project
1. Create a new directory for your Node.js project.
2. Open a terminal and navigate to the project directory.
3. Initialize a new Node.js project by running `npm init -y` to generate a `package.json` file.

Step 2: Install Nodemailer Package
1. Install the Nodemailer package by running the following command.

npm install nodemailer

Step 3: Create a Script to Send Emails
1. Create a new JavaScript file (e.g., `sendEmail.js`) in your project directory.
2. Require the Nodemailer package at the top of your script:

3. Set up a transporter using your Gmail account credentials:

const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
service: "Gmail",
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: "your-email@gmail.com",
pass: "your_app_password",
},
});

Note: For security reasons, avoid hardcoding your email and password directly in the script. Consider using environment variables or a configuration file. We will discuss security best practices in upcoming blogs.
4. Define the email options (e.g., sender, recipient, subject, body):

const mailOptions = {
from: 'your-email@gmail.com',
to: 'recipient-email@example.com',
subject: 'Test Email from Node.js',
text: 'This is a test email sent from Node.js using Nodemailer.',
};

5. Send the email using the transporter and mail options:

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error('Error occurred:', error);
} else {
console.log('Email sent successfully:', info.response);
}
});

Step 4: Run the Script
1. Save your script (`sendEmail.js`).
2. Open a terminal and navigate to your project directory.
3. Run the script using Node.js:

node sendEmail.js

Step 5: Verify Email Sent
1. Check the recipient’s inbox to verify that the email was successfully sent.

Step 6: Additional Considerations
1. Handle Errors: Implement error handling to handle any potential errors during the email-sending process.
2. Use Templates: For more complex emails, consider using email templates with placeholders for dynamic content.
3. Enhance Security: Avoid hardcoding sensitive information in your script. Use environment variables or a configuration file to store credentials securely.

Congratulations, you have successfully set up to send mail via your Gmail in nodeJs.

--

--