In this post we will discuss how you can send email using Node.js. It is important to note that in node.js there is a module for every implementation that you want to do. So if you want to send email using node.js you have to use the nodemailer module. This module is responsible for sending email in node.js. 

  • The Nodemailer module makes it easy to send emails from your computer.
  • The Nodemailer module can be downloaded and installed using npm:

You can also watch the below video to understand the full concept and I have also given some examples through which you can understand this module implementation.

Below is the example code to send emails using nodemailer module of node.js:

First we include the nodemailer module. Once the nodemailer module is included then we create the transport.

Node.js Create Transport :

var nodemailer = require(‘nodemailer’);
var transporter = nodemailer.createTransport({
service: ‘gmail’,
auth: {
user: ‘youremail@gmail.com‘,
pass: ‘yourpassword
}
});

2) Create Email Options like to, from , subject and message :

var mailOptions = {
from: ‘youremail@gmail.com‘,
to: ‘myfriend@yahoo.com‘,
subject: ‘Sending Email using Node.js’,
text: ‘That was easy!’
};

3) Send email using the transport created using nodemailer module of node.js

transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log(‘Email sent: ‘ + info.response);
}
});

 

177total visits,1visits today