How to send Emails using Node.js
 

Here we have mentioned some steps to send emails. If you want to send emails in Node JS then you can follow these steps. If you are a node js developer and looking for new jobs then here we have a list of top 50+ Node js interview questions and answers. These interview questions will really helpful to crack your interviews.

Send Email in node.js

Step 1. Installation

Install node mailer module with npm install nodemailer

Step 2. Include files

You have to include in var nodemailer = require('nodemailer'); your file where you want to use this.

Step 3. Use this code to send email

var transporter = mailer.createTransport({
service: 'gmail',
auth: {
   user: 'info@bestinterviewquestion.com',
   pass: 'PASSWORD'
}
});

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

Here is a simple EXAMPLE of sending emails using node.js.

var mailer = require('nodemailer');

var transporter = mailer.createTransport({
service: 'gmail',
auth: {
    user: 'info@bestinterviewquestion.com',
    pass: 'PASSWORD'
}
});

var options = {
    from: 'info@bestinterviewquestion.com',
    to: 'sales@bestinterviewquestion.com',
    subject: ‘Contact Us’,
    text: 'Hello admin, you got an inquiry from contact us form!'
}

If you want to use HTML in the body then you can use simple

var options = {
     from: 'info@bestinterviewquestion.com',
     to: 'sales@bestinterviewquestion.com',
     subject: ‘Contact Us’,
     html: '
<div>Hello admin. You got an enquiry from contact us form!</div>'
}

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

If you want to add multiple receivers then you have to add simply. You can add multiple email address comma separated.

var options = {
      from: 'info@bestinterviewquestion.com',
      to: 'sales@bestinterviewquestion.com, admin@bestinterviewquestion.com',
      subject: ‘Contact Us’,
      text: 'Hello admin, you got an inquiry from contact us form!'
}