How to send email in Node.Js?

Hey guys,

Today I want to show you how to use nodemailer in node.js to send email.

Node.js has a very active community that are adding new modules daily to the npm (node package module or node package manager), one my favorite module in node.js is nodemailer, this module helps you to send email to any recipient that you want, these are the steps that you need to take in order to use this module.

  1. You must install nodemailer as it is not a core module in node.js
Npm install nodemailer
  1. Create a object from the module.
const nodemailer = require('nodemailer');

 

  1. Create function that sends email and code all the configurations in this function:
</pre>
<pre>function sendEmail() {
// you need to create an object that contains all the sender email configuration
  var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
      user: 'your@email.com',
      pass: ' you password '
    }
  });
// You need to create an email content
  var mailOptions = {
    from: 'janfeshan.mehran@gmail.com',
    to: 'info@codingtips.net',
    subject: 'Your order is ready',
    text: 'Your order is ready for delivery',
    html: 'Your order is ready for delivery'
  };
//Pass the email content to transporter in order to send email.
  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log(error);
      res.redirect('/');
    } else {
      console.log('Message Sent: ' + info.response);
      res.redirect('/');
    }
  });
}</pre>
<pre>

Why Node.js?

Hey Guys,

I decided to start a tutorial about node.js. I know sounds is boring because there are plenty tutorials outside which can help you to learn. but in this tutorial I want to start differently. I want to start developing a real application. but we start it slowly and I will try to post  a few times per day. before we start lets say why node.js ?

My answer to this questions is : because node.js is very cool. hahaha… but if you really want to know why node.js,  you can look at this web page, this is a really good explanation about node.js

Why node.js?

Any way, after you read Why Node.js, please do not waste your time to search about it. if you really want to know more about it, you only have one choice:

  1. Move your ass and work on it … hahahah

How we can install Node.Js?

Ok, this a first question that you may ask and it is right question. to install node.Js, Please click Node.Js  and click on the Green INSTALL button ( I hope you can see it there !!!!! 😉  ). Just follow the install instruction and install the Node.Js,

Note: if you do not know how to install this app please turn off your PC and do something else. because installing node.js is as easy as installing Adobe Acrobat Reader 5.5.

How can check whether the Node.Js is installed properly or not?

it is a good questions. to check Node.Js installed properly or not, please open command prompt and type node -v

In response you may get something like this :

Node Version Control

I think for now is enough, I will try to post new things :

Bye bye