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 code

Below 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.js
Output

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: '******'
    }
});
Advertisements