
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
Node.js - Send an Email
Sending an emails from the Node.js application is a common requirement for user notifications, account verification and other automated communications. In this tutorial, we are going to explore how to send a emails using the Node.js with the Nodemailer package.
Nodemailer
It is the popular Node.js module that is used for sending the emails from the web applications and servers. It is widely used to for sending the account verification emails, password reset emails, and more. For installing the Nodemailer run the following command:
npm install nodemailer
It will install the Nodemailer and adds it to the node_modules directory.
Creating Basic Email Sender
Let's create a simple Node.js script for sending an email using the gmail account.
Importing and Configuration
Here, we are going to create a new file named 'tp1.js' and adding the following code:
const nodemailer = require('nodemailer'); const x = nodemailer.createTransport({ service: 'gmail', auth: { user: 'tutorialpoint.123@gmail.com', pass: '*******' } });
The 'x' object is responsible for sending the emails. we are going to specify the service as gmail and for authentication we are specifying the email credentials. If you are going to use the Gmail, you may need to enable the less secure apps or use the App Password for authentication.
Defining the Email Options
const y = { from: 'tutorialpoint.123@gmail.com', to: 'tp.143@yahoo.com', subject: 'Testing', text: 'Hello, Please provide the feedback.!' };
The 'y' object is used to define the email properties, including the sender, recipient, subject and email body content.
Using sendMail method
x.sendMail(y, (error, info) => { if (error) { console.log('Error:', error); } else { console.log('Email sent:', info.response); } });
The sendMail methods is used to send the email and return an error if the sending fails or a response on the success information.
Complete codeBelow is the complete code to be placed in the 'tp1.js' for sending the Email.
const nodemailer = require('nodemailer'); const x = nodemailer.createTransport({ service: 'gmail', auth: { user: 'tutorialpoint.123@gmail.com', pass: '******' } }); const y = { from: 'tutorialpoint.123@gmail.com', to: 'tp.143@yahoo.com', subject: 'Testing', text: 'Hello, Please provide the feedback.!' }; x.sendMail(y, (error, info) => { if (error) { console.log('Error:', error); } else { console.log('Email sent:', info.response); } });
Running the Script
After completion of placing the script in the file, we are going to check whether it is running successfully or not by using the following command:
node tp1.jsOutput
If everything is set up correctly, we are going to observe the output as shown below:
Email sent: 250 2.0.0 OK 1739957362 d9443c01a7336-220d545d072sm101195555ad.107 - gsmtp
Using the Custom SMTP Server
We can also use the custom SMTP server which allows you to send the emails programmatically. But for production environments, using the dedicated SMTP service like SendGrid, Mailgun, or AWS SES is highly recommended.
const x = nodemailer.createTransport({ host: 'smtp.gmail.com', port: 587, secure: false, auth: { user: 'tutorialpoint.123@gmail.com', pass: '******' } });