ProGrammer avatar

Nodejs Tutorial-- Sending Email using NodeMailer

pakgamer

Published: 22 Jul 2020 › Updated: 22 Jul 2020Nodejs Tutorial-- Sending Email using NodeMailer

Nodejs Tutorial-- Sending Email using NodeMailer

Hello Everyone

In this post, we will learn how to send Emails to any Email address using node.js. By following the steps at the end of this tutorial you will able to send any message through Email address. We are going to use Gmail to send Emails to other Gmail addresses. So without wasting time le's begin.

n2-2.png
Image source

  • Make sure you have Node.js installed. If you don't have it. go to the node.js site and download it or read the previous post in which I have installed the node.js.
  • Open vs code and download the node-modular from NPM(Node package Module).
    open terminal and type npm install node-mailer.

image.png

Now copy-paste the whole code.

const nodemailer = require('nodemailer');
const config = require('./config.json');

let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: config.from,
        pass: config.pass
    }
});
let mailOptions = {
    from: config.from,
    to: config.to,
    subject: 'Hive Post',
    text: 'This is mail sent by Noed-Mailer '
};

transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
        console.log(error);
    } else {
        console.log('Email sent to the given adress');
    }
});

Create a new file name it index.js and paste the whole code int it.
create another file and name it confog.json.

{
    "from": "YOUR-GMAIL",
    "to": "WHERE-YOU-WANT-SEND",
    "pass": "YOUR-PASSWORD"
}

copy the above code into it and change its values.

  • Now we are ready to send our Email. Let's run our code.

image.png

  • we got no error let's check the Gmail.

image.png

  • Let's send another Mail.

image.png

image.png


I think this much is enough for today. Now if you like the post please upvote and comment if you want to give me some advise
ntitled-1.png

Leave Nodejs Tutorial-- Sending Email using NodeMailer to:

Written by

Loves Coding, Gaming, Photography and LIFE

Read more #hive-148441 posts


Best Posts From ProGrammer

We have not curated any of pakgamer's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From ProGrammer