Nodejs Tutorial-- Sending Email using NodeMailer

in GEMS4 years ago

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

Sort:  

I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!

Congratulations @pakgamer! You received a personal badge!

Happy Hive Birthday! You are on the Hive blockchain for 1 year!

You can view your badges on your board And compare to others on the Ranking

Do not miss the last post from @hivebuzz:

Hive Power Up Day - Let's grow together!
The HiveBuzz Shop - New Items and Designs


@pakgamer, sorry to see you have less Hive Power.
Your level lowered and you are now a Red Fish!

Do not miss the last post from @hivebuzz:

Feedback from the last Hive Power Up Day